diff options
Diffstat (limited to 'app/src/main/java/ml')
2 files changed, 259 insertions, 182 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java index 45e3495e..ce1c32fa 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/CommentAndPostRecyclerViewAdapter.java @@ -47,7 +47,6 @@ import com.bumptech.glide.request.target.Target; import com.bumptech.glide.request.transition.Transition; import com.davemorrissey.labs.subscaleview.ImageSource; import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; -import com.github.piasy.biv.loader.ImageLoader; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.text.Cue; @@ -59,7 +58,6 @@ import com.lsjwzh.widget.materialloadingprogressbar.CircleProgressBar; import org.commonmark.ext.gfm.tables.TableBlock; -import java.io.File; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -103,7 +101,6 @@ import ml.docilealligator.infinityforreddit.BottomSheetFragment.ShareLinkBottomS import ml.docilealligator.infinityforreddit.Comment.Comment; import ml.docilealligator.infinityforreddit.Comment.FetchComment; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; -import ml.docilealligator.infinityforreddit.CustomView.AspectRatioBigImageView; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioSubsamplingScaleImageView; import ml.docilealligator.infinityforreddit.CustomView.MarkwonLinearLayoutManager; @@ -117,6 +114,7 @@ import ml.docilealligator.infinityforreddit.Utils.GlideImageGetter; import ml.docilealligator.infinityforreddit.Utils.SharedPreferencesUtils; import ml.docilealligator.infinityforreddit.Utils.Utils; import ml.docilealligator.infinityforreddit.VoteThing; +import pl.droidsonroids.gif.GifImageView; import retrofit2.Retrofit; import static ml.docilealligator.infinityforreddit.Activity.CommentActivity.WRITE_COMMENT_REQUEST_CODE; @@ -713,17 +711,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy if (holder instanceof PostDetailVideoAutoplayViewHolder) { ((PostDetailVideoAutoplayViewHolder) holder).aspectRatioFrameLayout.setAspectRatio((float) mPost.getPreviewWidth() / mPost.getPreviewHeight()); ((PostDetailVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.VISIBLE); - mGlide.asBitmap().load(mPost.getPreviewUrl()).into(new CustomTarget<Bitmap>() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { - ((PostDetailVideoAutoplayViewHolder) holder).previewImageView.setImage(ImageSource.bitmap(resource)); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }); + mGlide.load(mPost.getPreviewUrl()).into(((PostDetailVideoAutoplayViewHolder) holder).previewImageView); ((PostDetailVideoAutoplayViewHolder) holder).setVolume(mMuteAutoplayingVideos || (mPost.isNSFW() && mMuteNSFWVideo) ? 0f : 1f); ((PostDetailVideoAutoplayViewHolder) holder).bindVideoUri(Uri.parse(mPost.getVideoUrl())); } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { @@ -1235,49 +1223,26 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy private void loadImage(PostDetailBaseViewHolder holder) { if (holder instanceof PostDetailGifAutoplayViewHolder) { - ((PostDetailGifAutoplayViewHolder) holder).mImageView.setImageLoaderCallback(new ImageLoader.Callback() { - @Override - public void onCacheHit(int imageType, File image) { - - } - - @Override - public void onCacheMiss(int imageType, File image) { - - } - - @Override - public void onStart() { - - } - - @Override - public void onProgress(int progress) { - - } - - @Override - public void onFinish() { - - } - - @Override - public void onSuccess(File image) { - ((PostDetailGifAutoplayViewHolder) holder).mLoadWrapper.setVisibility(View.GONE); - } + mGlide.asBitmap().load(mPost.getPreviewUrl()) + .listener(new RequestListener<Bitmap>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { + ((PostDetailGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); + ((PostDetailGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.VISIBLE); + ((PostDetailGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { + ((PostDetailGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); + ((PostDetailGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); + loadImage(holder); + }); + return false; + } - @Override - public void onFail(Exception error) { - ((PostDetailGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); - ((PostDetailGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.VISIBLE); - ((PostDetailGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { - ((PostDetailGifAutoplayViewHolder) holder).mLoadImageProgressBar.setVisibility(View.VISIBLE); - ((PostDetailGifAutoplayViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.GONE); - loadImage(holder); - }); - } - }); - ((PostDetailGifAutoplayViewHolder) holder).mImageView.showImage(Uri.parse(mPost.getUrl())); + @Override + public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { + ((PostDetailGifAutoplayViewHolder) holder).mLoadWrapper.setVisibility(View.GONE); + return false; + } + }).into(((PostDetailGifAutoplayViewHolder) holder).mImageView); } else if (holder instanceof PostDetailImageViewHolder) { RequestBuilder<Bitmap> imageRequestBuilder = mGlide.asBitmap().load(mPost.getPreviewUrl()) .listener(new RequestListener<Bitmap>() { @@ -1318,10 +1283,10 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy imageRequestBuilder.apply(RequestOptions.noTransformation()).into(customTarget); } } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { - RequestBuilder<Bitmap> imageRequestBuilder = mGlide.asBitmap().load(mPost.getPreviewUrl()) - .listener(new RequestListener<Bitmap>() { + RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + .listener(new RequestListener<Drawable>() { @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setVisibility(View.VISIBLE); ((PostDetailVideoAndGifPreviewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { @@ -1333,35 +1298,23 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy } @Override - public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { + public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { ((PostDetailVideoAndGifPreviewHolder) holder).mLoadWrapper.setVisibility(View.GONE); return false; } }); - CustomTarget<Bitmap> customTarget = new CustomTarget<Bitmap>() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { - ((PostDetailVideoAndGifPreviewHolder) holder).mImageView.setImage(ImageSource.bitmap(resource)); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }; - if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(customTarget); + .into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); } else { - imageRequestBuilder.apply(RequestOptions.noTransformation()).into(customTarget); + imageRequestBuilder.apply(RequestOptions.noTransformation()).into(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); } } else if (holder instanceof PostDetailLinkViewHolder) { - RequestBuilder<Bitmap> imageRequestBuilder = mGlide.asBitmap().load(mPost.getPreviewUrl()) - .listener(new RequestListener<Bitmap>() { + RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + .listener(new RequestListener<Drawable>() { @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { ((PostDetailLinkViewHolder) holder).mLoadImageProgressBar.setVisibility(View.GONE); ((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setVisibility(View.VISIBLE); ((PostDetailLinkViewHolder) holder).mLoadImageErrorTextView.setOnClickListener(view -> { @@ -1373,28 +1326,17 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy } @Override - public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { + public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { ((PostDetailLinkViewHolder) holder).mLoadWrapper.setVisibility(View.GONE); return false; } }); - CustomTarget<Bitmap> customTarget = new CustomTarget<Bitmap>() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { - ((PostDetailLinkViewHolder) holder).mImageView.setImage(ImageSource.bitmap(resource)); - } - - @Override - public void onLoadCleared(@Nullable Drawable placeholder) { - - } - }; - if ((mPost.isNSFW() && mNeedBlurNsfw) || (mPost.isSpoiler() && mNeedBlurSpoiler)) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(customTarget); + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) + .into(((PostDetailLinkViewHolder) holder).mImageView); } else { - imageRequestBuilder.into(customTarget); + imageRequestBuilder.into(((PostDetailLinkViewHolder) holder).mImageView); } } } @@ -1961,6 +1903,8 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy ((PostDetailVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.GONE); } else if (holder instanceof PostDetailVideoAndGifPreviewHolder) { mGlide.clear(((PostDetailVideoAndGifPreviewHolder) holder).mImageView); + } else if (holder instanceof PostDetailGifAutoplayViewHolder) { + mGlide.clear(((PostDetailGifAutoplayViewHolder) holder).mImageView); } else if (holder instanceof PostDetailImageViewHolder) { mGlide.clear(((PostDetailImageViewHolder) holder).mImageView); } else if (holder instanceof PostDetailLinkViewHolder) { @@ -2459,7 +2403,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy @BindView(R.id.player_view_item_post_detail_video_autoplay) PlayerView playerView; @BindView(R.id.preview_image_view_item_post_detail_video_autoplay) - SubsamplingScaleImageView previewImageView; + GifImageView previewImageView; @BindView(R.id.mute_exo_playback_control_view) ImageView muteButton; @BindView(R.id.fullscreen_exo_playback_control_view) @@ -2678,7 +2622,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy @BindView(R.id.load_image_error_text_view_item_post_detail_video_and_gif_preview) TextView mLoadImageErrorTextView; @BindView(R.id.image_view_item_post_detail_video_and_gif_preview) - AspectRatioSubsamplingScaleImageView mImageView; + AspectRatioGifImageView mImageView; @BindView(R.id.bottom_constraint_layout_item_post_detail_video_and_gif_preview) ConstraintLayout mBottomConstraintLayout; @BindView(R.id.plus_button_item_post_detail_video_and_gif_preview) @@ -2782,7 +2726,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy @BindView(R.id.load_image_error_text_view_item_post_detail_gif_autoplay) TextView mLoadImageErrorTextView; @BindView(R.id.image_view_item_post_detail_gif_autoplay) - AspectRatioBigImageView mImageView; + AspectRatioGifImageView mImageView; @BindView(R.id.bottom_constraint_layout_item_post_detail_gif_autoplay) ConstraintLayout mBottomConstraintLayout; @BindView(R.id.plus_button_item_post_detail_gif_autoplay) @@ -2988,7 +2932,7 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter<Recy @BindView(R.id.load_image_error_text_view_item_post_detail_link) TextView mLoadImageErrorTextView; @BindView(R.id.image_view_item_post_detail_link) - AspectRatioSubsamplingScaleImageView mImageView; + AspectRatioGifImageView mImageView; @BindView(R.id.bottom_constraint_layout_item_post_detail_link) ConstraintLayout mBottomConstraintLayout; @BindView(R.id.plus_button_item_post_detail_link) diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java index 5f2c2605..c500366f 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Adapter/PostRecyclerViewAdapter.java @@ -5,6 +5,7 @@ import android.content.SharedPreferences; import android.content.res.ColorStateList; import android.content.res.Configuration; import android.content.res.Resources; +import android.graphics.Bitmap; import android.graphics.ColorFilter; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; @@ -39,7 +40,11 @@ import com.bumptech.glide.load.DataSource; import com.bumptech.glide.load.engine.GlideException; import com.bumptech.glide.request.RequestListener; import com.bumptech.glide.request.RequestOptions; +import com.bumptech.glide.request.target.CustomTarget; import com.bumptech.glide.request.target.Target; +import com.bumptech.glide.request.transition.Transition; +import com.davemorrissey.labs.subscaleview.ImageSource; +import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView; import com.google.android.exoplayer2.metadata.Metadata; import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.text.Cue; @@ -78,6 +83,7 @@ import ml.docilealligator.infinityforreddit.AsyncTask.LoadUserDataAsyncTask; import ml.docilealligator.infinityforreddit.BottomSheetFragment.ShareLinkBottomSheetFragment; import ml.docilealligator.infinityforreddit.CustomTheme.CustomThemeWrapper; import ml.docilealligator.infinityforreddit.CustomView.AspectRatioGifImageView; +import ml.docilealligator.infinityforreddit.CustomView.AspectRatioSubsamplingScaleImageView; import ml.docilealligator.infinityforreddit.Event.PostUpdateEventToDetailActivity; import ml.docilealligator.infinityforreddit.NetworkState; import ml.docilealligator.infinityforreddit.Post.Post; @@ -98,15 +104,16 @@ import retrofit2.Retrofit; */ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView.ViewHolder> implements CacheManager { - private static final int VIEW_TYPE_POST_CARD_VIDEO_TYPE_AUTOPLAY = 1; + private static final int VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE = 1; private static final int VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE = 2; - private static final int VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE = 3; - private static final int VIEW_TYPE_POST_CARD_LINK_TYPE = 4; - private static final int VIEW_TYPE_POST_CARD_NO_PREVIEW_LINK_TYPE = 5; - private static final int VIEW_TYPE_POST_CARD_TEXT_TYPE = 6; - private static final int VIEW_TYPE_POST_COMPACT = 7; - private static final int VIEW_TYPE_ERROR = 8; - private static final int VIEW_TYPE_LOADING = 9; + private static final int VIEW_TYPE_POST_CARD_GIF_AUTOPLAY_TYPE = 3; + private static final int VIEW_TYPE_POST_CARD_IMAGE_TYPE = 4; + private static final int VIEW_TYPE_POST_CARD_LINK_TYPE = 5; + private static final int VIEW_TYPE_POST_CARD_NO_PREVIEW_LINK_TYPE = 6; + private static final int VIEW_TYPE_POST_CARD_TEXT_TYPE = 7; + private static final int VIEW_TYPE_POST_COMPACT = 8; + private static final int VIEW_TYPE_ERROR = 9; + private static final int VIEW_TYPE_LOADING = 10; private static final DiffUtil.ItemCallback<Post> DIFF_CALLBACK = new DiffUtil.ItemCallback<Post>() { @Override public boolean areItemsTheSame(@NonNull Post post, @NonNull Post t1) { @@ -291,7 +298,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (!mAutoplayNsfwVideos && post.isNSFW()) { return VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE; } - return VIEW_TYPE_POST_CARD_VIDEO_TYPE_AUTOPLAY; + return VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE; } return VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE; case Post.GIF_TYPE: @@ -299,11 +306,11 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView if (!mAutoplayNsfwVideos && post.isNSFW()) { return VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE; } - return VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE; + return VIEW_TYPE_POST_CARD_GIF_AUTOPLAY_TYPE; } return VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE; case Post.IMAGE_TYPE: - return VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE; + return VIEW_TYPE_POST_CARD_IMAGE_TYPE; case Post.LINK_TYPE: return VIEW_TYPE_POST_CARD_LINK_TYPE; case Post.NO_PREVIEW_LINK_TYPE: @@ -322,12 +329,14 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - if (viewType == VIEW_TYPE_POST_CARD_VIDEO_TYPE_AUTOPLAY) { + if (viewType == VIEW_TYPE_POST_CARD_VIDEO_AUTOPLAY_TYPE) { return new PostVideoAutoplayViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_video_type_autoplay, parent, false)); } else if (viewType == VIEW_TYPE_POST_CARD_VIDEO_AND_GIF_PREVIEW_TYPE) { return new PostVideoAndGifPreviewViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_video_and_gif_preview, parent, false)); - } else if (viewType == VIEW_TYPE_POST_CARD_IMAGE_AND_GIF_AUTOPLAY_TYPE) { - return new PostImageAndGifAutoplayViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_image_and_gif_autoplay, parent, false)); + } else if (viewType == VIEW_TYPE_POST_CARD_GIF_AUTOPLAY_TYPE) { + return new PostGifAutoplayViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_gif_autoplay, parent, false)); + } else if (viewType == VIEW_TYPE_POST_CARD_IMAGE_TYPE) { + return new PostImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_image, parent, false)); } else if (viewType == VIEW_TYPE_POST_CARD_LINK_TYPE) { return new PostLinkTypeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_post_link, parent, false)); } else if (viewType == VIEW_TYPE_POST_CARD_NO_PREVIEW_LINK_TYPE) { @@ -559,20 +568,20 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostVideoAndGifPreviewViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); ((PostVideoAndGifPreviewViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); } - } else if (holder instanceof PostImageAndGifAutoplayViewHolder) { - if (post.getPostType() == Post.GIF_TYPE) { - ((PostImageAndGifAutoplayViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.gif)); - } else { - ((PostImageAndGifAutoplayViewHolder) holder).typeTextView.setText(mActivity.getString(R.string.image)); - } - ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostImageAndGifAutoplayViewHolder) holder).imageView + } else if (holder instanceof PostGifAutoplayViewHolder) { + ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostGifAutoplayViewHolder) holder).imageView + .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); + loadImage(holder, post); + } else if (holder instanceof PostImageViewHolder) { + ((PostImageViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageViewHolder) holder).imageView .setRatio((float) post.getPreviewHeight() / post.getPreviewWidth()); loadImage(holder, post); if (post.getPreviewWidth() <= 0 || post.getPreviewHeight() <= 0) { - ((PostImageAndGifAutoplayViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); - ((PostImageAndGifAutoplayViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); + ((PostImageViewHolder) holder).imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP); + ((PostImageViewHolder) holder).imageView.getLayoutParams().height = (int) (400 * mScale); } } else if (holder instanceof PostLinkTypeViewHolder) { ((PostLinkTypeViewHolder) holder).progressBar.setVisibility(View.VISIBLE); @@ -843,16 +852,15 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } private void loadImage(final RecyclerView.ViewHolder holder, final Post post) { - if (holder instanceof PostImageAndGifAutoplayViewHolder) { - String url = mAutoplay && post.getPostType() == Post.GIF_TYPE ? post.getUrl() : post.getPreviewUrl(); - RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(url).listener(new RequestListener<Drawable>() { + if (holder instanceof PostGifAutoplayViewHolder) { + mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) { - ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); - ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); - ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { - ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); - ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); loadImage(holder, post); }); return false; @@ -860,17 +868,49 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView @Override public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) { - ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); - ((PostImageAndGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostGifAutoplayViewHolder) holder).progressBar.setVisibility(View.GONE); + return false; + } + }).into(((PostGifAutoplayViewHolder) holder).imageView); + } else if (holder instanceof PostImageViewHolder) { + RequestBuilder<Bitmap> imageRequestBuilder = mGlide.asBitmap().load(post.getPreviewUrl()).listener(new RequestListener<Bitmap>() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) { + ((PostImageViewHolder) holder).progressBar.setVisibility(View.GONE); + ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.VISIBLE); + ((PostImageViewHolder) holder).errorRelativeLayout.setOnClickListener(view -> { + ((PostImageViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + loadImage(holder, post); + }); + return false; + } + + @Override + public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) { + ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); + CustomTarget<Bitmap> customTarget = new CustomTarget<Bitmap>() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { + ((PostImageViewHolder) holder).imageView.setImage(ImageSource.cachedBitmap(resource)); + } + + @Override + public void onLoadCleared(@Nullable Drawable placeholder) { + + } + }; + if ((post.isNSFW() && mNeedBlurNSFW) || post.isSpoiler() && mNeedBlurSpoiler) { - imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))) - .into(((PostImageAndGifAutoplayViewHolder) holder).imageView); + imageRequestBuilder.apply(RequestOptions.bitmapTransform(new BlurTransformation(50, 10))).into(customTarget); } else { - imageRequestBuilder.apply(RequestOptions.noTransformation()).into(((PostImageAndGifAutoplayViewHolder) holder).imageView); + imageRequestBuilder.apply(RequestOptions.noTransformation()).into(customTarget); } } else if (holder instanceof PostVideoAndGifPreviewViewHolder) { RequestBuilder<Drawable> imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener<Drawable>() { @@ -1079,14 +1119,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView ((PostVideoAutoplayViewHolder) holder).resetVolume(); mGlide.clear(((PostVideoAutoplayViewHolder) holder).previewImageView); ((PostVideoAutoplayViewHolder) holder).previewImageView.setVisibility(View.GONE); - } else if (holder instanceof PostImageAndGifAutoplayViewHolder) { - mGlide.clear(((PostImageAndGifAutoplayViewHolder) holder).imageView); - ((PostImageAndGifAutoplayViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.FIT_START); - ((PostImageAndGifAutoplayViewHolder) holder).imageView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; - ((PostImageAndGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + } else if (holder instanceof PostGifAutoplayViewHolder) { + mGlide.clear(((PostGifAutoplayViewHolder) holder).imageView); + ((PostGifAutoplayViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + } else if (holder instanceof PostImageViewHolder) { + mGlide.clear(((PostImageViewHolder) holder).imageView); + ((PostImageViewHolder) holder).imageView.recycle(); + ((PostImageViewHolder) holder).imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_START); + ((PostImageViewHolder) holder).imageView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; + ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + } else if (holder instanceof PostVideoAndGifPreviewViewHolder) { + mGlide.clear(((PostVideoAndGifPreviewViewHolder) holder).imageView); } else if (holder instanceof PostLinkTypeViewHolder) { mGlide.clear(((PostLinkTypeViewHolder) holder).imageView); - ((PostLinkTypeViewHolder) holder).imageView.setScaleType(ImageView.ScaleType.FIT_START); ((PostLinkTypeViewHolder) holder).imageView.getLayoutParams().height = FrameLayout.LayoutParams.WRAP_CONTENT; ((PostLinkTypeViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); } else if (holder instanceof PostTextTypeViewHolder) { @@ -1910,61 +1955,61 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView } } - class PostImageAndGifAutoplayViewHolder extends PostBaseViewHolder { - @BindView(R.id.card_view_item_post_image_and_gif_autoplay) + class PostGifAutoplayViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_gif_autoplay) MaterialCardView cardView; - @BindView(R.id.icon_gif_image_view_item_post_image_and_gif_autoplay) + @BindView(R.id.icon_gif_image_view_item_post_gif_autoplay) AspectRatioGifImageView iconGifImageView; - @BindView(R.id.subreddit_name_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.subreddit_name_text_view_item_post_gif_autoplay) TextView subredditTextView; - @BindView(R.id.user_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.user_text_view_item_post_gif_autoplay) TextView userTextView; - @BindView(R.id.stickied_post_image_view_item_post_image_and_gif_autoplay) + @BindView(R.id.stickied_post_image_view_item_post_gif_autoplay) ImageView stickiedPostImageView; - @BindView(R.id.post_time_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.post_time_text_view_item_post_gif_autoplay) TextView postTimeTextView; - @BindView(R.id.title_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.title_text_view_item_post_gif_autoplay) TextView titleTextView; - @BindView(R.id.type_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.type_text_view_item_post_gif_autoplay) CustomTextView typeTextView; - @BindView(R.id.archived_image_view_item_post_image_and_gif_autoplay) + @BindView(R.id.archived_image_view_item_post_gif_autoplay) ImageView archivedImageView; - @BindView(R.id.locked_image_view_item_post_image_and_gif_autoplay) + @BindView(R.id.locked_image_view_item_post_gif_autoplay) ImageView lockedImageView; - @BindView(R.id.crosspost_image_view_item_post_image_and_gif_autoplay) + @BindView(R.id.crosspost_image_view_item_post_gif_autoplay) ImageView crosspostImageView; - @BindView(R.id.nsfw_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.nsfw_text_view_item_post_gif_autoplay) CustomTextView nsfwTextView; - @BindView(R.id.spoiler_custom_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.spoiler_custom_text_view_item_post_gif_autoplay) CustomTextView spoilerTextView; - @BindView(R.id.flair_custom_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.flair_custom_text_view_item_post_gif_autoplay) CustomTextView flairTextView; - @BindView(R.id.awards_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.awards_text_view_item_post_gif_autoplay) CustomTextView awardsTextView; - @BindView(R.id.progress_bar_item_post_image_and_gif_autoplay) + @BindView(R.id.progress_bar_item_post_gif_autoplay) ProgressBar progressBar; - @BindView(R.id.image_view_item_post_image_and_gif_autoplay) + @BindView(R.id.image_view_item_post_gif_autoplay) AspectRatioGifImageView imageView; - @BindView(R.id.load_image_error_relative_layout_item_post_image_and_gif_autoplay) + @BindView(R.id.load_image_error_relative_layout_item_post_gif_autoplay) RelativeLayout errorRelativeLayout; - @BindView(R.id.load_image_error_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.load_image_error_text_view_item_post_gif_autoplay) TextView errorTextView; - @BindView(R.id.bottom_constraint_layout_item_post_image_and_gif_autoplay) + @BindView(R.id.bottom_constraint_layout_item_post_gif_autoplay) ConstraintLayout bottomConstraintLayout; - @BindView(R.id.plus_button_item_post_image_and_gif_autoplay) + @BindView(R.id.plus_button_item_post_gif_autoplay) ImageView upvoteButton; - @BindView(R.id.score_text_view_item_post_image_and_gif_autoplay) + @BindView(R.id.score_text_view_item_post_gif_autoplay) TextView scoreTextView; - @BindView(R.id.minus_button_item_post_image_and_gif_autoplay) + @BindView(R.id.minus_button_item_post_gif_autoplay) ImageView downvoteButton; - @BindView(R.id.comments_count_item_post_image_and_gif_autoplay) + @BindView(R.id.comments_count_item_post_gif_autoplay) TextView commentsCountTextView; - @BindView(R.id.save_button_item_post_image_and_gif_autoplay) + @BindView(R.id.save_button_item_post_gif_autoplay) ImageView saveButton; - @BindView(R.id.share_button_item_post_image_and_gif_autoplay) + @BindView(R.id.share_button_item_post_gif_autoplay) ImageView shareButton; - PostImageAndGifAutoplayViewHolder(View itemView) { + PostGifAutoplayViewHolder(View itemView) { super(itemView); ButterKnife.bind(this, itemView); setBaseView(cardView, @@ -1996,21 +2041,109 @@ public class PostRecyclerViewAdapter extends PagedListAdapter<Post, RecyclerView imageView.setOnClickListener(view -> { Post post = getItem(getAdapterPosition()); if (post != null) { - if (post.getPostType() == Post.IMAGE_TYPE) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, post.getUrl()); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() - + "-" + post.getId() + ".jpg"); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); - } else if (post.getPostType() == Post.GIF_TYPE) { - Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); - intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() - + "-" + post.getId() + ".gif"); - intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); - intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); - mActivity.startActivity(intent); - } + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".gif"); + intent.putExtra(ViewImageOrGifActivity.GIF_URL_KEY, post.getVideoUrl()); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); + } + }); + } + } + + class PostImageViewHolder extends PostBaseViewHolder { + @BindView(R.id.card_view_item_post_image) + MaterialCardView cardView; + @BindView(R.id.icon_gif_image_view_item_post_image) + AspectRatioGifImageView iconGifImageView; + @BindView(R.id.subreddit_name_text_view_item_post_image) + TextView subredditTextView; + @BindView(R.id.user_text_view_item_post_image) + TextView userTextView; + @BindView(R.id.stickied_post_image_view_item_post_image) + ImageView stickiedPostImageView; + @BindView(R.id.post_time_text_view_item_post_image) + TextView postTimeTextView; + @BindView(R.id.title_text_view_item_post_image) + TextView titleTextView; + @BindView(R.id.type_text_view_item_post_image) + CustomTextView typeTextView; + @BindView(R.id.archived_image_view_item_post_image) + ImageView archivedImageView; + @BindView(R.id.locked_image_view_item_post_image) + ImageView lockedImageView; + @BindView(R.id.crosspost_image_view_item_post_image) + ImageView crosspostImageView; + @BindView(R.id.nsfw_text_view_item_post_image) + CustomTextView nsfwTextView; + @BindView(R.id.spoiler_custom_text_view_item_post_image) + CustomTextView spoilerTextView; + @BindView(R.id.flair_custom_text_view_item_post_image) + CustomTextView flairTextView; + @BindView(R.id.awards_text_view_item_post_image) + CustomTextView awardsTextView; + @BindView(R.id.progress_bar_item_post_image) + ProgressBar progressBar; + @BindView(R.id.image_view_item_post_image) + AspectRatioSubsamplingScaleImageView imageView; + @BindView(R.id.load_image_error_relative_layout_item_post_image) + RelativeLayout errorRelativeLayout; + @BindView(R.id.load_image_error_text_view_item_post_image) + TextView errorTextView; + @BindView(R.id.bottom_constraint_layout_item_post_image) + ConstraintLayout bottomConstraintLayout; + @BindView(R.id.plus_button_item_post_image) + ImageView upvoteButton; + @BindView(R.id.score_text_view_item_post_image) + TextView scoreTextView; + @BindView(R.id.minus_button_item_post_image) + ImageView downvoteButton; + @BindView(R.id.comments_count_item_post_image) + TextView commentsCountTextView; + @BindView(R.id.save_button_item_post_image) + ImageView saveButton; + @BindView(R.id.share_button_item_post_image) + ImageView shareButton; + + PostImageViewHolder(View itemView) { + super(itemView); + ButterKnife.bind(this, itemView); + setBaseView(cardView, + iconGifImageView, + subredditTextView, + userTextView, + stickiedPostImageView, + postTimeTextView, + titleTextView, + typeTextView, + archivedImageView, + lockedImageView, + crosspostImageView, + nsfwTextView, + spoilerTextView, + flairTextView, + awardsTextView, + bottomConstraintLayout, + upvoteButton, + scoreTextView, + downvoteButton, + commentsCountTextView, + saveButton, + shareButton); + + progressBar.setIndeterminateTintList(ColorStateList.valueOf(mColorAccent)); + errorTextView.setTextColor(mPrimaryTextColor); + + imageView.setOnClickListener(view -> { + Post post = getItem(getAdapterPosition()); + if (post != null) { + Intent intent = new Intent(mActivity, ViewImageOrGifActivity.class); + intent.putExtra(ViewImageOrGifActivity.IMAGE_URL_KEY, post.getUrl()); + intent.putExtra(ViewImageOrGifActivity.FILE_NAME_KEY, post.getSubredditName() + + "-" + post.getId() + ".jpg"); + intent.putExtra(ViewImageOrGifActivity.POST_TITLE_KEY, post.getTitle()); + mActivity.startActivity(intent); } }); } |