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

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.checkbox.MaterialCheckBox;

import java.util.List;

import jp.wasabeef.glide.transformations.RoundedCornersTransformation;
import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.subreddit.SubredditData;
import pl.droidsonroids.gif.GifImageView;

public class SubredditAutocompleteRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final BaseActivity activity;
    private List<SubredditData> subreddits;
    private final RequestManager glide;
    private final CustomThemeWrapper customThemeWrapper;
    private final ItemOnClickListener itemOnClickListener;

    public SubredditAutocompleteRecyclerViewAdapter(BaseActivity activity, CustomThemeWrapper customThemeWrapper,
                                                    ItemOnClickListener itemOnClickListener) {
        this.activity = activity;
        glide = Glide.with(activity);
        this.customThemeWrapper = customThemeWrapper;
        this.itemOnClickListener = itemOnClickListener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SubredditViewHolder) {
            glide.load(subreddits.get(position).getIconUrl())
                    .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0)))
                    .error(glide.load(R.drawable.subreddit_default_icon)
                            .apply(RequestOptions.bitmapTransform(new RoundedCornersTransformation(72, 0))))
                    .into(((SubredditViewHolder) holder).iconImageView);
            ((SubredditViewHolder) holder).subredditNameTextView.setText(subreddits.get(position).getName());
            ((SubredditViewHolder) holder).subscriberCountTextView.setText(activity.getString(R.string.subscribers_number_detail, subreddits.get(position).getNSubscribers()));
        }
    }

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

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof SubredditViewHolder) {
            glide.clear(((SubredditViewHolder) holder).iconImageView);
        }
    }

    public void setSubreddits(List<SubredditData> subreddits) {
        this.subreddits = subreddits;
        notifyDataSetChanged();
    }

    class SubredditViewHolder extends RecyclerView.ViewHolder {

        GifImageView iconImageView;
        TextView subredditNameTextView;
        TextView subscriberCountTextView;
        ImageView subscribeImageView;
        MaterialCheckBox checkBox;

        public SubredditViewHolder(@NonNull View itemView) {
            super(itemView);
            iconImageView = itemView.findViewById(R.id.subreddit_icon_gif_image_view_item_subreddit_listing);
            subredditNameTextView = itemView.findViewById(R.id.subreddit_name_text_view_item_subreddit_listing);
            subscriberCountTextView = itemView.findViewById(R.id.subscriber_count_text_view_item_subreddit_listing);
            subscribeImageView = itemView.findViewById(R.id.subscribe_image_view_item_subreddit_listing);
            checkBox = itemView.findViewById(R.id.checkbox_item_subreddit_listing);

            subscribeImageView.setVisibility(View.GONE);
            checkBox.setVisibility(View.GONE);

            subredditNameTextView.setTextColor(customThemeWrapper.getPrimaryTextColor());
            subscriberCountTextView.setTextColor(customThemeWrapper.getSecondaryTextColor());

            if (activity.typeface != null) {
                subredditNameTextView.setTypeface(activity.typeface);
                subscriberCountTextView.setTypeface(activity.typeface);
            }

            itemView.setOnClickListener(view -> {
                itemOnClickListener.onClick(subreddits.get(getBindingAdapterPosition()));
            });
        }
    }

    public interface ItemOnClickListener {
        void onClick(SubredditData subredditData);
    }
}