From 08c41a69fa5818c3d318c42301c145f2f46e4a76 Mon Sep 17 00:00:00 2001 From: Sam Judd Date: Sun, 12 Oct 2014 15:36:13 -0700 Subject: [PATCH] Add a clone() API to create copies of builders. --- checkstyle_suppressions.xml | 1 + .../java/com/bumptech/glide/GlideTest.java | 33 +++++++++++++++++ .../bumptech/glide/BitmapRequestBuilder.java | 6 ++++ .../glide/DrawableRequestBuilder.java | 5 +++ .../bumptech/glide/GenericRequestBuilder.java | 35 +++++++++++++++---- .../com/bumptech/glide/GifRequestBuilder.java | 5 +++ .../glide/provider/ChildLoadProvider.java | 12 ++++++- .../glide/samples/flickr/FlickrPhotoGrid.java | 7 +--- .../glide/samples/flickr/FlickrPhotoList.java | 12 +------ .../glide/samples/giphy/MainActivity.java | 12 ++----- 10 files changed, 94 insertions(+), 34 deletions(-) diff --git a/checkstyle_suppressions.xml b/checkstyle_suppressions.xml index c8daa99c0c..1d8da750bd 100644 --- a/checkstyle_suppressions.xml +++ b/checkstyle_suppressions.xml @@ -7,5 +7,6 @@ + diff --git a/library/src/androidTest/java/com/bumptech/glide/GlideTest.java b/library/src/androidTest/java/com/bumptech/glide/GlideTest.java index 1777f7d184..593485f801 100644 --- a/library/src/androidTest/java/com/bumptech/glide/GlideTest.java +++ b/library/src/androidTest/java/com/bumptech/glide/GlideTest.java @@ -30,6 +30,7 @@ import com.bumptech.glide.load.resource.bytes.BytesResource; import com.bumptech.glide.load.resource.drawable.GlideDrawable; import com.bumptech.glide.load.resource.gif.GifDrawable; +import com.bumptech.glide.load.resource.gifbitmap.GifBitmapWrapper; import com.bumptech.glide.load.resource.transcode.ResourceTranscoder; import com.bumptech.glide.manager.Lifecycle; import com.bumptech.glide.request.Request; @@ -676,6 +677,38 @@ public void testByteData() { requestManager.load(data).into(target); } + @Test + public void testClone() throws IOException { + GlideDrawable firstResult = mock(GlideDrawable.class); + Resource firstResource = mock(Resource.class); + when(firstResource.get()).thenReturn(firstResult); + ResourceTranscoder firstTranscoder = mock(ResourceTranscoder.class); + when(firstTranscoder.transcode(any(Resource.class))).thenReturn(firstResource); + when(firstTranscoder.getId()).thenReturn("transcoder1"); + + GlideDrawable secondResult = mock(GlideDrawable.class); + Resource secondResource = mock(Resource.class); + when(secondResource.get()).thenReturn(secondResult); + ResourceTranscoder secondTranscoder = mock(ResourceTranscoder.class); + when(secondTranscoder.transcode(any(Resource.class))).thenReturn(secondResource); + when(secondTranscoder.getId()).thenReturn("transcoder2"); + + DrawableRequestBuilder firstRequest = requestManager.from(String.class).transcoder(firstTranscoder) + .override(100, 100); + DrawableRequestBuilder secondRequest = firstRequest.clone().transcoder(secondTranscoder); + + Target firstTarget = mock(Target.class); + Target secondTarget = mock(Target.class); + + String fakeUri = mockUri("content://fakeUri"); + + firstRequest.load(fakeUri).into(firstTarget); + verify(firstTarget).onResourceReady(eq(firstResult), any(GlideAnimation.class)); + + secondRequest.load(fakeUri).into(secondTarget); + verify(secondTarget).onResourceReady(eq(secondResult), any(GlideAnimation.class)); + } + @SuppressWarnings("unchecked") private void registerFailFactory(Class failModel, Class failResource) throws Exception { DataFetcher failFetcher = mock(DataFetcher.class); diff --git a/library/src/main/java/com/bumptech/glide/BitmapRequestBuilder.java b/library/src/main/java/com/bumptech/glide/BitmapRequestBuilder.java index 4aaee99d27..f4941850d0 100644 --- a/library/src/main/java/com/bumptech/glide/BitmapRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/BitmapRequestBuilder.java @@ -44,6 +44,7 @@ public class BitmapRequestBuilder extends GenericRequestBuilder implements BitmapOptions { private final BitmapPool bitmapPool; + private Downsampler downsampler = Downsampler.AT_LEAST; private DecodeFormat decodeFormat = DecodeFormat.PREFER_RGB_565; private ResourceDecoder imageDecoder; @@ -458,6 +459,11 @@ public BitmapRequestBuilder load(ModelType model) { return this; } + @Override + public BitmapRequestBuilder clone() { + return (BitmapRequestBuilder) super.clone(); + } + /** * {@inheritDoc} * diff --git a/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java b/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java index 0a7b9bcebd..7880276509 100644 --- a/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/DrawableRequestBuilder.java @@ -412,6 +412,11 @@ public DrawableRequestBuilder load(ModelType model) { return this; } + @Override + public DrawableRequestBuilder clone() { + return (DrawableRequestBuilder) super.clone(); + } + /** * {@inheritDoc} * diff --git a/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java b/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java index 802b784251..84b1828f63 100644 --- a/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/GenericRequestBuilder.java @@ -44,18 +44,17 @@ * @param The type of the resource that will be loaded. * @param The type of resource the decoded resource will be transcoded to. */ -public class GenericRequestBuilder { +public class GenericRequestBuilder implements Cloneable { protected final Class modelClass; protected final Context context; protected final Glide glide; protected final Class transcodeClass; protected final RequestTracker requestTracker; protected final Lifecycle lifecycle; - private final ChildLoadProvider loadProvider; - - private Key signature = EmptySignature.obtain(); + private ChildLoadProvider loadProvider; private ModelType model; + private Key signature = EmptySignature.obtain(); // model may occasionally be null, so to enforce that load() was called, set a boolean rather than relying on model // not to be null. private boolean isModelSet; @@ -80,11 +79,11 @@ public class GenericRequestBuilder transcodeClass, GenericRequestBuilder other) { this(other.context, other.modelClass, loadProvider, transcodeClass, other.glide, other.requestTracker, other.lifecycle); + this.model = other.model; + this.isModelSet = other.isModelSet; this.signature = other.signature; this.diskCacheStrategy = other.diskCacheStrategy; this.isCacheable = other.isCacheable; - this.model = other.model; - this.isModelSet = other.isModelSet; } GenericRequestBuilder(Context context, Class modelClass, @@ -559,6 +558,28 @@ public GenericRequestBuilder l return this; } + /** + * Returns a copy of this request builder with all of the options set so far on this builder. + * + *

+ * This method returns a "deep" copy in that all non-immutable arguments are copied such that changes to one + * builder will not affect the other builder. However, in addition to immutable arguments, the current model + * is not copied copied so changes to the model will affect both builders. + *

+ */ + @SuppressWarnings("unchecked") + @Override + public GenericRequestBuilder clone() { + try { + GenericRequestBuilder clone = + (GenericRequestBuilder) super.clone(); + clone.loadProvider = loadProvider != null ? loadProvider.clone() : null; + return clone; + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } + /** * Set the target the resource will be loaded into. * @@ -729,7 +750,7 @@ private Request buildRequestRecursive(Target target, ThumbnailReq } } - private Request obtainRequest(Target target, float sizeMultiplier, Priority priority, + private Request obtainRequest(Target target, float sizeMultiplier, Priority priority, RequestCoordinator requestCoordinator) { return GenericRequest.obtain( loadProvider, diff --git a/library/src/main/java/com/bumptech/glide/GifRequestBuilder.java b/library/src/main/java/com/bumptech/glide/GifRequestBuilder.java index 5e63549de5..ffde6fe1d7 100644 --- a/library/src/main/java/com/bumptech/glide/GifRequestBuilder.java +++ b/library/src/main/java/com/bumptech/glide/GifRequestBuilder.java @@ -403,6 +403,11 @@ public GifRequestBuilder load(ModelType model) { return this; } + @Override + public GifRequestBuilder clone() { + return (GifRequestBuilder) super.clone(); + } + @Override void applyFitCenter() { fitCenter(); diff --git a/library/src/main/java/com/bumptech/glide/provider/ChildLoadProvider.java b/library/src/main/java/com/bumptech/glide/provider/ChildLoadProvider.java index ee07f74cec..5478eab210 100644 --- a/library/src/main/java/com/bumptech/glide/provider/ChildLoadProvider.java +++ b/library/src/main/java/com/bumptech/glide/provider/ChildLoadProvider.java @@ -17,7 +17,7 @@ * @param The type of the resource that will be decoded from the data. * @param The type of the resource that will be transcoded from the decoded resource. */ -public class ChildLoadProvider implements LoadProvider { +public class ChildLoadProvider implements LoadProvider, Cloneable { private final LoadProvider parent; private ResourceDecoder cacheDecoder; @@ -141,4 +141,14 @@ public ResourceTranscoder getTranscoder() { return parent.getTranscoder(); } } + + @SuppressWarnings("unchecked") + @Override + public ChildLoadProvider clone() { + try { + return (ChildLoadProvider) super.clone(); + } catch (CloneNotSupportedException e) { + throw new RuntimeException(e); + } + } } diff --git a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java index 4f97391387..12a676f028 100644 --- a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java +++ b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoGrid.java @@ -8,7 +8,6 @@ import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; - import com.bumptech.glide.DrawableRequestBuilder; import com.bumptech.glide.GenericRequestBuilder; import com.bumptech.glide.Glide; @@ -64,14 +63,10 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa thumbnailRequest = Glide.with(this) .from(Photo.class) - .priority(Priority.HIGH) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE); - preloadRequest = thumbnail ? thumbnailRequest : Glide.with(this) - .from(Photo.class) - .priority(Priority.HIGH) - .centerCrop(); + preloadRequest = thumbnail ? thumbnailRequest.clone().priority(Priority.HIGH) : fullRequest; final View result = inflater.inflate(R.layout.flickr_photo_grid, container, false); grid = (GridView) result.findViewById(R.id.images); diff --git a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java index 19537c98ac..2627f6602f 100644 --- a/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java +++ b/samples/flickr/src/main/java/com/bumptech/glide/samples/flickr/FlickrPhotoList.java @@ -11,12 +11,10 @@ import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; - import com.bumptech.glide.DrawableRequestBuilder; import com.bumptech.glide.GenericRequestBuilder; import com.bumptech.glide.Glide; import com.bumptech.glide.ListPreloader; -import com.bumptech.glide.Priority; import com.bumptech.glide.load.engine.DiskCacheStrategy; import com.bumptech.glide.samples.flickr.api.Api; import com.bumptech.glide.samples.flickr.api.Photo; @@ -36,7 +34,6 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer { private ListView list; private DrawableRequestBuilder fullRequest; private DrawableRequestBuilder thumbRequest; - private DrawableRequestBuilder preloadRequest; public static FlickrPhotoList newInstance() { return new FlickrPhotoList(); @@ -67,18 +64,11 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sa .placeholder(new ColorDrawable(Color.GRAY)) .centerCrop(); - preloadRequest = Glide.with(FlickrPhotoList.this) - .from(Photo.class) - .placeholder(new ColorDrawable(Color.GRAY)) - .centerCrop() - .priority(Priority.HIGH); - thumbRequest = Glide.with(FlickrPhotoList.this) .from(Photo.class) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .override(Api.SQUARE_THUMB_SIZE, Api.SQUARE_THUMB_SIZE); - if (savedInstanceState != null) { int index = savedInstanceState.getInt(STATE_POSITION_INDEX); int offset = savedInstanceState.getInt(STATE_POSITION_OFFSET); @@ -139,7 +129,7 @@ protected List getItems(int start, int end) { @Override protected GenericRequestBuilder getRequestBuilder(Photo item) { - return preloadRequest + return fullRequest .thumbnail(thumbRequest.load(item)) .load(item); } diff --git a/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java b/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java index 6f5fd6421c..e8b25e716e 100644 --- a/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java +++ b/samples/giphy/src/main/java/com/bumptech/glide/samples/giphy/MainActivity.java @@ -12,7 +12,6 @@ import com.bumptech.glide.GenericRequestBuilder; import com.bumptech.glide.Glide; import com.bumptech.glide.ListPreloader; -import com.bumptech.glide.Priority; import java.io.InputStream; import java.util.ArrayList; @@ -25,7 +24,7 @@ public class MainActivity extends Activity implements Api.Monitor { private static final String TAG = "GiphyActivity"; private GifAdapter adapter; - private DrawableRequestBuilder preloadRequest; + private DrawableRequestBuilder fullRequest; @Override protected void onCreate(Bundle savedInstanceState) { @@ -44,15 +43,10 @@ protected void onCreate(Bundle savedInstanceState) { ListView gifList = (ListView) findViewById(R.id.gif_list); GiphyPreloader preloader = new GiphyPreloader(2); - DrawableRequestBuilder fullRequest = Glide.with(this) + fullRequest = Glide.with(this) .from(Api.GifResult.class) .fitCenter(); - preloadRequest = Glide.with(this) - .from(Api.GifResult.class) - .fitCenter() - .priority(Priority.HIGH); - adapter = new GifAdapter(this, preloader, fullRequest); gifList.setAdapter(adapter); gifList.setOnScrollListener(preloader); @@ -99,7 +93,7 @@ protected List getItems(int start, int end) { @Override protected GenericRequestBuilder getRequestBuilder(Api.GifResult item) { - return preloadRequest.load(item); + return fullRequest.load(item); } }