8
8
import android .support .annotation .Nullable ;
9
9
import android .widget .ImageView ;
10
10
import com .bumptech .glide .load .engine .DiskCacheStrategy ;
11
+ import com .bumptech .glide .request .ErrorRequestCoordinator ;
11
12
import com .bumptech .glide .request .FutureTarget ;
12
13
import com .bumptech .glide .request .Request ;
13
14
import com .bumptech .glide .request .RequestCoordinator ;
@@ -55,6 +56,7 @@ public class RequestBuilder<TranscodeType> implements Cloneable {
55
56
// than relying on model not to be null.
56
57
@ Nullable private RequestListener <TranscodeType > requestListener ;
57
58
@ Nullable private RequestBuilder <TranscodeType > thumbnailBuilder ;
59
+ @ Nullable private RequestBuilder <TranscodeType > errorBuilder ;
58
60
@ Nullable private Float thumbSizeMultiplier ;
59
61
private boolean isDefaultTransitionOptionsSet = true ;
60
62
private boolean isModelSet ;
@@ -115,7 +117,7 @@ public RequestBuilder<TranscodeType> transition(
115
117
}
116
118
117
119
/**
118
- * Sets a RequestBuilder listener to monitor the resource load. It's best to create a single
120
+ * Sets a {@link RequestListener} to monitor the resource load. It's best to create a single
119
121
* instance of an exception handler per type of request (usually activity/fragment) rather than
120
122
* pass one in per request to avoid some redundant object allocation.
121
123
*
@@ -131,6 +133,34 @@ public RequestBuilder<TranscodeType> listener(
131
133
return this ;
132
134
}
133
135
136
+ /**
137
+ * Sets a {@link RequestBuilder} that is built and run iff the load started by this
138
+ * {@link RequestBuilder} fails.
139
+ *
140
+ * <p>If this {@link RequestBuilder} uses a thumbnail that succeeds the given error
141
+ * {@link RequestBuilder} will be started anyway if the non-thumbnail request fails.
142
+ *
143
+ * <p>Recursive calls to {@link #error(RequestBuilder)} as well as calls to
144
+ * {@link #thumbnail(float)} and {@link #thumbnail(RequestBuilder)} are supported for the given
145
+ * error {@link RequestBuilder}.
146
+ *
147
+ * <p>Unlike {@link #thumbnail(RequestBuilder)} and {@link #thumbnail(float)}, no options from
148
+ * this primary {@link RequestBuilder} are propagated to the given error {@link RequestBuilder}.
149
+ * Options like priority, override widths and heights and transitions must be applied
150
+ * independently to the error builder.
151
+ *
152
+ * <p>The given {@link RequestBuilder} will start and potentially override a fallback drawable
153
+ * if it's set on this {@link RequestBuilder} via
154
+ * {@link RequestOptions#fallback(android.graphics.drawable.Drawable)} or
155
+ * {@link RequestOptions#fallback(int)}.
156
+ *
157
+ * @return This {@link RequestBuilder}.
158
+ */
159
+ public RequestBuilder <TranscodeType > error (@ Nullable RequestBuilder <TranscodeType > errorBuilder ) {
160
+ this .errorBuilder = errorBuilder ;
161
+ return this ;
162
+ }
163
+
134
164
/**
135
165
* Loads and displays the resource retrieved by the given thumbnail request if it finishes before
136
166
* this request. Best used for loading thumbnail resources that are smaller and will be loaded
@@ -607,9 +637,55 @@ private Request buildRequest(Target<TranscodeType> target, RequestOptions reques
607
637
}
608
638
609
639
private Request buildRequestRecursive (Target <TranscodeType > target ,
610
- @ Nullable ThumbnailRequestCoordinator parentCoordinator ,
640
+ @ Nullable RequestCoordinator parentCoordinator ,
611
641
TransitionOptions <?, ? super TranscodeType > transitionOptions ,
612
642
Priority priority , int overrideWidth , int overrideHeight , RequestOptions requestOptions ) {
643
+
644
+ // Build the ErrorRequestCoordinator first if necessary so we can update parentCoordinator.
645
+ ErrorRequestCoordinator errorRequestCoordinator = null ;
646
+ if (errorBuilder != null ) {
647
+ errorRequestCoordinator = new ErrorRequestCoordinator (parentCoordinator );
648
+ parentCoordinator = errorRequestCoordinator ;
649
+ }
650
+
651
+ Request mainRequest =
652
+ buildThumbnailRequestRecursive (
653
+ target ,
654
+ parentCoordinator ,
655
+ transitionOptions ,
656
+ priority ,
657
+ overrideWidth ,
658
+ overrideHeight ,
659
+ requestOptions );
660
+
661
+ if (errorRequestCoordinator == null ) {
662
+ return mainRequest ;
663
+ }
664
+
665
+ int errorOverrideWidth = errorBuilder .requestOptions .getOverrideWidth ();
666
+ int errorOverrideHeight = errorBuilder .requestOptions .getOverrideHeight ();
667
+ if (Util .isValidDimensions (overrideWidth , overrideHeight )
668
+ && !errorBuilder .requestOptions .isValidOverride ()) {
669
+ errorOverrideWidth = requestOptions .getOverrideWidth ();
670
+ errorOverrideHeight = requestOptions .getOverrideHeight ();
671
+ }
672
+
673
+ Request errorRequest = errorBuilder .buildRequestRecursive (
674
+ target ,
675
+ errorRequestCoordinator ,
676
+ errorBuilder .transitionOptions ,
677
+ errorBuilder .requestOptions .getPriority (),
678
+ errorOverrideWidth ,
679
+ errorOverrideHeight ,
680
+ errorBuilder .requestOptions );
681
+ errorRequestCoordinator .setRequests (mainRequest , errorRequest );
682
+ return errorRequestCoordinator ;
683
+ }
684
+
685
+ private Request buildThumbnailRequestRecursive (Target <TranscodeType > target ,
686
+ @ Nullable RequestCoordinator parentCoordinator ,
687
+ TransitionOptions <?, ? super TranscodeType > transitionOptions ,
688
+ Priority priority , int overrideWidth , int overrideHeight , RequestOptions requestOptions ) {
613
689
if (thumbnailBuilder != null ) {
614
690
// Recursive case: contains a potentially recursive thumbnail request builder.
615
691
if (isThumbnailBuilt ) {
0 commit comments