Skip to content

Commit

Permalink
Add a clone() API to create copies of builders.
Browse files Browse the repository at this point in the history
  • Loading branch information
sjudd committed Oct 14, 2014
1 parent 16ac7e1 commit 08c41a6
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 34 deletions.
1 change: 1 addition & 0 deletions checkstyle_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@
<suppressions>
<suppress files=".*[/\\]library[/\\]src[/\\]androidTest[/\\].*" checks="Javadoc.*"/>
<suppress files=".*[/\\]gif_encoder[/\\].*" checks=".*"/>
<suppress files=".*RequestBuilder.java|ChildLoadProvider.java" checks="NoClone" />
</suppressions>

33 changes: 33 additions & 0 deletions library/src/androidTest/java/com/bumptech/glide/GlideTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -676,6 +677,38 @@ public void testByteData() {
requestManager.load(data).into(target);
}

@Test
public void testClone() throws IOException {
GlideDrawable firstResult = mock(GlideDrawable.class);
Resource<GlideDrawable> firstResource = mock(Resource.class);
when(firstResource.get()).thenReturn(firstResult);
ResourceTranscoder<GifBitmapWrapper, GlideDrawable> firstTranscoder = mock(ResourceTranscoder.class);
when(firstTranscoder.transcode(any(Resource.class))).thenReturn(firstResource);
when(firstTranscoder.getId()).thenReturn("transcoder1");

GlideDrawable secondResult = mock(GlideDrawable.class);
Resource<GlideDrawable> secondResource = mock(Resource.class);
when(secondResource.get()).thenReturn(secondResult);
ResourceTranscoder<GifBitmapWrapper, GlideDrawable> secondTranscoder = mock(ResourceTranscoder.class);
when(secondTranscoder.transcode(any(Resource.class))).thenReturn(secondResource);
when(secondTranscoder.getId()).thenReturn("transcoder2");

DrawableRequestBuilder<String> firstRequest = requestManager.from(String.class).transcoder(firstTranscoder)
.override(100, 100);
DrawableRequestBuilder<String> 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 <T, Z> void registerFailFactory(Class<T> failModel, Class<Z> failResource) throws Exception {
DataFetcher<Z> failFetcher = mock(DataFetcher.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
public class BitmapRequestBuilder<ModelType, TranscodeType>
extends GenericRequestBuilder<ModelType, ImageVideoWrapper, Bitmap, TranscodeType> implements BitmapOptions {
private final BitmapPool bitmapPool;

private Downsampler downsampler = Downsampler.AT_LEAST;
private DecodeFormat decodeFormat = DecodeFormat.PREFER_RGB_565;
private ResourceDecoder<InputStream, Bitmap> imageDecoder;
Expand Down Expand Up @@ -458,6 +459,11 @@ public BitmapRequestBuilder<ModelType, TranscodeType> load(ModelType model) {
return this;
}

@Override
public BitmapRequestBuilder<ModelType, TranscodeType> clone() {
return (BitmapRequestBuilder<ModelType, TranscodeType>) super.clone();
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@ public DrawableRequestBuilder<ModelType> load(ModelType model) {
return this;
}

@Override
public DrawableRequestBuilder<ModelType> clone() {
return (DrawableRequestBuilder<ModelType>) super.clone();
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,17 @@
* @param <ResourceType> The type of the resource that will be loaded.
* @param <TranscodeType> The type of resource the decoded resource will be transcoded to.
*/
public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> {
public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> implements Cloneable {
protected final Class<ModelType> modelClass;
protected final Context context;
protected final Glide glide;
protected final Class<TranscodeType> transcodeClass;
protected final RequestTracker requestTracker;
protected final Lifecycle lifecycle;
private final ChildLoadProvider<ModelType, DataType, ResourceType, TranscodeType> loadProvider;

private Key signature = EmptySignature.obtain();
private ChildLoadProvider<ModelType, DataType, ResourceType, TranscodeType> 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;
Expand All @@ -80,11 +79,11 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
Class<TranscodeType> transcodeClass, GenericRequestBuilder<ModelType, ?, ?, ?> 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<ModelType> modelClass,
Expand Down Expand Up @@ -559,6 +558,28 @@ public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> l
return this;
}

/**
* Returns a copy of this request builder with all of the options set so far on this builder.
*
* <p>
* 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.
* </p>
*/
@SuppressWarnings("unchecked")
@Override
public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> clone() {
try {
GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> clone =
(GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType>) 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.
*
Expand Down Expand Up @@ -729,7 +750,7 @@ private Request buildRequestRecursive(Target<TranscodeType> target, ThumbnailReq
}
}

private <Z> Request obtainRequest(Target<TranscodeType> target, float sizeMultiplier, Priority priority,
private Request obtainRequest(Target<TranscodeType> target, float sizeMultiplier, Priority priority,
RequestCoordinator requestCoordinator) {
return GenericRequest.obtain(
loadProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ public GifRequestBuilder<ModelType> load(ModelType model) {
return this;
}

@Override
public GifRequestBuilder<ModelType> clone() {
return (GifRequestBuilder<ModelType>) super.clone();
}

@Override
void applyFitCenter() {
fitCenter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* @param <Z> The type of the resource that will be decoded from the data.
* @param <R> The type of the resource that will be transcoded from the decoded resource.
*/
public class ChildLoadProvider<A, T, Z, R> implements LoadProvider<A, T, Z, R> {
public class ChildLoadProvider<A, T, Z, R> implements LoadProvider<A, T, Z, R>, Cloneable {
private final LoadProvider<A, T, Z, R> parent;

private ResourceDecoder<File, Z> cacheDecoder;
Expand Down Expand Up @@ -141,4 +141,14 @@ public ResourceTranscoder<Z, R> getTranscoder() {
return parent.getTranscoder();
}
}

@SuppressWarnings("unchecked")
@Override
public ChildLoadProvider<A, T, Z, R> clone() {
try {
return (ChildLoadProvider<A, T, Z, R>) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -36,7 +34,6 @@ public class FlickrPhotoList extends Fragment implements PhotoViewer {
private ListView list;
private DrawableRequestBuilder<Photo> fullRequest;
private DrawableRequestBuilder<Photo> thumbRequest;
private DrawableRequestBuilder<Photo> preloadRequest;

public static FlickrPhotoList newInstance() {
return new FlickrPhotoList();
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -139,7 +129,7 @@ protected List<Photo> getItems(int start, int end) {

@Override
protected GenericRequestBuilder getRequestBuilder(Photo item) {
return preloadRequest
return fullRequest
.thumbnail(thumbRequest.load(item))
.load(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +24,7 @@ public class MainActivity extends Activity implements Api.Monitor {
private static final String TAG = "GiphyActivity";

private GifAdapter adapter;
private DrawableRequestBuilder<Api.GifResult> preloadRequest;
private DrawableRequestBuilder<Api.GifResult> fullRequest;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -44,15 +43,10 @@ protected void onCreate(Bundle savedInstanceState) {
ListView gifList = (ListView) findViewById(R.id.gif_list);
GiphyPreloader preloader = new GiphyPreloader(2);

DrawableRequestBuilder<Api.GifResult> 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);
Expand Down Expand Up @@ -99,7 +93,7 @@ protected List<Api.GifResult> getItems(int start, int end) {

@Override
protected GenericRequestBuilder getRequestBuilder(Api.GifResult item) {
return preloadRequest.load(item);
return fullRequest.load(item);
}
}

Expand Down

0 comments on commit 08c41a6

Please sign in to comment.