From 48a65a4b2fba95a1aa76250afefefdc9829fa653 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 15 Jul 2021 21:51:41 +0800 Subject: Implement RedditGallerySubmissionRecyclerViewAdapter. --- .../activities/PostGalleryActivity.java | 3 - ...RedditGallerySubmissionRecyclerViewAdapter.java | 162 +++++++++++++++++++++ .../item_reddit_gallery_submission_add_image.xml | 5 + .../item_reddit_gallery_submission_image.xml | 27 ++++ 4 files changed, 194 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java create mode 100644 app/src/main/res/layout/item_reddit_gallery_submission_add_image.xml create mode 100644 app/src/main/res/layout/item_reddit_gallery_submission_image.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java index 83118caf..dd340e68 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java @@ -497,9 +497,6 @@ public class PostGalleryActivity extends BaseActivity implements FlairBottomShee outState.putString(SUBREDDIT_ICON_STATE, iconUrl); outState.putBoolean(SUBREDDIT_SELECTED_STATE, subredditSelected); outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); - if (imageUri != null) { - outState.putString(IMAGE_URI_STATE, imageUri.toString()); - } outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); outState.putBoolean(IS_POSTING_STATE, isPosting); outState.putParcelable(FLAIR_STATE, flair); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java new file mode 100644 index 00000000..c80a8113 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java @@ -0,0 +1,162 @@ +package ml.docilealligator.infinityforreddit.adapters; + +import android.content.Context; +import android.content.res.ColorStateList; +import android.graphics.drawable.Drawable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageView; +import android.widget.ProgressBar; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; +import androidx.recyclerview.widget.RecyclerView; + +import com.bumptech.glide.Glide; +import com.bumptech.glide.RequestManager; +import com.bumptech.glide.load.DataSource; +import com.bumptech.glide.load.engine.GlideException; +import com.bumptech.glide.request.RequestListener; +import com.bumptech.glide.request.target.Target; +import com.google.android.material.floatingactionbutton.FloatingActionButton; + +import java.util.List; + +import butterknife.BindView; +import butterknife.ButterKnife; +import ml.docilealligator.infinityforreddit.R; +import ml.docilealligator.infinityforreddit.RedditGalleryPayload; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; +import ml.docilealligator.infinityforreddit.customviews.AspectRatioGifImageView; + +public class RedditGallerySubmissionRecyclerViewAdapter extends RecyclerView.Adapter { + + private static final int VIEW_TYPE_IMAGE = 1; + private static final int VIEW_TYPE_ADD_IMAGE = 2; + + private List redditGalleryImageInfoList; + private CustomThemeWrapper customThemeWrapper; + private ItemClickListener itemClickListener; + private RequestManager glide; + + public RedditGallerySubmissionRecyclerViewAdapter(Context context, CustomThemeWrapper customThemeWrapper, + ItemClickListener itemClickListener) { + glide = Glide.with(context); + this.customThemeWrapper = customThemeWrapper; + this.itemClickListener = itemClickListener; + } + + @Override + public int getItemViewType(int position) { + if (redditGalleryImageInfoList == null || position >= redditGalleryImageInfoList.size()) { + return VIEW_TYPE_ADD_IMAGE; + } + + return VIEW_TYPE_IMAGE; + } + + @NonNull + @Override + public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + if (viewType == VIEW_TYPE_ADD_IMAGE) { + return new AddImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reddit_gallery_submission_add_image, parent, false)); + } + return new ImageViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_reddit_gallery_submission_image, parent, false)); + } + + @Override + public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { + if (holder instanceof ImageViewHolder) { + glide.load(redditGalleryImageInfoList.get(position).imageUrlString) + .listener(new RequestListener() { + + @Override + public boolean onLoadFailed(@Nullable GlideException e, Object model, Target target, boolean isFirstResource) { + ((ImageViewHolder) holder).progressBar.setVisibility(View.GONE); + ((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE); + return false; + } + + @Override + public boolean onResourceReady(Drawable resource, Object model, Target target, DataSource dataSource, boolean isFirstResource) { + ((ImageViewHolder) holder).progressBar.setVisibility(View.GONE); + ((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE); + return false; + } + }) + .into(((ImageViewHolder) holder).imageView); + + if (redditGalleryImageInfoList.get(position).payload != null) { + ((ImageViewHolder) holder).progressBar.setVisibility(View.GONE); + ((ImageViewHolder) holder).closeImageView.setVisibility(View.VISIBLE); + } + } + } + + @Override + public int getItemCount() { + return redditGalleryImageInfoList == null ? 1 : (redditGalleryImageInfoList.size() >= 9 ? 9 : redditGalleryImageInfoList.size() + 1); + } + + @Override + public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) { + super.onViewRecycled(holder); + if (holder instanceof ImageViewHolder) { + glide.clear(((ImageViewHolder) holder).imageView); + ((ImageViewHolder) holder).progressBar.setVisibility(View.VISIBLE); + ((ImageViewHolder) holder).closeImageView.setVisibility(View.GONE); + } + } + + public List getRedditGalleryImageInfoList() { + return redditGalleryImageInfoList; + } + + class ImageViewHolder extends RecyclerView.ViewHolder { + @BindView(R.id.aspect_ratio_gif_image_view_item_reddit_gallery_submission_image) + AspectRatioGifImageView imageView; + @BindView(R.id.progress_bar_item_reddit_gallery_submission_image) + ProgressBar progressBar; + @BindView(R.id.close_image_view_item_reddit_gallery_submission_image) + ImageView closeImageView; + + public ImageViewHolder(@NonNull View itemView) { + super(itemView); + + ButterKnife.bind(this, itemView); + + imageView.setRatio(1); + + closeImageView.setOnClickListener(view -> { + redditGalleryImageInfoList.remove(getBindingAdapterPosition()); + notifyItemRemoved(getBindingAdapterPosition()); + }); + } + } + + class AddImageViewHolder extends RecyclerView.ViewHolder { + + public AddImageViewHolder(@NonNull View itemView) { + super(itemView); + + itemView.setBackgroundTintList(ColorStateList.valueOf(customThemeWrapper.getColorPrimaryLightTheme())); + ((FloatingActionButton) itemView).setImageTintList(ColorStateList.valueOf(customThemeWrapper.getFABIconColor())); + + itemView.setOnClickListener(view -> itemClickListener.onAddImageClicked()); + } + } + + public static class RedditGalleryImageInfo { + public String imageUrlString; + public RedditGalleryPayload.Item payload; + + public RedditGalleryImageInfo(String imageUrlString) { + this.imageUrlString = imageUrlString; + } + } + + public interface ItemClickListener { + void onAddImageClicked(); + } +} diff --git a/app/src/main/res/layout/item_reddit_gallery_submission_add_image.xml b/app/src/main/res/layout/item_reddit_gallery_submission_add_image.xml new file mode 100644 index 00000000..c9d3478e --- /dev/null +++ b/app/src/main/res/layout/item_reddit_gallery_submission_add_image.xml @@ -0,0 +1,5 @@ + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_reddit_gallery_submission_image.xml b/app/src/main/res/layout/item_reddit_gallery_submission_image.xml new file mode 100644 index 00000000..e10bb1f1 --- /dev/null +++ b/app/src/main/res/layout/item_reddit_gallery_submission_image.xml @@ -0,0 +1,27 @@ + + + + + + + + + + \ No newline at end of file -- cgit v1.2.3