aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/RedditGallerySubmissionRecyclerViewAdapter.java
blob: 9c97d69b6d414ce7a61028bfcc77e1cd0635bc12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package ml.docilealligator.infinityforreddit.adapters;

import android.content.res.ColorStateList;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;

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.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import java.util.ArrayList;

import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.post.RedditGalleryPayload;
import ml.docilealligator.infinityforreddit.activities.PostGalleryActivity;
import ml.docilealligator.infinityforreddit.bottomsheetfragments.SetRedditGalleryItemCaptionAndUrlBottomSheetFragment;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.databinding.ItemRedditGallerySubmissionImageBinding;

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 final PostGalleryActivity activity;
    private ArrayList<RedditGalleryImageInfo> redditGalleryImageInfoList;
    private final CustomThemeWrapper customThemeWrapper;
    private final ItemClickListener itemClickListener;
    private final RequestManager glide;

    public RedditGallerySubmissionRecyclerViewAdapter(PostGalleryActivity activity, CustomThemeWrapper customThemeWrapper,
                                                      ItemClickListener itemClickListener) {
        this.activity = activity;
        glide = Glide.with(activity);
        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(ItemRedditGallerySubmissionImageBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ImageViewHolder) {
            glide.load(redditGalleryImageInfoList.get(position).imageUrlString)
                    .apply(new RequestOptions().transform(new CenterCrop(), new RoundedCorners(48)))
                    .listener(new RequestListener<>() {

                        @Override
                        public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                            ((ImageViewHolder) holder).binding.progressBarItemRedditGallerySubmissionImage.setVisibility(View.GONE);
                            ((ImageViewHolder) holder).binding.closeImageViewItemRedditGallerySubmissionImage.setVisibility(View.VISIBLE);
                            return false;
                        }

                        @Override
                        public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                            return false;
                        }
                    })
                    .into(((ImageViewHolder) holder).binding.aspectRatioGifImageViewItemRedditGallerySubmissionImage);

            if (redditGalleryImageInfoList.get(position).payload != null) {
                ((ImageViewHolder) holder).binding.progressBarItemRedditGallerySubmissionImage.setVisibility(View.GONE);
                ((ImageViewHolder) holder).binding.closeImageViewItemRedditGallerySubmissionImage.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return redditGalleryImageInfoList == null ? 1 : (redditGalleryImageInfoList.size() >= 20 ? 20 : redditGalleryImageInfoList.size() + 1);
    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof ImageViewHolder) {
            glide.clear(((ImageViewHolder) holder).binding.aspectRatioGifImageViewItemRedditGallerySubmissionImage);
            ((ImageViewHolder) holder).binding.progressBarItemRedditGallerySubmissionImage.setVisibility(View.VISIBLE);
            ((ImageViewHolder) holder).binding.closeImageViewItemRedditGallerySubmissionImage.setVisibility(View.GONE);
        }
    }

    public ArrayList<RedditGalleryImageInfo> getRedditGalleryImageInfoList() {
        return redditGalleryImageInfoList;
    }

    public void setRedditGalleryImageInfoList(ArrayList<RedditGalleryImageInfo> redditGalleryImageInfoList) {
        this.redditGalleryImageInfoList = redditGalleryImageInfoList;
        notifyDataSetChanged();
    }

    public void addImage(String imageUrl) {
        if (redditGalleryImageInfoList == null) {
            redditGalleryImageInfoList = new ArrayList<>();
        }
        redditGalleryImageInfoList.add(new RedditGalleryImageInfo(imageUrl));
        notifyItemInserted(redditGalleryImageInfoList.size() - 1);
    }

    public void setImageAsUploaded(String mediaId) {
        redditGalleryImageInfoList.get(redditGalleryImageInfoList.size() - 1).payload = new RedditGalleryPayload.Item("", "", mediaId);
        notifyItemChanged(redditGalleryImageInfoList.size() - 1);
    }

    public void removeFailedToUploadImage() {
        redditGalleryImageInfoList.remove(redditGalleryImageInfoList.size() - 1);
        notifyItemRemoved(redditGalleryImageInfoList.size());
    }

    public void setCaptionAndUrl(int position, String caption, String url) {
        if (redditGalleryImageInfoList.size() > position && position >= 0) {
            redditGalleryImageInfoList.get(position).payload.setCaption(caption);
            redditGalleryImageInfoList.get(position).payload.setOutboundUrl(url);
        }
    }

    class ImageViewHolder extends RecyclerView.ViewHolder {
        ItemRedditGallerySubmissionImageBinding binding;

        public ImageViewHolder(@NonNull ItemRedditGallerySubmissionImageBinding binding) {
            super(binding.getRoot());

            this.binding = binding;

            binding.aspectRatioGifImageViewItemRedditGallerySubmissionImage.setRatio(1);

            binding.aspectRatioGifImageViewItemRedditGallerySubmissionImage.setOnClickListener(view -> {
                RedditGalleryPayload.Item payload = redditGalleryImageInfoList.get(getBindingAdapterPosition()).payload;
                if (payload != null) {
                    SetRedditGalleryItemCaptionAndUrlBottomSheetFragment fragment = new SetRedditGalleryItemCaptionAndUrlBottomSheetFragment();
                    Bundle bundle = new Bundle();
                    bundle.putInt(SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.EXTRA_POSITION, getBindingAdapterPosition());
                    bundle.putString(SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.EXTRA_CAPTION, payload.getCaption());
                    bundle.putString(SetRedditGalleryItemCaptionAndUrlBottomSheetFragment.EXTRA_URL, payload.getOutboundUrl());
                    fragment.setArguments(bundle);
                    fragment.show(activity.getSupportFragmentManager(), fragment.getTag());
                }
            });

            binding.closeImageViewItemRedditGallerySubmissionImage.setOnClickListener(view -> {
                redditGalleryImageInfoList.remove(getBindingAdapterPosition());
                notifyItemRemoved(getBindingAdapterPosition());
            });
        }
    }

    class AddImageViewHolder extends RecyclerView.ViewHolder {

        public AddImageViewHolder(@NonNull View itemView) {
            super(itemView);

            FloatingActionButton fab = itemView.findViewById(R.id.fab_item_gallery_submission_add_image);
            fab.setBackgroundTintList(ColorStateList.valueOf(customThemeWrapper.getColorAccent()));
            fab.setImageTintList(ColorStateList.valueOf(customThemeWrapper.getFABIconColor()));

            itemView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    itemView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    int width = itemView.getMeasuredWidth();
                    ViewGroup.LayoutParams params = itemView.getLayoutParams();
                    params.height = width;
                    itemView.setLayoutParams(params);
                }
            });

            fab.setOnClickListener(view -> itemClickListener.onAddImageClicked());
            itemView.setOnClickListener(view -> fab.performClick());
        }
    }

    public static class RedditGalleryImageInfo implements Parcelable {
        public String imageUrlString;
        public RedditGalleryPayload.Item payload;

        public RedditGalleryImageInfo(String imageUrlString) {
            this.imageUrlString = imageUrlString;
        }

        protected RedditGalleryImageInfo(Parcel in) {
            imageUrlString = in.readString();
            payload = in.readParcelable(RedditGalleryPayload.Item.class.getClassLoader());
        }

        public static final Creator<RedditGalleryImageInfo> CREATOR = new Creator<RedditGalleryImageInfo>() {
            @Override
            public RedditGalleryImageInfo createFromParcel(Parcel in) {
                return new RedditGalleryImageInfo(in);
            }

            @Override
            public RedditGalleryImageInfo[] newArray(int size) {
                return new RedditGalleryImageInfo[size];
            }
        };

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel parcel, int i) {
            parcel.writeString(imageUrlString);
            parcel.writeParcelable(payload, i);
        }
    }

    public interface ItemClickListener {
        void onAddImageClicked();
    }
}