aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-07-15 13:51:41 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-07-15 13:51:41 +0000
commit48a65a4b2fba95a1aa76250afefefdc9829fa653 (patch)
treed8422d4b2989e85c9fcb92267b94221622e10cc5 /app/src/main/java/ml/docilealligator/infinityforreddit
parent14819b780ff1dd7e856965741ba522c9b5a85eab (diff)
downloadinfinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.tar
infinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.tar.gz
infinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.tar.bz2
infinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.tar.lz
infinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.tar.xz
infinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.tar.zst
infinity-for-reddit-48a65a4b2fba95a1aa76250afefefdc9829fa653.zip
Implement RedditGallerySubmissionRecyclerViewAdapter.
Diffstat (limited to 'app/src/main/java/ml/docilealligator/infinityforreddit')
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/PostGalleryActivity.java3
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java162
2 files changed, 162 insertions, 3 deletions
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<RecyclerView.ViewHolder> {
+
+ private static final int VIEW_TYPE_IMAGE = 1;
+ private static final int VIEW_TYPE_ADD_IMAGE = 2;
+
+ private List<RedditGalleryImageInfo> 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<Drawable>() {
+
+ @Override
+ public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> 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<Drawable> 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<RedditGalleryImageInfo> 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();
+ }
+}