aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/adapter/SectionAdapter.java
blob: 33bbb384766c5c9a8665873b311f6a467a69e0bd (plain)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
/*
  This file is part of Subsonic.
	Subsonic 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.
	Subsonic 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 Subsonic. If not, see <http://www.gnu.org/licenses/>.
	Copyright 2015 (C) Scott Jackson
*/

package github.daneren2005.dsub.adapter;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.util.TypedValue;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.PopupMenu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.activity.SubsonicFragmentActivity;
import github.daneren2005.dsub.util.Constants;
import github.daneren2005.dsub.util.MenuUtil;
import github.daneren2005.dsub.util.Util;
import github.daneren2005.dsub.view.BasicHeaderView;
import github.daneren2005.dsub.view.UpdateView;
import github.daneren2005.dsub.view.UpdateView.UpdateViewHolder;

public abstract class SectionAdapter<T> extends RecyclerView.Adapter<UpdateViewHolder<T>> {
	private static String TAG = SectionAdapter.class.getSimpleName();
	public static int VIEW_TYPE_HEADER = 0;
	public static String[] ignoredArticles;

	protected Context context;
	protected List<String> headers;
	protected List<List<T>> sections;
	protected boolean singleSectionHeader;
	protected OnItemClickedListener<T> onItemClickedListener;
	protected List<T> selected = new ArrayList<>();
	protected List<UpdateView> selectedViews = new ArrayList<>();
	protected ActionMode currentActionMode;
	protected boolean checkable = false;

	protected SectionAdapter() {}
	public SectionAdapter(Context context, List<T> section) {
		this(context, section, false);
	}
	public SectionAdapter(Context context, List<T> section, boolean singleSectionHeader) {
		this.context = context;
		this.headers = Arrays.asList("Section");
		this.sections = new ArrayList<>();
		this.sections.add(section);
		this.singleSectionHeader = singleSectionHeader;
	}
	public SectionAdapter(Context context, List<String> headers, List<List<T>> sections) {
		this(context, headers, sections, true);
	}
	public SectionAdapter(Context context, List<String> headers, List<List<T>> sections, boolean singleSectionHeader){
		this.context = context;
		this.headers = headers;
		this.sections = sections;
		this.singleSectionHeader = singleSectionHeader;
	}

	public void replaceExistingData(List<T> section) {
		this.sections = new ArrayList<>();
		this.sections.add(section);
		notifyDataSetChanged();
	}
	public void replaceExistingData(List<String> headers, List<List<T>> sections) {
		this.headers = headers;
		this.sections = sections;
		notifyDataSetChanged();
	}

