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

import android.content.res.ColorStateList;
import android.view.LayoutInflater;
import android.view.ViewGroup;

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

import java.util.List;

import ml.docilealligator.infinityforreddit.R;
import ml.docilealligator.infinityforreddit.thing.SelectThingReturnKey;
import ml.docilealligator.infinityforreddit.activities.BaseActivity;
import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper;
import ml.docilealligator.infinityforreddit.databinding.ItemRecentSearchQueryBinding;
import ml.docilealligator.infinityforreddit.recentsearchquery.RecentSearchQuery;

public class SearchActivityRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final BaseActivity activity;
    private List<RecentSearchQuery> recentSearchQueries;
    private final int filledCardViewBackgroundColor;
    private final int primaryTextColor;
    private final int secondaryTextColor;
    private final int subredditTextColor;
    private final int userTextColor;
    private final ItemOnClickListener itemOnClickListener;

    public interface ItemOnClickListener {
        void onClick(RecentSearchQuery recentSearchQuery);
        void onDelete(RecentSearchQuery recentSearchQuery);
    }

    public SearchActivityRecyclerViewAdapter(BaseActivity activity, CustomThemeWrapper customThemeWrapper,
                                             ItemOnClickListener itemOnClickListener) {
        this.activity = activity;
        this.filledCardViewBackgroundColor = customThemeWrapper.getFilledCardViewBackgroundColor();
        this.primaryTextColor = customThemeWrapper.getPrimaryTextColor();
        this.secondaryTextColor = customThemeWrapper.getSecondaryTextColor();
        this.subredditTextColor = customThemeWrapper.getSubreddit();
        this.userTextColor = customThemeWrapper.getUsername();
        this.itemOnClickListener = itemOnClickListener;
    }

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

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof RecentSearchQueryViewHolder) {
            if (recentSearchQueries != null && !recentSearchQueries.isEmpty() && position < recentSearchQueries.size()) {
                RecentSearchQuery recentSearchQuery = recentSearchQueries.get(position);
                ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryTextViewItemRecentSearchQuery.setText(recentSearchQuery.getSearchQuery());
                switch (recentSearchQuery.getSearchInThingType()) {
                    case SelectThingReturnKey.THING_TYPE.SUBREDDIT:
                        if (recentSearchQuery.getSearchInSubredditOrUserName() != null && !recentSearchQuery.getSearchInSubredditOrUserName().isEmpty()) {
                            ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                    .setTextColor(subredditTextColor);
                            ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                    .setText("r/" + recentSearchQuery.getSearchInSubredditOrUserName());
                        } else {
                            ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                    .setTextColor(secondaryTextColor);
                            ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                    .setText(R.string.all_subreddits);
                        }
                        break;
                    case SelectThingReturnKey.THING_TYPE.USER:
                        ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                .setTextColor(userTextColor);
                        ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                .setText("u/" + recentSearchQuery.getSearchInSubredditOrUserName());
                        break;
                    case SelectThingReturnKey.THING_TYPE.MULTIREDDIT:
                        ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                .setTextColor(secondaryTextColor);
                        ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery
                                .setText(recentSearchQuery.getMultiRedditDisplayName());
                }
                holder.itemView.postDelayed(() -> {
                    ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryTextViewItemRecentSearchQuery.setSelected(true);
                    ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery.setSelected(true);
                }, 1000);
            }
        }
    }

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

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        if (holder instanceof RecentSearchQueryViewHolder) {
            ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryTextViewItemRecentSearchQuery.setSelected(false);
            ((RecentSearchQueryViewHolder) holder).binding.recentSearchQueryWhereTextViewItemRecentSearchQuery.setSelected(false);
        }
    }

    public void setRecentSearchQueries(List<RecentSearchQuery> recentSearchQueries) {
        this.recentSearchQueries = recentSearchQueries;
        notifyDataSetChanged();
    }

    class RecentSearchQueryViewHolder extends RecyclerView.ViewHolder {
        ItemRecentSearchQueryBinding binding;

        public RecentSearchQueryViewHolder(@NonNull ItemRecentSearchQueryBinding binding) {
            super(binding.getRoot());
            this.binding = binding;

            itemView.setBackgroundTintList(ColorStateList.valueOf(filledCardViewBackgroundColor));

            binding.recentSearchQueryTextViewItemRecentSearchQuery.setTextColor(primaryTextColor);

            if (activity.typeface != null) {
                binding.recentSearchQueryTextViewItemRecentSearchQuery.setTypeface(activity.typeface);
            }

            itemView.setOnClickListener(view -> {
                if (recentSearchQueries != null && !recentSearchQueries.isEmpty()) {
                    itemOnClickListener.onClick(recentSearchQueries.get(getBindingAdapterPosition()));
                }
            });

            itemView.setOnLongClickListener(view -> {
                itemOnClickListener.onDelete(recentSearchQueries.get(getBindingAdapterPosition()));
                return true;
            });
        }
    }
}