forked from Mobile4You/MobileScripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSectionAdapter.java
301 lines (240 loc) · 9.83 KB
/
SectionAdapter.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
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
public class SectionAdapter<Section, Item, SectionItemView extends View> extends RecyclerView.Adapter<SectionAdapter.SectionViewHolder> {
private final Context context;
private List<Section> sections;
private List<Section> blacklist;
private LayoutInflater inflater;
private Section filter;
@Nullable
private Map<Section, List<Item>> data;
private Func1<Section, String> sectionMapper;
private Func2<Item, ItemPosition, SectionItemView> dataBinder;
private Action3<SectionItemView, Item, ItemPosition> dataRebinder;
private Action1<Item> onItemClick;
private Action0 onSectionOpened;
private Action0 onDataAvailable;
private Action0 onNoDataAvailable;
private Map<Section, Boolean> collapsedState;
private Map<Section, Boolean> loadingState;
private PublishSubject<CollapseEvent> sectionCollapseStatusStream = PublishSubject.create();
private Handler handler = new Handler(Looper.getMainLooper());
private boolean mIsLoadingItems;
public SectionAdapter(Context context) {
this(context, null, null);
}
public SectionAdapter(Context context, List<Section> excludedStatuses) {
this(context, null, excludedStatuses);
}
public SectionAdapter(Context context, Map<Section, List<Item>> data) {
this(context, data, null);
}
public SectionAdapter(Context context, @Nullable Map<Section, List<Item>> data, List<Section> blacklist) {
this.context = context;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.data = data;
this.blacklist = blacklist;
this.sections = new ArrayList<>();
validateItems(data);
}
@Override
public int getItemCount() {
if (data == null)
return 0;
if (filterIsValid()) {
boolean hasFilteredData = data.get(filter).size() > 0;
return hasFilteredData ? 1 : 0;
} else {
return sections.size();
}
}
private boolean filterIsValid() {
return filter != null && data != null && data.get(filter) != null;
}
@Override
public SectionViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.grouped_order_row, parent, false);
return new SectionViewHolder(view);
}
@Override
public void onBindViewHolder(SectionAdapter.SectionViewHolder holder, int position) {
Section section = filterIsValid() ? filter : sections.get(position);
List<Item> items = null;
if (data != null) {
items = data.get(section);
}
holder.setSection(section);
holder.setItems(items);
}
public void setOnItemClick(Action1<Item> onItemClick) {
this.onItemClick = onItemClick;
}
public void setOnSectionOpened(Action0 onSectionOpened) {
this.onSectionOpened = onSectionOpened;
}
public void setData(@Nullable Map<Section, List<Item>> sectionedItems, Map<Section, Boolean> collapsedState) {
this.data = sectionedItems;
this.collapsedState = collapsedState;
new Thread(() -> {
validateItems(sectionedItems);
handler.post(this::notifyDataSetChanged);
}).start();
}
@Override
public long getItemId(int position) {
return position;
}
public void setDataBinder(Func2<Item, ItemPosition, SectionItemView> dataBinder) {
this.dataBinder = dataBinder;
}
public void setDataRebinder(Action3<SectionItemView, Item, ItemPosition> dataRebinder) {
this.dataRebinder = dataRebinder;
}
public void setSectionMapper(Func1<Section, String> sectionMapper) {
this.sectionMapper = sectionMapper;
}
public void setFilter(Section filter) {
this.filter = filter;
notifyDataSetChanged();
}
private void validateItems(Map<Section, List<Item>> data) {
if (data == null) {
return;
}
if (loadingState != null) {
for (boolean isLoading : loadingState.values()) {
if (isLoading) {
return;
}
}
}
if (blacklist != null) {
for (Section excludedSection : blacklist) {
data.remove(excludedSection);
}
}
sections = new ArrayList<>();
loadingState = new HashMap<>();
for (Section section : data.keySet()) {
if (data.get(section).size() > 0) {
sections.add(section);
loadingState.put(section, false);
}
}
if (sections.size() > 0) {
if (onDataAvailable != null) {
handler.post(onDataAvailable::call);
}
} else {
if (onNoDataAvailable != null) {
handler.post(onNoDataAvailable::call);
}
}
}
public void subscribeSectionCollapseEvents(Action1<CollapseEvent> action) {
sectionCollapseStatusStream.subscribe(action);
}
public void setOnDataAvailable(Action0 action0) {
this.onDataAvailable = action0;
}
public void setOnNoDataAvailable(Action0 action0) {
this.onNoDataAvailable = action0;
}
public enum ItemPosition {
FIRST, MIDDLE, LAST
}
class SectionViewHolder extends RecyclerView.ViewHolder {
private View mViewSectionHeader;
private TextView mTextViewSectionHeaderTitle;
private ImageView mImageViewSectionHeaderEndIcon;
private ViewGroup mViewGroupSectionItems;
private boolean mCollapsed = true;
private Section mSection;
public SectionViewHolder(View itemView) {
super(itemView);
mViewSectionHeader = itemView.findViewById(R.id.section_header);
mTextViewSectionHeaderTitle = (TextView) itemView.findViewById(R.id.section_header_title);
mImageViewSectionHeaderEndIcon = (ImageView) itemView.findViewById(R.id.section_header_end_icon);
mViewGroupSectionItems = (ViewGroup) itemView.findViewById(R.id.section_items_container);
}
public void setSection(Section section) {
String groupTitle;
mSection = section;
if (sectionMapper != null) {
groupTitle = sectionMapper.call(section);
} else {
groupTitle = section.toString();
}
if (collapsedState != null) {
mCollapsed = collapsedState.get(section) != null ? collapsedState.get(section) : mCollapsed;
}
mTextViewSectionHeaderTitle.setText(groupTitle);
mViewSectionHeader.setOnClickListener(v -> {
mCollapsed = !mCollapsed;
dealWithSectionCollapseStatus(mCollapsed);
sectionCollapseStatusStream.onNext(new CollapseEvent(section, mCollapsed));
if (!mCollapsed && onSectionOpened != null) {
onSectionOpened.call();
}
});
dealWithSectionCollapseStatus(mCollapsed);
}
private void dealWithSectionCollapseStatus(Boolean collapsed) {
mViewGroupSectionItems.setVisibility(collapsed ? View.GONE : View.VISIBLE);
mImageViewSectionHeaderEndIcon.setImageResource(collapsed ? R.drawable.ic_expand_more : R.drawable.ic_expand_less);
}
public void setItems(@Nullable List<Item> items) {
loadingState.put(mSection, true);
new Thread(() -> {
if (dataBinder != null && dataRebinder != null && items != null) {
int childCount = mViewGroupSectionItems.getChildCount();
int itemsSize = items.size();
while (childCount > itemsSize) {
final int viewToRemoveIndex = childCount - 1;
handler.post(() -> mViewGroupSectionItems.removeViewAt(viewToRemoveIndex));
childCount--;
}
for (int i = 0; i < items.size(); i++) {
Item item = items.get(i);
ItemPosition itemPosition;
if (i == items.size() - 1) {
itemPosition = ItemPosition.LAST;
} else if (i == 0) {
itemPosition = ItemPosition.FIRST;
} else {
itemPosition = ItemPosition.MIDDLE;
}
SectionItemView child = (SectionItemView) mViewGroupSectionItems.getChildAt(i);
if (child == null) {
final View newChild = dataBinder.call(item, itemPosition);
if (newChild == null)
return;
handler.post(() -> {
mViewGroupSectionItems.addView(newChild);
newChild.setOnClickListener(v -> onItemClick.call(item));
});
} else {
handler.post(() -> {
dataRebinder.call(child, item, itemPosition);
child.setOnClickListener(v -> onItemClick.call(item));
});
}
}
}
loadingState.put(mSection, false);
}).start();
}
}
public class CollapseEvent {
Section section;
Boolean collapsed;
CollapseEvent(Section section, boolean collapsed) {
this.section = section;
this.collapsed = collapsed;
}
public Section getSection() {
return section;
}
public Boolean isCollapsed() {
return collapsed;
}
}
}