1
+ /*
2
+ * Copyright 2019 Pranav Pandey
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ package com .pranavpandey .android .dynamic .support .theme .adapter ;
18
+
19
+ import android .content .Context ;
20
+ import android .database .Cursor ;
21
+ import android .view .LayoutInflater ;
22
+ import android .view .View ;
23
+ import android .view .ViewGroup ;
24
+
25
+ import androidx .annotation .IntDef ;
26
+ import androidx .annotation .LayoutRes ;
27
+ import androidx .annotation .NonNull ;
28
+ import androidx .annotation .Nullable ;
29
+ import androidx .recyclerview .widget .RecyclerView ;
30
+
31
+ import com .pranavpandey .android .dynamic .support .R ;
32
+ import com .pranavpandey .android .dynamic .support .model .DynamicAppTheme ;
33
+ import com .pranavpandey .android .dynamic .support .theme .view .DynamicPresetsView ;
34
+ import com .pranavpandey .android .dynamic .support .theme .view .ThemePreview ;
35
+ import com .pranavpandey .android .dynamic .support .widget .DynamicCardView ;
36
+ import com .pranavpandey .android .dynamic .theme .ThemeContract ;
37
+ import com .pranavpandey .android .dynamic .theme .utils .DynamicThemeUtils ;
38
+
39
+ import static com .pranavpandey .android .dynamic .support .theme .adapter .DynamicPresetsAdapter .Type .HORIZONTAL ;
40
+ import static com .pranavpandey .android .dynamic .support .theme .adapter .DynamicPresetsAdapter .Type .VERTICAL ;
41
+
42
+ /**
43
+ * A recycler view adapter to show the theme presets.
44
+ */
45
+ public class DynamicPresetsAdapter <T extends DynamicAppTheme >
46
+ extends RecyclerView .Adapter <DynamicPresetsAdapter .ViewHolder <T >> {
47
+
48
+ /**
49
+ * Interface to hold the presets layout according to the recycler view orientation.
50
+ */
51
+ @ IntDef (value = { VERTICAL , HORIZONTAL })
52
+ public @interface Type {
53
+
54
+ /**
55
+ * Constant for the vertical orientation.
56
+ */
57
+ int VERTICAL = 1 ;
58
+
59
+ /**
60
+ * Constant for the horizontal orientation.
61
+ */
62
+ int HORIZONTAL = 2 ;
63
+ }
64
+
65
+ /**
66
+ * Layout inflater used by this adapter.
67
+ */
68
+ private final LayoutInflater mInflater ;
69
+
70
+ /**
71
+ * Cursor for presets handled by this adapter.
72
+ */
73
+ private Cursor mPresets ;
74
+
75
+ /**
76
+ * Type of the preset layout used by this adapter.
77
+ */
78
+ private @ Type int mType ;
79
+
80
+ /**
81
+ * Layout resource containing the theme preview.
82
+ *
83
+ * @see ThemePreview
84
+ */
85
+ private @ LayoutRes int mLayoutRes ;
86
+
87
+ /**
88
+ * Listener to handle the preset events.
89
+ */
90
+ private DynamicPresetsView .DynamicPresetsListener <T > mDynamicPresetsListener ;
91
+
92
+ /**
93
+ * Constructor to initialize an object of this class.
94
+ *
95
+ * @param context The context to get the layout inflater.
96
+ * @param type The type of the preset layout.
97
+ */
98
+ public DynamicPresetsAdapter (@ NonNull Context context , @ Type int type ) {
99
+ this (context , type , type == VERTICAL ? R .layout .ads_layout_item_preset
100
+ : R .layout .ads_layout_item_preset_horizontal );
101
+ }
102
+
103
+ /**
104
+ * Constructor to initialize an object of this class.
105
+ *
106
+ * @param context The context to get the layout inflater.
107
+ * @param type The type of the preset layout.
108
+ * @param layoutRes The layout resource containing the theme preview.
109
+ *
110
+ * @see ThemePreview
111
+ */
112
+ public DynamicPresetsAdapter (@ NonNull Context context ,
113
+ @ Type int type , @ LayoutRes int layoutRes ) {
114
+ this .mInflater = LayoutInflater .from (context );
115
+ this .mType = type ;
116
+ this .mLayoutRes = layoutRes ;
117
+ }
118
+
119
+ @ Override
120
+ public @ NonNull ViewHolder <T > onCreateViewHolder (@ NonNull ViewGroup parent , int viewType ) {
121
+ return new ViewHolder <>(mInflater .inflate (mLayoutRes , parent , false ));
122
+ }
123
+
124
+ @ Override
125
+ public void onBindViewHolder (@ NonNull ViewHolder <T > holder , int position ) {
126
+ if (mPresets != null ) {
127
+ holder .getRoot ().setVisibility (View .VISIBLE );
128
+ try {
129
+ if (mDynamicPresetsListener != null ) {
130
+ T theme = null ;
131
+ if (mPresets .moveToPosition (position )) {
132
+ String decodeTheme = DynamicThemeUtils .decodeTheme (
133
+ mPresets .getString (mPresets .getColumnIndexOrThrow (
134
+ ThemeContract .Preset .Column .THEME )));
135
+
136
+ if (decodeTheme != null ) {
137
+ theme = mDynamicPresetsListener .getDynamicTheme (decodeTheme );
138
+ }
139
+ }
140
+
141
+ if (theme == null ) {
142
+ return ;
143
+ }
144
+
145
+ holder .getThemePreview ().getActionView ()
146
+ .setImageResource (R .drawable .ads_ic_palette );
147
+ holder .getThemePreview ().setDynamicTheme (theme );
148
+
149
+ if (holder .getRoot () instanceof DynamicCardView ) {
150
+ ((DynamicCardView ) holder .getRoot ()).setCorner (
151
+ (float ) theme .getCornerRadius ());
152
+ }
153
+
154
+ holder .getRoot ().setOnClickListener (new View .OnClickListener () {
155
+ @ Override
156
+ public void onClick (View v ) {
157
+ mDynamicPresetsListener .onPresetClick (v ,
158
+ holder .getThemePreview ().getDynamicTheme ().toDynamicString (),
159
+ holder .getThemePreview ());
160
+ }
161
+ });
162
+ holder .getThemePreview ().getActionView ().setOnClickListener (
163
+ new View .OnClickListener () {
164
+ @ Override
165
+ public void onClick (View v ) {
166
+ mDynamicPresetsListener .onPresetClick (v ,
167
+ holder .getThemePreview ().getDynamicTheme ().toDynamicString (),
168
+ holder .getThemePreview ());
169
+ }
170
+ });
171
+ } else {
172
+ holder .getRoot ().setClickable (false );
173
+ holder .getThemePreview ().getActionView ().setClickable (false );
174
+ }
175
+ } catch (Exception ignored ) {
176
+ }
177
+ } else {
178
+ holder .getRoot ().setVisibility (View .GONE );
179
+ }
180
+ }
181
+
182
+ @ Override
183
+ public int getItemCount () {
184
+ return mPresets == null ? 0 : mPresets .getCount ();
185
+ }
186
+
187
+ /**
188
+ * Sets the theme presets for this adapter.
189
+ *
190
+ * @param presets The theme presets to be set.
191
+ */
192
+ public void setPresets (@ Nullable Cursor presets ) {
193
+ mPresets = presets ;
194
+
195
+ notifyDataSetChanged ();
196
+ }
197
+
198
+ /**
199
+ * Returns the layout resource containing the theme preview.
200
+ *
201
+ * @return The layout resource containing the theme preview.
202
+ */
203
+ public int getLayoutRes () {
204
+ return mLayoutRes ;
205
+ }
206
+
207
+ /**
208
+ * Get the dynamic preset listener used by this adapter.
209
+ *
210
+ * @return The dynamic preset listener used by this adapter.
211
+ */
212
+ public @ Nullable DynamicPresetsView .DynamicPresetsListener <T > getDynamicPresetsListener () {
213
+ return mDynamicPresetsListener ;
214
+ }
215
+
216
+ /**
217
+ * Sets the dynamic preset listener for this adapter.
218
+ */
219
+ public void setDynamicPresetsListener (
220
+ @ Nullable DynamicPresetsView .DynamicPresetsListener <T > dynamicPresetsListener ) {
221
+ this .mDynamicPresetsListener = dynamicPresetsListener ;
222
+
223
+ notifyDataSetChanged ();
224
+ }
225
+
226
+ /**
227
+ * View holder class to hold the preset view.
228
+ */
229
+ static class ViewHolder <T extends DynamicAppTheme > extends RecyclerView .ViewHolder {
230
+
231
+ /**
232
+ * Item view root.
233
+ */
234
+ private final ViewGroup root ;
235
+
236
+ /**
237
+ * Theme preview to display the preset.
238
+ */
239
+ private final ThemePreview <T > themePreview ;
240
+
241
+ /**
242
+ * Constructor to initialize views from the supplied layout.
243
+ *
244
+ * @param view The view for this view holder.
245
+ */
246
+ ViewHolder (View view ) {
247
+ super (view );
248
+
249
+ root = view .findViewById (R .id .ads_preset_root );
250
+ themePreview = view .findViewById (R .id .ads_preset_theme_preview );
251
+ }
252
+
253
+ /**
254
+ * Get the item view root.
255
+ *
256
+ * @return The item view root.
257
+ */
258
+ ViewGroup getRoot () {
259
+ return root ;
260
+ }
261
+
262
+ /**
263
+ * Get the theme preview to display the preset.
264
+ *
265
+ * @return The theme preview to display the preset.
266
+ */
267
+ ThemePreview <T > getThemePreview () {
268
+ return themePreview ;
269
+ }
270
+ }
271
+ }
0 commit comments