Skip to content

Commit b667cab

Browse files
Tolriqsjudd
authored andcommitted
Implement a new optional model interface Model. (bumptech#2353)
This allows models to handle equity for internal states that can be different from the target image result that is handled with equals()/hashCode() For example to allow restart of image download from a new source but still using the same cache in Glide.
1 parent 648c58e commit b667cab

File tree

4 files changed

+44
-8
lines changed

4 files changed

+44
-8
lines changed

library/src/main/java/com/bumptech/glide/RequestBuilder.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -359,15 +359,11 @@ public <Y extends Target<TranscodeType>> Y into(@NonNull Y target) {
359359
Request request = buildRequest(target);
360360

361361
Request previous = target.getRequest();
362-
// When request was failed or cancelled, be sure to use the updated model as it can contains
363-
// unexposed data that could help the request to succeed on restart.
364-
// See https://github.com/bumptech/glide/issues/2270
365-
if (request.isEquivalentTo(previous)
366-
&& (Preconditions.checkNotNull(previous).isComplete()
367-
|| Preconditions.checkNotNull(previous).isRunning())) {
362+
if (request.isEquivalentTo(previous)) {
368363
request.recycle();
369364
// If the request is completed, beginning again will ensure the result is re-delivered,
370-
// triggering RequestListeners and Targets. If the request is already
365+
// triggering RequestListeners and Targets. If the request is failed, beginning again will
366+
// restart the request, giving it another chance to complete. If the request is already
371367
// running, we can let it continue running without interruption.
372368
if (!Preconditions.checkNotNull(previous).isRunning()) {
373369
previous.begin();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.bumptech.glide.load.model;
2+
3+
/**
4+
* An optional interface that models can implement to enhance control over Glide behaviors.
5+
*/
6+
public interface Model {
7+
8+
/**
9+
* Returns {@code true} if this model produces the same image using the same mechanism
10+
* (server, authentication, source etc) as the given model.
11+
* <p>
12+
* Models must also implement {@link #equals(Object other)} and {@link #hashCode()}
13+
* to ensure that caching functions correctly.
14+
* If this object returns {@code true} from this method for a given Model,
15+
* it must also be equal to and have the same hash code as the given model.
16+
* <p>
17+
* However, this model may be equal to and have the same hash code as a given model
18+
* but still return {@code false} from this method.
19+
* This method optionally allows you to differentiate between Models that load the same image
20+
* via multiple different means.
21+
* For example one Model might load the image from server A and another model might load
22+
* the same image from server B.
23+
* The models must be equal to each other with the same hash code because they load
24+
* the same image. However two requests made with the different models are not exactly the
25+
* same because the way the image is loaded will differ.
26+
*/
27+
boolean isEquivalentTo(Object other);
28+
}

library/src/main/java/com/bumptech/glide/request/SingleRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ public boolean isEquivalentTo(Request o) {
578578
SingleRequest that = (SingleRequest) o;
579579
return overrideWidth == that.overrideWidth
580580
&& overrideHeight == that.overrideHeight
581-
&& Util.bothNullOrEqual(model, that.model)
581+
&& Util.bothModelsNullEquivalentOrEquals(model, that.model)
582582
&& transcodeClass.equals(that.transcodeClass)
583583
&& requestOptions.equals(that.requestOptions)
584584
&& priority == that.priority;

library/src/main/java/com/bumptech/glide/util/Util.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import android.graphics.Bitmap;
55
import android.os.Build;
66
import android.os.Looper;
7+
8+
import com.bumptech.glide.load.model.Model;
79
import com.bumptech.glide.request.target.Target;
810
import java.util.ArrayDeque;
911
import java.util.ArrayList;
@@ -190,6 +192,16 @@ public static boolean bothNullOrEqual(Object a, Object b) {
190192
return a == null ? b == null : a.equals(b);
191193
}
192194

195+
public static boolean bothModelsNullEquivalentOrEquals(Object a, Object b) {
196+
if (a == null) {
197+
return b == null;
198+
}
199+
if (a instanceof Model) {
200+
return ((Model) a).isEquivalentTo(b);
201+
}
202+
return a.equals(b);
203+
}
204+
193205
public static int hashCode(int value) {
194206
return hashCode(value, HASH_ACCUMULATOR);
195207
}

0 commit comments

Comments
 (0)