AccountAdapter: fix fallback if cover could not be loaded
@@ -14,6 +14,9 @@ import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.model.stream.BaseGlideUrlLoader;
+import com.bumptech.glide.load.resource.drawable.GlideDrawable;
+import com.bumptech.glide.request.RequestListener;
+import com.bumptech.glide.request.target.Target;
import java.util.List;
@@ -123,16 +126,42 @@ public abstract class AccountAdapter<I extends AccountItem, VH extends AccountAd
ivCover.setVisibility(View.GONE);
} else {
if (item.getCover() != null) {
+ showCover(item);
+ } else {
+ showMediaTypeIcon(item);
+ }
+ }
+ }
+
+ private void showCover(I item) {
ivCover.setVisibility(View.VISIBLE);
ivMediaType.setVisibility(View.GONE);
- Drawable loading = VectorDrawableCompat.create(context.getResources(), R.drawable.ic_loading, null);
+ Drawable loading = VectorDrawableCompat
+ .create(context.getResources(), R.drawable.ic_loading, null);
Glide.with(context).using(new ISBNToolsUrlLoader(context))
.load(item.getCover())
.placeholder(loading)
.crossFade()
+ .listener(new RequestListener<String, GlideDrawable>() {
+ @Override
+ public boolean onException(Exception e, String model,
+ Target<GlideDrawable> target, boolean isFirstResource) {
+ showMediaTypeIcon(item);
+ return true;
+ }
+
+ @Override
+ public boolean onResourceReady(GlideDrawable resource, String model,
+ Target<GlideDrawable> target, boolean isFromMemoryCache,
+ boolean isFirstResource) {
+ return false;
+ }
+ })
.into(ivCover);
- } else {
+ }
+
+ private void showMediaTypeIcon(I item) {
ivCover.setVisibility(View.GONE);
Glide.clear(ivCover);
if (item.getMediaType() != null) {
@@ -145,8 +174,6 @@ public abstract class AccountAdapter<I extends AccountItem, VH extends AccountAd
ivMediaType.setVisibility(View.INVISIBLE);
}
}
- }
- }
public void setCoversHidden(boolean coversHidden) {
this.coversHidden = coversHidden;
