-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add API to start a new Request when a load fails.
We’re going with .error() to match the behavior of the .error Drawable and resource id methods. fallback() is a somewhat better name, but we unfortunately already use it to handle cases where models are null and expected to be null. Fixes #451.
- Loading branch information
Showing
5 changed files
with
249 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
library/src/main/java/com/bumptech/glide/request/ErrorRequestCoordinator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package com.bumptech.glide.request; | ||
|
||
/** | ||
* Runs a single primary {@link Request} until it completes and then a fallback error request only | ||
* if the single primary request fails. | ||
*/ | ||
public final class ErrorRequestCoordinator implements RequestCoordinator, | ||
Request { | ||
|
||
private final RequestCoordinator coordinator; | ||
private Request primary; | ||
private Request error; | ||
|
||
public ErrorRequestCoordinator(RequestCoordinator coordinator) { | ||
this.coordinator = coordinator; | ||
} | ||
|
||
public void setRequests(Request primary, Request error) { | ||
this.primary = primary; | ||
this.error = error; | ||
} | ||
|
||
@Override | ||
public void begin() { | ||
if (!primary.isRunning()) { | ||
primary.begin(); | ||
} | ||
} | ||
|
||
@Override | ||
public void pause() { | ||
if (!primary.isFailed()) { | ||
primary.pause(); | ||
} | ||
if (error.isRunning()) { | ||
error.pause(); | ||
} | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
if (primary.isFailed()) { | ||
error.clear(); | ||
} else { | ||
primary.clear(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isPaused() { | ||
return primary.isFailed() ? error.isPaused() : primary.isPaused(); | ||
} | ||
|
||
@Override | ||
public boolean isRunning() { | ||
return primary.isFailed() ? error.isRunning() : primary.isRunning(); | ||
} | ||
|
||
@Override | ||
public boolean isComplete() { | ||
return primary.isFailed() ? error.isComplete() : primary.isComplete(); | ||
} | ||
|
||
@Override | ||
public boolean isResourceSet() { | ||
return primary.isFailed() ? error.isResourceSet() : primary.isResourceSet(); | ||
} | ||
|
||
@Override | ||
public boolean isCancelled() { | ||
return primary.isFailed() ? error.isCancelled() : primary.isCancelled(); | ||
} | ||
|
||
@Override | ||
public boolean isFailed() { | ||
return primary.isFailed() && error.isFailed(); | ||
} | ||
|
||
@Override | ||
public void recycle() { | ||
primary.recycle(); | ||
error.recycle(); | ||
} | ||
|
||
@Override | ||
public boolean isEquivalentTo(Request o) { | ||
if (o instanceof ErrorRequestCoordinator) { | ||
ErrorRequestCoordinator other = (ErrorRequestCoordinator) o; | ||
return primary.isEquivalentTo(other.primary) && error.isEquivalentTo(other.error); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean canSetImage(Request request) { | ||
return parentCanSetImage() && isValidRequest(request); | ||
} | ||
|
||
private boolean parentCanSetImage() { | ||
return coordinator == null || coordinator.canSetImage(this); | ||
} | ||
|
||
@Override | ||
public boolean canNotifyStatusChanged(Request request) { | ||
return parentCanNotifyStatusChanged() && isValidRequest(request); | ||
} | ||
|
||
private boolean parentCanNotifyStatusChanged() { | ||
return coordinator == null || coordinator.canNotifyStatusChanged(this); | ||
} | ||
|
||
private boolean isValidRequest(Request request) { | ||
return request.equals(primary) || (primary.isFailed() && request.equals(error)); | ||
} | ||
|
||
@Override | ||
public boolean isAnyResourceSet() { | ||
return parentIsAnyResourceSet() || isResourceSet(); | ||
} | ||
|
||
private boolean parentIsAnyResourceSet() { | ||
return coordinator != null && coordinator.isAnyResourceSet(); | ||
} | ||
|
||
@Override | ||
public void onRequestSuccess(Request request) { | ||
if (coordinator != null) { | ||
coordinator.onRequestSuccess(this); | ||
} | ||
} | ||
|
||
@Override | ||
public void onRequestFailed(Request request) { | ||
if (!request.equals(error)) { | ||
if (!error.isRunning()) { | ||
error.begin(); | ||
} | ||
return; | ||
} | ||
|
||
if (coordinator != null) { | ||
coordinator.onRequestFailed(error); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters