-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
ColorRVAdapter.java
154 lines (126 loc) · 5.59 KB
/
ColorRVAdapter.java
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
/*
* Copyright (C) 2019 - 2024 Vishnu Sanal. T
*
* This file is part of Quotes Status Creator.
*
* Quotes Status Creator is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package phone.vishnu.quotes.adapter;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
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.core.content.ContextCompat;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.ListAdapter;
import androidx.recyclerview.widget.RecyclerView;
import phone.vishnu.quotes.R;
import phone.vishnu.quotes.helper.Constants;
import phone.vishnu.quotes.helper.SharedPreferenceHelper;
import phone.vishnu.quotes.helper.Utils;
public class ColorRVAdapter extends ListAdapter<String, ColorRVAdapter.ViewHolder> {
private OnItemClickListener listener;
private final int REQ_CODE;
public ColorRVAdapter(int REQ_CODE) {
super(
new DiffUtil.ItemCallback<String>() {
@Override
public boolean areItemsTheSame(
@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equals(newItem);
}
@Override
public boolean areContentsTheSame(
@NonNull String oldItem, @NonNull String newItem) {
return oldItem.equals(newItem);
}
});
this.REQ_CODE = REQ_CODE;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v =
LayoutInflater.from(parent.getContext())
.inflate(R.layout.color_pick_single_item, parent, false);
return new ColorRVAdapter.ViewHolder(v);
}
@SuppressLint("SetTextI18n")
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.setIsRecyclable(false);
if (getItem(position).equals("#00000000")) {
if (position == 0) {
holder.imageView.setImageDrawable(
ContextCompat.getDrawable(
holder.itemView.getContext(), R.drawable.ic_custom));
holder.textView.setText("CUSTOM");
} else {
holder.imageView.setImageDrawable(
ContextCompat.getDrawable(
holder.itemView.getContext(), R.drawable.ic_no_color));
holder.textView.setText("TRANSPARENT");
}
} else {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setColor(Color.parseColor(getItem(position)));
gradientDrawable.setCornerRadius(
Utils.Companion.DPtoPX(holder.itemView.getContext(), 96));
if (isFontColor(holder.itemView.getContext(), getItem(position))
|| isCardColor(holder.itemView.getContext(), getItem(position))) {
gradientDrawable.setStroke(
Utils.Companion.DPtoPX(holder.itemView.getContext(), 2),
holder.itemView.getContext().getResources().getColor(R.color.spacerColor));
}
holder.imageView.setBackgroundDrawable(gradientDrawable);
holder.textView.setText(getItem(position).replace("#FF", "#"));
}
}
private boolean isCardColor(Context context, String s) {
return REQ_CODE == Constants.PICK_CARD_COLOR_REQ_CODE
&& s.equals(new SharedPreferenceHelper(context).getCardColorPreference());
}
private boolean isFontColor(Context context, String s) {
return REQ_CODE == Constants.PICK_FONT_COLOR_REQ_CODE
&& s.equals(new SharedPreferenceHelper(context).getFontColorPreference());
}
public void setOnItemClickListener(OnItemClickListener listener) {
this.listener = listener;
}
public interface OnItemClickListener {
void onItemClick(String colorString, boolean isCustom);
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private final ImageView imageView;
private final TextView textView;
public ViewHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.colorPickSingleView);
textView = itemView.findViewById(R.id.colorPickSingleTV);
imageView.setOnClickListener(this);
textView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (listener != null && getAdapterPosition() != RecyclerView.NO_POSITION)
listener.onItemClick(getItem(getAdapterPosition()), getAdapterPosition() == 0);
}
}
}