/
TiRecyclerViewHolder.java
248 lines (212 loc) Β· 8.01 KB
/
TiRecyclerViewHolder.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
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
247
248
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2020 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui.widget.listview;
import android.app.Activity;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RippleDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.util.TypedValue;
import android.view.ViewGroup;
import androidx.core.graphics.ColorUtils;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.color.MaterialColors;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.R;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiViewProxy;
import org.appcelerator.titanium.util.TiConvert;
import org.appcelerator.titanium.util.TiUIHelper;
import java.lang.ref.WeakReference;
public abstract class TiRecyclerViewHolder extends RecyclerView.ViewHolder
{
private static final String TAG = "TiRecyclerViewHolder";
protected static final int COLOR_GRAY = Color.rgb(169, 169, 169);
protected static int COLOR_NORMAL;
protected static int COLOR_PRIMARY;
protected static int COLOR_SELECTED;
protected static Drawable checkDrawable;
protected static Drawable disclosureDrawable;
protected static Drawable dragDrawable;
protected static Drawable moreDrawable;
protected static Drawable checkcircleDrawable;
protected static Drawable circleDrawable;
protected static Resources resources;
protected WeakReference<TiViewProxy> proxy;
public TiRecyclerViewHolder(final Context context, final ViewGroup viewGroup)
{
super(viewGroup);
COLOR_NORMAL = MaterialColors.getColor(context, R.attr.colorButtonNormal, Color.DKGRAY);
COLOR_PRIMARY = MaterialColors.getColor(context, R.attr.colorPrimary, Color.DKGRAY);
COLOR_SELECTED = ColorUtils.setAlphaComponent(COLOR_PRIMARY, 20);
if (resources == null) {
// Obtain resources instance.
resources = context.getResources();
}
if (resources != null) {
// Attempt to load `titanium_icon_more` drawable.
if (moreDrawable == null) {
try {
final int icon_more_id = R.drawable.titanium_icon_more;
moreDrawable = resources.getDrawable(icon_more_id);
} catch (Exception e) {
Log.w(TAG, "Drawable 'drawable.titanium_icon_more' not found.");
}
}
// Attempt to load `titanium_icon_checkmark` drawable.
if (checkDrawable == null) {
try {
final int icon_checkmark_id = R.drawable.titanium_icon_checkmark;
checkDrawable = resources.getDrawable(icon_checkmark_id);
} catch (Exception e) {
Log.w(TAG, "Drawable 'drawable.titanium_icon_checkmark' not found.");
}
}
// Attempt to load `titanium_icon_disclosure` drawable.
if (disclosureDrawable == null) {
try {
final int icon_disclosure_id = R.drawable.titanium_icon_disclosure;
disclosureDrawable = resources.getDrawable(icon_disclosure_id);
} catch (Exception e) {
Log.w(TAG, "Drawable 'drawable.titanium_icon_disclosure' not found.");
}
}
// Attempt to load `titanium_icon_drag` drawable.
if (dragDrawable == null) {
try {
final int icon_drag_id = R.drawable.titanium_icon_drag;
dragDrawable = resources.getDrawable(icon_drag_id);
} catch (Exception e) {
Log.w(TAG, "Drawable 'drawable.titanium_icon_drag' not found.");
}
}
// Attempt to load `titanium_icon_checkcircle` drawable.
if (checkcircleDrawable == null) {
try {
final int icon_checkcircle_id = R.drawable.titanium_icon_checkcircle;
checkcircleDrawable = resources.getDrawable(icon_checkcircle_id);
} catch (Exception e) {
Log.w(TAG, "Drawable 'drawable.titanium_icon_checkcircle' not found.");
}
}
if (checkcircleDrawable != null) {
// Always set tint color in case of dynamic theme change.
checkcircleDrawable.setTint(COLOR_PRIMARY);
}
// Attempt to load `titanium_icon_circle` drawable.
if (circleDrawable == null) {
try {
final int icon_circle_id = R.drawable.titanium_icon_circle;
circleDrawable = resources.getDrawable(icon_circle_id);
} catch (Exception e) {
Log.w(TAG, "Drawable 'drawable.titanium_icon_circle' not found.");
}
}
if (circleDrawable != null) {
// Always set tint color in case of dynamic theme change.
circleDrawable.setTint(COLOR_NORMAL);
}
} else {
Log.w(TAG, "Could not obtain context resources instance.");
}
}
/**
* Generate ripple effect drawable from specified drawable.
*
* @param drawable Drawable to apply ripple effect.
* @return Drawable
*/
protected Drawable generateRippleDrawable(Drawable drawable, String color)
{
final Activity activity = TiApplication.getAppCurrentActivity();
if (activity != null) {
final int[][] rippleStates = new int[][] { new int[] {} };
final TypedValue typedValue = new TypedValue();
final TypedArray colorControlHighlight = activity.obtainStyledAttributes(
typedValue.data, new int[] { android.R.attr.colorControlHighlight });
final int colorControlHighlightInt = color != null && !color.isEmpty()
? TiConvert.toColor(color) : colorControlHighlight.getColor(0, 0);
final int[] rippleColors = new int[] { colorControlHighlightInt };
final ColorStateList colorStateList = new ColorStateList(rippleStates, rippleColors);
final ShapeDrawable maskDrawable = drawable == null ? new ShapeDrawable() : null;
// Create the RippleDrawable.
drawable = new RippleDrawable(colorStateList, drawable, maskDrawable);
}
return drawable;
}
/**
* Generate selected background from proxy properties.
*
* @param properties Dictionary containing selected background properties.
* @return Drawable
*/
protected Drawable generateSelectedDrawable(KrollDict properties, Drawable drawable)
{
final StateListDrawable stateDrawable = new StateListDrawable();
if (properties.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR)
|| properties.containsKeyAndNotNull(TiC.PROPERTY_BACKGROUND_SELECTED_IMAGE)) {
final Drawable selectedBackgroundDrawable = TiUIHelper.buildBackgroundDrawable(
properties.getString(TiC.PROPERTY_BACKGROUND_SELECTED_COLOR),
properties.getString(TiC.PROPERTY_BACKGROUND_SELECTED_IMAGE),
TiConvert.toBoolean(properties.get(TiC.PROPERTY_BACKGROUND_REPEAT), false),
null
);
stateDrawable.addState(new int[] { android.R.attr.state_activated }, selectedBackgroundDrawable);
} else {
stateDrawable.addState(new int[] { android.R.attr.state_activated }, new ColorDrawable(COLOR_SELECTED));
}
// NOTE: Android 7.1 and below require ShapeDrawable to have non-null Shape.
// This bug is fixed on Android 8.0 and above.
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
Drawable currentDrawable = drawable;
if (currentDrawable instanceof RippleDrawable) {
currentDrawable = ((RippleDrawable) currentDrawable).getDrawable(0);
}
if (currentDrawable instanceof ShapeDrawable) {
final ShapeDrawable shapeDrawable = (ShapeDrawable) currentDrawable;
if (shapeDrawable.getShape() == null) {
shapeDrawable.setShape(new Shape()
{
@Override
public void draw(Canvas canvas, Paint paint)
{
canvas.drawPaint(paint);
}
});
}
}
}
if (drawable != null) {
stateDrawable.addState(new int[] {}, drawable);
}
return stateDrawable;
}
/**
* Get current proxy assigned to holder.
*
* @return TiViewProxy
*/
public TiViewProxy getProxy()
{
if (this.proxy != null) {
return this.proxy.get();
}
return null;
}
}