	@Override
	public UpdateViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		if(viewType == VIEW_TYPE_HEADER) {
			return onCreateHeaderHolder(parent);
		} else {
			final UpdateViewHolder<T> holder = onCreateSectionViewHolder(parent, viewType);
			final UpdateView updateView = holder.getUpdateView();

			if(updateView != null) {
				updateView.getChildAt(0).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						T item = holder.getItem();
						updateView.onClick();
						if (currentActionMode != null) {
							if(updateView.isCheckable()) {
								if (selected.contains(item)) {
									selected.remove(item);
									selectedViews.remove(updateView);
									setChecked(updateView, false);
								} else {
									selected.add(item);
									selectedViews.add(updateView);
									setChecked(updateView, true);
								}

								if (selected.isEmpty()) {
									currentActionMode.finish();
								} else {
									currentActionMode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
								}
							}
						} else if (onItemClickedListener != null) {
							onItemClickedListener.onItemClicked(updateView, item);
						}
					}
				});

				View moreButton = updateView.findViewById(R.id.item_more);
				if (moreButton != null) {
					moreButton.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View v) {
							try {
								final T item = holder.getItem();
								if (onItemClickedListener != null) {
									PopupMenu popup = new PopupMenu(context, v);
									onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);

									popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
										@Override
										public boolean onMenuItemClick(MenuItem menuItem) {
											return onItemClickedListener.onContextItemSelected(menuItem, updateView, item);
										}
									});
									popup.show();
								}
							} catch(Exception e) {
								Log.w(TAG, "Failed to show popup", e);
							}
						}
					});

					if(checkable) {
						updateView.getChildAt(0).setOnLongClickListener(new View.OnLongClickListener() {
							@Override
							public boolean onLongClick(View v) {
								if(updateView.isCheckable()) {
									if (currentActionMode == null) {
										startActionMode(holder);
									} else {
										updateView.getChildAt(0).performClick();
									}
								}
								return true;
							}
						});
					}
				}
			}

			return holder;
		}
	}

	@Override
	public void onBindViewHolder(UpdateViewHolder holder, int position) {
		UpdateView updateView = holder.getUpdateView();

		if(sections.size() == 1 && !singleSectionHeader) {
			T item = sections.get(0).get(position);
			onBindViewHolder(holder, item, getItemViewType(position));
			postBindView(updateView, item);
			holder.setItem(item);
			return;
		}

		int subPosition = 0;
		int subHeader = 0;
		for(List<T> section: sections) {
			boolean validHeader = headers.get(subHeader) != null;
			if(position == subPosition && validHeader) {
				onBindHeaderHolder(holder, headers.get(subHeader), subHeader);
				return;
			}

			int headerOffset = validHeader ? 1 : 0;
			if(position < (subPosition + section.size() + headerOffset)) {
				T item = section.get(position - subPosition - headerOffset);
				onBindViewHolder(holder, item, getItemViewType(item));

				postBindView(updateView, item);
				holder.setItem(item);
				return;
			}

			subPosition += section.size();
			if(validHeader) {
				subPosition += 1;
			}
			subHeader++;
		}
	}

	private void postBindView(UpdateView updateView, T item) {
		if(updateView.isCheckable()) {
			setChecked(updateView, selected.contains(item));
		}

		View moreButton = updateView.findViewById(R.id.item_more);
		if(moreButton != null) {
			if(onItemClickedListener != null) {
				PopupMenu popup = new PopupMenu(context, moreButton);
				Menu menu = popup.getMenu();
				onItemClickedListener.onCreateContextMenu(popup.getMenu(), popup.getMenuInflater(), updateView, item);
				if (menu.size() == 0) {
					moreButton.setVisibility(View.GONE);
				} else {
					moreButton.setVisibility(View.VISIBLE);
				}
			} else {
				moreButton.setVisibility(View.VISIBLE);
			}
		}
	}

	@Override
	public int getItemCount() {
		if(sections.size() == 1 && !singleSectionHeader) {
			return sections.get(0).size();
		}

		int count = 0;
		for(String header: headers) {
			if(header != null) {
				count++;
			}
		}
		for(List<T> section: sections) {
			count += section.size();
		}

		return count;
	}

	@Override
	public int getItemViewType(int position) {
		if(sections.size() == 1 && !singleSectionHeader) {
			return getItemViewType(sections.get(0).get(position));
		}

		int subPosition = 0;
		int subHeader = 0;
		for(List<T> section: sections) {
			boolean validHeader = headers.get(subHeader) != null;
			if(position == subPosition && validHeader) {
				return VIEW_TYPE_HEADER;
			}

			int headerOffset = validHeader ? 1 : 0;
			if(position < (subPosition + section.size() + headerOffset)) {
				return getItemViewType(section.get(position - subPosition - headerOffset));
			}

			subPosition += section.size();
			if(validHeader) {
				subPosition += 1;
			}
			subHeader++;
		}

		return -1;
	}

	public UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
		return new UpdateViewHolder(new BasicHeaderView(context));
	}
	public void onBindHeaderHolder(UpdateViewHolder holder, String header, int sectionIndex) {
		UpdateView view = holder.getUpdateView();
		if(view != null) {
			view.setObject(header);
		}
	}

	public T getItemForPosition(int position) {
		if(sections.size() == 1 && !singleSectionHeader) {
			return sections.get(0).get(position);
		}

		int subPosition = 0;
		for(List<T> section: sections) {
			if(position == subPosition) {
				return null;
			}

			if(position <= (subPosition + section.size())) {
				return section.get(position - subPosition - 1);
			}

			subPosition += section.size() + 1;
		}

		return null;
	}
	public int getItemPosition(T item) {
		if(sections.size() == 1 && !singleSectionHeader) {
			return sections.get(0).indexOf(item);
		}

		int subPosition = 0;
		for(List<T> section: sections) {
			subPosition += section.size() + 1;

			int position = section.indexOf(item);
			if(position != -1) {
				return position + subPosition;
			}
		}

		return -1;
	}

	public void setOnItemClickedListener(OnItemClickedListener<T> onItemClickedListener) {
		this.onItemClickedListener = onItemClickedListener;
	}

	public void addSelected(T item) {
		selected.add(item);
	}
	public List<T> getSelected() {
		List<T> selected = new ArrayList<>();
		selected.addAll(this.selected);
		return selected;
	}

	public void clearSelected() {
		// TODO: This needs to work with multiple sections
		for(T item: selected) {
			int index = sections.get(0).indexOf(item);

			if(singleSectionHeader) {
				index++;
			}
		}
		selected.clear();

		for(UpdateView updateView: selectedViews) {
			updateView.setChecked(false);
		}
	}

	public void moveItem(int from, int to) {
		List<T> section = sections.get(0);
		int max = section.size();
		if(to >= max) {
			to = max - 1;
		} else if(to < 0) {
			to = 0;
		}

		T moved = section.remove(from);
		section.add(to, moved);

		notifyItemMoved(from, to);
	}
	public void removeItem(T item) {
		int subPosition = 0;
		for(List<T> section: sections) {
			if(sections.size() > 1 || singleSectionHeader) {
				subPosition++;
			}

			int index = section.indexOf(item);
			if (index != -1) {
				section.remove(item);
				notifyItemRemoved(subPosition + index);
				break;
			}

			subPosition += section.size();
		}
	}

	public abstract UpdateView.UpdateViewHolder onCreateSectionViewHolder(ViewGroup parent, int viewType);
	public abstract void onBindViewHolder(UpdateViewHolder holder, T item, int viewType);
	public abstract int getItemViewType(T item);
	public void setCheckable(boolean checkable) {
		this.checkable = checkable;
	}
	public void setChecked(UpdateView updateView, boolean checked) {
		updateView.setChecked(checked);
	}
	public void onCreateActionModeMenu(Menu menu, MenuInflater menuInflater) {}

	private void startActionMode(final UpdateView.UpdateViewHolder<T> holder) {
		final UpdateView<T> updateView = holder.getUpdateView();
		if (context instanceof SubsonicFragmentActivity && currentActionMode == null) {
			final SubsonicFragmentActivity fragmentActivity = (SubsonicFragmentActivity) context;
			fragmentActivity.startSupportActionMode(new ActionMode.Callback() {
				@Override
				public boolean onCreateActionMode(ActionMode mode, Menu menu) {
					currentActionMode = mode;

					T item = holder.getItem();
					selected.add(item);
					selectedViews.add(updateView);
					setChecked(updateView, true);

					onCreateActionModeMenu(menu, mode.getMenuInflater());
					MenuUtil.hideMenuItems(context, menu, updateView);

					mode.setTitle(context.getResources().getString(R.string.select_album_n_selected, selected.size()));
					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
						TypedValue typedValue = new TypedValue();
						Resources.Theme theme = context.getTheme();
						theme.resolveAttribute(R.attr.colorPrimaryDark, typedValue, true);
						int colorPrimaryDark = typedValue.data;

						Window window = ((SubsonicFragmentActivity) context).getWindow();
						window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
						window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
						window.setStatusBarColor(colorPrimaryDark);
					}
					return true;
				}

				@Override
				public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
					return false;
				}

				@Override
				public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
					if (fragmentActivity.onOptionsItemSelected(item)) {
						currentActionMode.finish();
						return true;
					} else {
						return false;
					}
				}

				@Override
				public void onDestroyActionMode(ActionMode mode) {
					currentActionMode = null;
					selected.clear();
					for (UpdateView<T> updateView : selectedViews) {
						updateView.setChecked(false);
					}
					selectedViews.clear();

					if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Util.getPreferences(context).getBoolean(Constants.PREFERENCES_KEY_COLOR_ACTION_BAR, true)) {
						Window window = ((SubsonicFragmentActivity) context).getWindow();
						window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
					}
				}
			});
		}
	}
	public void stopActionMode() {
		if(currentActionMode != null) {
			currentActionMode.finish();
		}
	}

	public String getNameIndex(String name) {
		return getNameIndex(name, false);
	}
	public String getNameIndex(String name, boolean removeIgnoredArticles) {
		if(name == null) {
			return "*";
		}

		if(removeIgnoredArticles) {
			if (ignoredArticles == null) {
				SharedPreferences prefs = Util.getPreferences(context);
				String ignoredArticlesString = prefs.getString(Constants.CACHE_KEY_IGNORE, "The El La Los Las Le Les");
				ignoredArticles = ignoredArticlesString.split(" ");
			}

			name = name.toLowerCase();
			for (String article : ignoredArticles) {
				int index = name.indexOf(article.toLowerCase() + " ");
				if (index == 0) {
					name = name.substring(article.length() + 1);
				}
			}
		}

		String index = name.substring(0, 1).toUpperCase();
		if (!Character.isLetter(index.charAt(0))) {
			index = "#";
		}

		return index;
	}

	public interface OnItemClickedListener<T> {
		void onItemClicked(UpdateView<T> updateView, T item);
		void onCreateContextMenu(Menu menu, MenuInflater menuInflater, UpdateView<T> updateView, T item);
		boolean onContextItemSelected(MenuItem menuItem, UpdateView<T> updateView, T item);
	}
}