From 83eea69459eb56c964f25b5565766108d8fc25d6 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Mon, 13 Jul 2020 00:24:25 +0800 Subject: Only use SubsamplingScaleImageView to load images in ViewHolders for image in PostRecyclerViewAdapter and CommentAndPostRecyclerViewAdapter. --- .../Adapter/CommentAndPostRecyclerViewAdapter.java | 136 +++------ .../Adapter/PostRecyclerViewAdapter.java | 305 +++++++++++++++------ 2 files changed, 259 insertions(+), 182 deletions(-) (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit') 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() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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 target, DataSource dataSource, boolean isFirstResource) { + ((PostDetailGifAutoplayViewHolder) holder).mLoadWrapper.setVisibility(View.GONE); + return false; + } + }).into(((PostDetailGifAutoplayViewHolder) holder).mImageView); } else if (holder instanceof PostDetailImageViewHolder) { RequestBuilder imageRequestBuilder = mGlide.asBitmap().load(mPost.getPreviewUrl()) .listener(new RequestListener() { @@ -1318,10 +1283,10 @@ public class CommentAndPostRecyclerViewAdapter extends RecyclerView.Adapter imageRequestBuilder = mGlide.asBitmap().load(mPost.getPreviewUrl()) - .listener(new RequestListener() { + RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + .listener(new RequestListener() { @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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 target, DataSource dataSource, boolean isFirstResource) { + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { ((PostDetailVideoAndGifPreviewHolder) holder).mLoadWrapper.setVisibility(View.GONE); return false; } }); - CustomTarget customTarget = new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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 imageRequestBuilder = mGlide.asBitmap().load(mPost.getPreviewUrl()) - .listener(new RequestListener() { + RequestBuilder imageRequestBuilder = mGlide.load(mPost.getPreviewUrl()) + .listener(new RequestListener() { @Override - public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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 target, DataSource dataSource, boolean isFirstResource) { + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { ((PostDetailLinkViewHolder) holder).mLoadWrapper.setVisibility(View.GONE); return false; } }); - CustomTarget customTarget = new CustomTarget() { - @Override - public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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 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 DIFF_CALLBACK = new DiffUtil.ItemCallback() { @Override public boolean areItemsTheSame(@NonNull Post post, @NonNull Post t1) { @@ -291,7 +298,7 @@ public class PostRecyclerViewAdapter extends PagedListAdapter imageRequestBuilder = mGlide.load(url).listener(new RequestListener() { + if (holder instanceof PostGifAutoplayViewHolder) { + mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @Override public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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 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 imageRequestBuilder = mGlide.asBitmap().load(post.getPreviewUrl()).listener(new RequestListener() { + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target 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 target, DataSource dataSource, boolean isFirstResource) { + ((PostImageViewHolder) holder).errorRelativeLayout.setVisibility(View.GONE); + ((PostImageViewHolder) holder).progressBar.setVisibility(View.GONE); return false; } }); + CustomTarget customTarget = new CustomTarget() { + @Override + public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition 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 imageRequestBuilder = mGlide.load(post.getPreviewUrl()).listener(new RequestListener() { @@ -1079,14 +1119,19 @@ public class PostRecyclerViewAdapter extends PagedListAdapter { 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); } }); } -- cgit v1.2.3