aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/ml/docilealligator/infinityforreddit/adapters/UploadedImagesRecyclerViewAdapter.java
blob: f2c0364b43559fbb2a7a249a8fba7f2aec699a1e (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
package ml.docilealligator.infinityforreddit.adapters;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.thing.UploadedImage;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;

public class UploadedImagesRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private BaseActivity activity;
    private final ArrayList<UploadedImage> uploadedImages;
    private final ItemClickListener itemClickListener;

    public UploadedImagesRecyclerViewAdapter(Activity activity, ArrayList<UploadedImage> uploadedImages, ItemClickListener itemClickListener) {
        if (activity instanceof BaseActivity) {
            this.activity = (BaseActivity) activity;
        }
        this.uploadedImages = uploadedImages;
        this.itemClickListener = itemClickListener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new UploadedImageViewHolder(LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_uploaded_image, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        ((UploadedImageViewHolder) holder).imageNameTextView.setText(uploadedImages.get(position).imageName);
        ((UploadedImageViewHolder) holder).imageUrlTextView.setText(uploadedImages.get(position).imageUrlOrKey);
    }

    @Override
    public int getItemCount() {
        return uploadedImages == null ? 0 : uploadedImages.size();
    }

    private class UploadedImageViewHolder extends RecyclerView.ViewHolder {
        TextView imageNameTextView;
        TextView imageUrlTextView;

        public UploadedImageViewHolder(@NonNull View itemView) {
            super(itemView);
            imageNameTextView = itemView.findViewById(R.id.image_name_item_uploaded_image);
            imageUrlTextView = itemView.findViewById(R.id.image_url_item_uploaded_image);

            if (activity != null && activity.typeface != null) {
                imageNameTextView.setTypeface(activity.typeface);
                imageUrlTextView.setTypeface(activity.typeface);
            }

            itemView.setOnClickListener(view -> {
                itemClickListener.onClick(uploadedImages.get(getBindingAdapterPosition()));
            });
        }
    }

    public interface ItemClickListener {
        void onClick(UploadedImage uploadedImage);
    }
}