aboutsummaryrefslogtreecommitdiff
path: root/app/src/main/java/github/daneren2005/dsub/adapter/ExpandableSectionAdapter.java
blob: 6fc41b1c0cc2c1aaf2e5dee76fb6327703cafec4 (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
/*
  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.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import github.daneren2005.dsub.R;
import github.daneren2005.dsub.util.DrawableTint;
import github.daneren2005.dsub.view.BasicHeaderView;
import github.daneren2005.dsub.view.UpdateView;

public abstract class ExpandableSectionAdapter<T> extends SectionAdapter<T> {
	private static final int DEFAULT_VISIBLE = 4;
	private static final int EXPAND_TOGGLE = R.attr.select_server;
	private static final int COLLAPSE_TOGGLE = R.attr.select_tabs;

	protected List<Integer> sectionsDefaultVisible;
	protected List<List<T>> sectionsExtras;
	protected int expandToggleRes;
	protected int collapseToggleRes;

	protected ExpandableSectionAdapter() {}
	public ExpandableSectionAdapter(Context context, List<T> section) {
		List<List<T>> sections = new ArrayList<>();
		sections.add(section);

		init(context, Collections.singletonList("Section"), sections, Collections.singletonList((Integer) null));
	}
	public ExpandableSectionAdapter(Context context, List<String> headers, List<List<T>> sections, List<Integer> sectionsDefaultVisible) {
		init(context, headers, sections, sectionsDefaultVisible);
	}
	protected void init(Context context, List<String> headers, List<List<T>> fullSections, List<Integer> sectionsDefaultVisible) {
		this.context = context;
		this.headers = headers;
		this.sectionsDefaultVisible = sectionsDefaultVisible;
		if(sectionsDefaultVisible == null) {
			sectionsDefaultVisible = new ArrayList<>(fullSections.size());
			for(int i = 0; i < fullSections.size(); i++) {
				sectionsDefaultVisible.add(DEFAULT_VISIBLE);
			}
		}

		this.sections = new ArrayList<>();
		this.sectionsExtras = new ArrayList<>();
		int i = 0;
		for(List<T> fullSection: fullSections) {
			List<T> visibleSection = new ArrayList<>();

			Integer defaultVisible = sectionsDefaultVisible.get(i);
			if(defaultVisible == null || defaultVisible >= fullSection.size()) {
				visibleSection.addAll(fullSection);
				this.sectionsExtras.add(null);
			} else {
				visibleSection.addAll(fullSection.subList(0, defaultVisible));
				this.sectionsExtras.add(fullSection.subList(defaultVisible, fullSection.size()));
			}
			this.sections.add(visibleSection);

			i++;
		}

		expandToggleRes = DrawableTint.getDrawableRes(context, EXPAND_TOGGLE);
		collapseToggleRes = DrawableTint.getDrawableRes(context, COLLAPSE_TOGGLE);
	}

	@Override
	public UpdateView.UpdateViewHolder onCreateHeaderHolder(ViewGroup parent) {
		return new UpdateView.UpdateViewHolder(new BasicHeaderView(context, R.layout.expandable_header));
	}

	@Override
	public void onBindHeaderHolder(UpdateView.UpdateViewHolder holder, String header, final int sectionIndex) {
		UpdateView view = holder.getUpdateView();
		ImageView toggleSelectionView = (ImageView) view.findViewById(R.id.item_select);

		List<T> visibleSelection = sections.get(sectionIndex);
		List<T> sectionExtras = sectionsExtras.get(sectionIndex);

		if(sectionExtras != null && !sectionExtras.isEmpty()) {
			toggleSelectionView.setVisibility(View.VISIBLE);
			toggleSelectionView.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					List<T> visibleSelection = sections.get(sectionIndex);
					List<T> sectionExtras = sectionsExtras.get(sectionIndex);

					// Update icon
					int selectToggleAttr;
					if (!visibleSelection.contains(sectionExtras.get(0))) {
						selectToggleAttr = COLLAPSE_TOGGLE;

						// Update how many are displayed
						int lastIndex = getItemPosition(visibleSelection.get(visibleSelection.size() - 1));
						visibleSelection.addAll(sectionExtras);
						notifyItemRangeInserted(lastIndex, sectionExtras.size());
					} else {
						selectToggleAttr = EXPAND_TOGGLE;

						// Update how many are displayed
						visibleSelection.removeAll(sectionExtras);
						int lastIndex = getItemPosition(visibleSelection.get(visibleSelection.size() - 1));
						notifyItemRangeRemoved(lastIndex, sectionExtras.size());
					}

					((ImageView) v).setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
				}
			});

			int selectToggleAttr;
			if (!visibleSelection.contains(sectionExtras.get(0))) {
				selectToggleAttr = EXPAND_TOGGLE;
			} else {
				selectToggleAttr = COLLAPSE_TOGGLE;
			}

			toggleSelectionView.setImageResource(DrawableTint.getDrawableRes(context, selectToggleAttr));
		} else {
			toggleSelectionView.setVisibility(View.GONE);
		}

		view.setObject(header);
	}
}