提交 95482113 编写于 作者: S Sam Judd

Add a fallback Drawable to display for null models

Fixes #268
上级 b9a2d3b2
......@@ -77,6 +77,8 @@ public class GenericRequestTest {
int placeholderResourceId = 0;
Drawable placeholderDrawable = null;
int errorResourceId = 0;
Drawable fallbackDrawable = null;
int fallbackResourceId = 0;
Transformation transformation = mock(Transformation.class);
Drawable errorDrawable = null;
LoadProvider<Number, Object, Object, List> loadProvider = mock(LoadProvider.class);
......@@ -122,6 +124,8 @@ public class GenericRequestTest {
placeholderResourceId,
errorDrawable,
errorResourceId,
fallbackDrawable,
fallbackResourceId,
requestListener,
requestCoordinator,
engine,
......@@ -331,7 +335,8 @@ public class GenericRequestTest {
assertTrue(request.isFailed());
verify(harness.requestListener)
.onException(any(Exception.class), any(Number.class), eq(harness.target), anyBoolean());
.onException(any(Exception.class), any(Number.class), eq(harness.target),
anyBoolean());
}
@Test
......@@ -357,7 +362,8 @@ public class GenericRequestTest {
assertTrue(request.isFailed());
verify(harness.engine).release(eq(harness.resource));
verify(harness.requestListener)
.onException(any(Exception.class), any(Number.class), eq(harness.target), anyBoolean());
.onException(any(Exception.class), any(Number.class), eq(harness.target),
anyBoolean());
}
@Test
......@@ -483,9 +489,10 @@ public class GenericRequestTest {
request.onSizeReady(100, 100);
request.onSizeReady(100, 100);
verify(harness.engine, times(1)).load(eq(harness.signature), eq(100), eq(100), any(DataFetcher.class),
any(DataLoadProvider.class), any(Transformation.class), any(ResourceTranscoder.class),
any(Priority.class), anyBoolean(), any(DiskCacheStrategy.class), any(ResourceCallback.class));
verify(harness.engine, times(1)).load(eq(harness.signature), eq(100), eq(100),
any(DataFetcher.class), any(DataLoadProvider.class), any(Transformation.class),
any(ResourceTranscoder.class), any(Priority.class), anyBoolean(),
any(DiskCacheStrategy.class), any(ResourceCallback.class));
}
@Test
......@@ -506,8 +513,8 @@ public class GenericRequestTest {
request.onSizeReady(100, 100);
verify(harness.engine).load(any(Key.class), anyInt(), anyInt(), any(DataFetcher.class),
any(DataLoadProvider.class), any(Transformation.class), any(ResourceTranscoder.class),
eq(expected), anyBoolean(), any(DiskCacheStrategy.class), any(ResourceCallback.class));
any(DataLoadProvider.class), any(Transformation.class), any(ResourceTranscoder.class),
eq(expected), anyBoolean(), any(DiskCacheStrategy.class), any(ResourceCallback.class));
}
@Test
......@@ -602,7 +609,7 @@ public class GenericRequestTest {
}
@Test
public void setTestPlaceholderDrawableSetOnNullModel() {
public void testPlaceholderDrawableSetOnNullModelWithNoErrorDrawable() {
Drawable placeholder = new ColorDrawable(Color.RED);
MockTarget target = new MockTarget();
......@@ -618,7 +625,7 @@ public class GenericRequestTest {
}
@Test
public void testErrorDrawableSetOnNullModel() {
public void testErrorDrawableSetOnNullModelWithErrorDrawable() {
Drawable placeholder = new ColorDrawable(Color.RED);
Drawable errorPlaceholder = new ColorDrawable(Color.GREEN);
......@@ -635,6 +642,26 @@ public class GenericRequestTest {
assertEquals(errorPlaceholder, target.currentPlaceholder);
}
@Test
public void testFallbackDrawableSetOnNullModelWithErrorAndFallbackDrawables() {
Drawable placeholder = new ColorDrawable(Color.RED);
Drawable errorPlaceholder = new ColorDrawable(Color.GREEN);
Drawable fallback = new ColorDrawable(Color.BLUE);
MockTarget target = new MockTarget();
harness.placeholderDrawable = placeholder;
harness.errorDrawable = errorPlaceholder;
harness.fallbackDrawable = fallback;
harness.target = target;
harness.model = null;
GenericRequest request = harness.getRequest();
request.begin();
assertEquals(fallback, target.currentPlaceholder);
}
@Test
public void testIsNotRunningBeforeRunCalled() {
assertFalse(harness.getRequest().isRunning());
......
......@@ -369,6 +369,18 @@ public class BitmapRequestBuilder<ModelType, TranscodeType>
return this;
}
@Override
public BitmapRequestBuilder<ModelType, TranscodeType> fallback(Drawable drawable) {
super.fallback(drawable);
return this;
}
@Override
public BitmapRequestBuilder<ModelType, TranscodeType> fallback(int resourceId) {
super.fallback(resourceId);
return this;
}
/**
* {@inheritDoc}
*/
......
......@@ -329,6 +329,18 @@ public class DrawableRequestBuilder<ModelType>
return this;
}
@Override
public DrawableRequestBuilder<ModelType> fallback(Drawable drawable) {
super.fallback(drawable);
return this;
}
@Override
public DrawableRequestBuilder<ModelType> fallback(int resourceId) {
super.fallback(resourceId);
return this;
}
/**
* {@inheritDoc}
*/
......
......@@ -77,6 +77,8 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
private Transformation<ResourceType> transformation = UnitTransformation.get();
private boolean isTransformationSet;
private boolean isThumbnailBuilt;
private Drawable fallbackDrawable;
private int fallbackResource;
GenericRequestBuilder(LoadProvider<ModelType, DataType, ResourceType, TranscodeType> loadProvider,
Class<TranscodeType> transcodeClass, GenericRequestBuilder<ModelType, ?, ?, ?> other) {
......@@ -447,6 +449,49 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
return this;
}
/**
* Sets an {@link android.graphics.drawable.Drawable} to display if the model provided to
* {@link #load(Object)} is {@code null}.
*
* <p>
* If a fallback is not set, null models will cause the error drawable to be displayed. If
* the error drawable is not set, the placeholder will be displayed.
* </p>
*
* @see #placeholder(Drawable)
* @see #placeholder(int)
*
* @param drawable The drawable to display as a placeholder.
* @return This request builder.
*/
public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> fallback(
Drawable drawable) {
this.fallbackDrawable = drawable;
return this;
}
/**
* Sets a resource to display if the model provided to {@link #load(Object)} is {@code null}.
*
* <p>
* If a fallback is not set, null models will cause the error drawable to be displayed. If
* the error drawable is not set, the placeholder will be displayed.
* </p>
*
* @see #placeholder(Drawable)
* @see #placeholder(int)
*
* @param resourceId The id of the resource to use as a fallback.
* @return This request builder.
*/
public GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> fallback(
int resourceId) {
this.fallbackResource = resourceId;
return this;
}
/**
* Sets a resource to display if a load fails.
*
......@@ -802,6 +847,8 @@ public class GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeT
placeholderId,
errorPlaceholder,
errorId,
fallbackDrawable,
fallbackResource,
requestListener,
requestCoordinator,
glide.getEngine(),
......
......@@ -319,6 +319,18 @@ public class GifRequestBuilder<ModelType>
return this;
}
@Override
public GifRequestBuilder<ModelType> fallback(Drawable drawable) {
super.fallback(drawable);
return this;
}
@Override
public GifRequestBuilder<ModelType> fallback(int resourceId) {
super.fallback(resourceId);
return this;
}
/**
* {@inheritDoc}
*/
......
......@@ -59,6 +59,8 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
private final String tag = String.valueOf(hashCode());
private Key signature;
private Drawable fallbackDrawable;
private int fallbackResourceId;
private int placeholderResourceId;
private int errorResourceId;
private Context context;
......@@ -99,6 +101,8 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
int placeholderResourceId,
Drawable errorDrawable,
int errorResourceId,
Drawable fallbackDrawable,
int fallbackResourceId,
RequestListener<? super A, R> requestListener,
RequestCoordinator requestCoordinator,
Engine engine,
......@@ -125,6 +129,8 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
placeholderResourceId,
errorDrawable,
errorResourceId,
fallbackDrawable,
fallbackResourceId,
requestListener,
requestCoordinator,
engine,
......@@ -150,6 +156,7 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
target = null;
placeholderDrawable = null;
errorDrawable = null;
fallbackDrawable = null;
requestListener = null;
requestCoordinator = null;
transformation = null;
......@@ -171,6 +178,8 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
int placeholderResourceId,
Drawable errorDrawable,
int errorResourceId,
Drawable fallbackDrawable,
int fallbackResourceId,
RequestListener<? super A, R> requestListener,
RequestCoordinator requestCoordinator,
Engine engine,
......@@ -184,6 +193,8 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
this.loadProvider = loadProvider;
this.model = model;
this.signature = signature;
this.fallbackDrawable = fallbackDrawable;
this.fallbackResourceId = fallbackResourceId;
this.context = context.getApplicationContext();
this.priority = priority;
this.target = target;
......@@ -371,12 +382,22 @@ public final class GenericRequest<A, T, Z, R> implements Request, SizeReadyCallb
return status == Status.FAILED;
}
private Drawable getFallbackDrawable() {
if (fallbackDrawable == null && fallbackResourceId > 0) {
fallbackDrawable = context.getResources().getDrawable(fallbackResourceId);
}
return fallbackDrawable;
}
private void setErrorPlaceholder(Exception e) {
if (!canNotifyStatusChanged()) {
return;
}
Drawable error = getErrorDrawable();
Drawable error = model == null ? getFallbackDrawable() : null;
if (error == null) {
error = getErrorDrawable();
}
if (error == null) {
error = getPlaceholderDrawable();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册