Encapsulated CoverTarget

This commit is contained in:
ByteHamster 2019-01-03 14:59:35 +01:00
parent f23682f538
commit c05f81a9f1
5 changed files with 142 additions and 93 deletions

View File

@ -20,8 +20,6 @@ import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.joanzapata.iconify.Iconify;
import java.lang.ref.WeakReference;
@ -30,7 +28,6 @@ import de.danoeh.antennapod.R;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
import de.danoeh.antennapod.core.storage.DownloadRequester;
import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.DateUtils;
@ -194,13 +191,12 @@ public class AllEpisodesRecycleAdapter extends RecyclerView.Adapter<AllEpisodesR
holder.butSecondary.setTag(item);
holder.butSecondary.setOnClickListener(secondaryActionListener);
Glide.with(mainActivityRef.get())
.load(item.getImageLocation())
.apply(new RequestOptions()
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate())
.into(new CoverTarget(item.getFeed().getImageLocation(), holder.placeholder, holder.cover, mainActivityRef.get()));
new CoverLoader(mainActivityRef.get())
.withUri(item.getImageLocation())
.withFallbackUri(item.getFeed().getImageLocation())
.withPlaceholderView(holder.placeholder)
.withCoverView(holder.cover)
.load();
}
@Nullable

View File

@ -0,0 +1,123 @@
package de.danoeh.antennapod.adapter;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import java.lang.ref.WeakReference;
import com.bumptech.glide.request.transition.Transition;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
public class CoverLoader {
private String uri;
private String fallbackUri;
private TextView txtvPlaceholder;
private ImageView imgvCover;
private MainActivity activity;
private int errorResource = -1;
public CoverLoader(MainActivity activity) {
this.activity = activity;
}
public CoverLoader withUri(String uri) {
this.uri = uri;
return this;
}
public CoverLoader withFallbackUri(String uri) {
fallbackUri = uri;
return this;
}
public CoverLoader withCoverView(ImageView coverView) {
imgvCover = coverView;
return this;
}
public CoverLoader withError(int errorResource) {
this.errorResource = errorResource;
return this;
}
public CoverLoader withPlaceholderView(TextView placeholderView) {
txtvPlaceholder = placeholderView;
return this;
}
public void load() {
RequestOptions options = new RequestOptions()
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate();
if (errorResource != -1) {
options = options.error(errorResource);
}
Glide.with(activity)
.load(uri)
.apply(options)
.into(new CoverTarget(fallbackUri, txtvPlaceholder, imgvCover, activity));
}
class CoverTarget extends CustomViewTarget<ImageView, Drawable> {
private final WeakReference<String> fallback;
private final WeakReference<TextView> placeholder;
private final WeakReference<ImageView> cover;
private final WeakReference<MainActivity> mainActivity;
public CoverTarget(String fallbackUri, TextView txtvPlaceholder, ImageView imgvCover, MainActivity activity) {
super(imgvCover);
fallback = new WeakReference<>(fallbackUri);
placeholder = new WeakReference<>(txtvPlaceholder);
cover = new WeakReference<>(imgvCover);
mainActivity = new WeakReference<>(activity);
}
@Override
public void onLoadFailed(Drawable errorDrawable) {
String fallbackUri = fallback.get();
TextView txtvPlaceholder = placeholder.get();
ImageView imgvCover = cover.get();
if (fallbackUri != null && txtvPlaceholder != null && imgvCover != null) {
MainActivity activity = mainActivity.get();
new Handler().post(() -> Glide.with(activity)
.load(fallbackUri)
.apply(new RequestOptions()
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate())
.into(new CoverTarget(null, txtvPlaceholder, imgvCover, activity)));
}
}
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
TextView txtvPlaceholder = placeholder.get();
if (txtvPlaceholder != null) {
txtvPlaceholder.setVisibility(View.INVISIBLE);
}
ImageView ivCover = cover.get();
ivCover.setImageDrawable(resource);
}
@Override
protected void onResourceCleared(@Nullable Drawable placeholder) {
ImageView ivCover = cover.get();
ivCover.setImageDrawable(placeholder);
}
}
}

View File

@ -1,68 +0,0 @@
package de.danoeh.antennapod.adapter;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.CustomViewTarget;
import java.lang.ref.WeakReference;
import com.bumptech.glide.request.transition.Transition;
import de.danoeh.antennapod.activity.MainActivity;
import de.danoeh.antennapod.core.glide.ApGlideSettings;
class CoverTarget extends CustomViewTarget<ImageView, Drawable> {
private final WeakReference<String> fallback;
private final WeakReference<TextView> placeholder;
private final WeakReference<ImageView> cover;
private final WeakReference<MainActivity> mainActivity;
public CoverTarget(String fallbackUri, TextView txtvPlaceholder, ImageView imgvCover, MainActivity activity) {
super(imgvCover);
fallback = new WeakReference<>(fallbackUri);
placeholder = new WeakReference<>(txtvPlaceholder);
cover = new WeakReference<>(imgvCover);
mainActivity = new WeakReference<>(activity);
}
@Override
public void onLoadFailed(Drawable errorDrawable) {
String fallbackUri = fallback.get();
TextView txtvPlaceholder = placeholder.get();
ImageView imgvCover = cover.get();
if (fallbackUri != null && txtvPlaceholder != null && imgvCover != null) {
MainActivity activity = mainActivity.get();
new Handler().post(() -> Glide.with(activity)
.load(fallbackUri)
.apply(new RequestOptions()
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate())
.into(new CoverTarget(null, txtvPlaceholder, imgvCover, activity)));
}
}
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
TextView txtvPlaceholder = placeholder.get();
if (txtvPlaceholder != null) {
txtvPlaceholder.setVisibility(View.INVISIBLE);
}
ImageView ivCover = cover.get();
ivCover.setImageDrawable(resource);
}
@Override
protected void onResourceCleared(@Nullable Drawable placeholder) {
ImageView ivCover = cover.get();
ivCover.setImageDrawable(placeholder);
}
}

View File

@ -292,13 +292,12 @@ public class QueueRecyclerAdapter extends RecyclerView.Adapter<QueueRecyclerAdap
butSecondary.setTag(item);
butSecondary.setOnClickListener(secondaryActionListener);
Glide.with(mainActivity.get())
.load(item.getImageLocation())
.apply(new RequestOptions()
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate())
.into(new CoverTarget(item.getFeed().getImageLocation(), placeholder, cover, mainActivity.get()));
new CoverLoader(mainActivity.get())
.withUri(item.getImageLocation())
.withFallbackUri(item.getFeed().getImageLocation())
.withPlaceholderView(placeholder)
.withCoverView(cover)
.load();
}
}

View File

@ -126,14 +126,13 @@ public class SubscriptionsAdapter extends BaseAdapter implements AdapterView.OnI
} else {
holder.count.setVisibility(View.GONE);
}
Glide.with(mainActivityRef.get())
.load(feed.getImageLocation())
.apply(new RequestOptions()
.error(R.color.light_gray)
.diskCacheStrategy(ApGlideSettings.AP_DISK_CACHE_STRATEGY)
.fitCenter()
.dontAnimate())
.into(new CoverTarget(null, holder.feedTitle, holder.imageView, mainActivityRef.get()));
new CoverLoader(mainActivityRef.get())
.withUri(feed.getImageLocation())
.withPlaceholderView(holder.feedTitle)
.withCoverView(holder.imageView)
.withError(R.color.light_gray)
.load();
return convertView;
}