-
Notifications
You must be signed in to change notification settings - Fork 49
/
ThreadsAdapter.java
338 lines (307 loc) · 11.7 KB
/
ThreadsAdapter.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
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
package com.mishiranu.dashchan.ui.navigator.adapter;
import android.content.Context;
import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.RecyclerView;
import chan.content.Chan;
import chan.util.StringUtils;
import com.mishiranu.dashchan.content.Preferences;
import com.mishiranu.dashchan.content.model.AttachmentItem;
import com.mishiranu.dashchan.content.model.GalleryItem;
import com.mishiranu.dashchan.content.model.PostItem;
import com.mishiranu.dashchan.ui.navigator.manager.UiManager;
import com.mishiranu.dashchan.ui.navigator.manager.ViewUnit;
import com.mishiranu.dashchan.util.AnimationUtils;
import com.mishiranu.dashchan.util.ListViewUtils;
import com.mishiranu.dashchan.util.ResourceUtils;
import com.mishiranu.dashchan.widget.DividerItemDecoration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
public class ThreadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements GalleryItem.Provider {
public interface Callback extends ListViewUtils.SimpleCallback<PostItem> {}
private static final int LIST_PADDING = 12;
private static final int CARD_MIN_WIDTH_LARGE_DP = 120;
private static final int CARD_MIN_WIDTH_SMALL_DP = 90;
private static final int CARD_PADDING_OUT_DP = 8;
private static final int CARD_PADDING_IN_DP = 4;
private static final int CARD_PADDING_IN_EXTRA_DP = 1;
private static class GridMode {
public final int columns;
public final boolean small;
public final int gridItemContentHeight;
private GridMode(int columns, boolean small, int gridItemContentHeight) {
this.columns = columns;
this.small = small;
this.gridItemContentHeight = gridItemContentHeight;
}
}
private final ArrayList<PostItem> postItems = new ArrayList<>();
private ArrayList<PostItem> catalogSortedPostItems;
private ArrayList<PostItem> filteredPostItems;
private boolean catalog;
private final Context context;
private final UiManager uiManager;
private final UiManager.ConfigurationSet configurationSet;
private String filterText;
private Preferences.CatalogSort catalogSort = Preferences.CatalogSort.UNSORTED;
private boolean cardsMode;
private GridMode gridMode;
public ThreadsAdapter(Context context, Callback callback, String chanName, UiManager uiManager,
UiManager.PostStateProvider postStateProvider, FragmentManager fragmentManager) {
this.context = context;
this.uiManager = uiManager;
configurationSet = new UiManager.ConfigurationSet(chanName, null, null, postStateProvider,
this, fragmentManager, uiManager.dialog().createStackInstance(), null, callback,
false, false, false, false, false, null);
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return uiManager.view().createView(parent, ViewUnit.ViewType.values()[viewType]);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
onBindViewHolder(holder, position, Collections.emptyList());
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder,
int position, @NonNull List<Object> payloads) {
PostItem postItem = getItem(position);
switch (ViewUnit.ViewType.values()[holder.getItemViewType()]) {
case THREAD:
case THREAD_CARD: {
if (payloads.isEmpty()) {
uiManager.view().bindThreadView(holder, postItem, configurationSet);
} else {
for (Object object : payloads) {
if (object instanceof AttachmentItem) {
uiManager.view().bindThreadViewReloadAttachment(holder, (AttachmentItem) object);
}
}
}
break;
}
case THREAD_HIDDEN:
case THREAD_CARD_HIDDEN: {
uiManager.view().bindThreadHiddenView(holder, postItem, configurationSet);
break;
}
case THREAD_CARD_CELL: {
if (payloads.isEmpty()) {
uiManager.view().bindThreadCellView(holder, postItem, configurationSet,
gridMode.small, gridMode.gridItemContentHeight);
} else {
for (Object object : payloads) {
if (object instanceof AttachmentItem) {
uiManager.view().bindThreadViewReloadAttachment(holder, (AttachmentItem) object);
}
}
}
break;
}
}
}
public UiManager.ConfigurationSet getConfigurationSet() {
return configurationSet;
}
@Override
public GalleryItem.Set getGallerySet(PostItem postItem) {
return postItem.getThreadGallerySet();
}
public boolean isRealEmpty() {
return postItems.isEmpty();
}
@Override
public int getItemViewType(int position) {
PostItem postItem = getItem(position);
return (gridMode != null ? ViewUnit.ViewType.THREAD_CARD_CELL
: configurationSet.postStateProvider.isHiddenResolve(postItem)
? (cardsMode ? ViewUnit.ViewType.THREAD_CARD_HIDDEN : ViewUnit.ViewType.THREAD_HIDDEN)
: (cardsMode ? ViewUnit.ViewType.THREAD_CARD : ViewUnit.ViewType.THREAD)).ordinal();
}
private List<PostItem> getPostItems() {
return filteredPostItems != null ? filteredPostItems : catalogSortedPostItems != null
? catalogSortedPostItems : postItems;
}
private PostItem getItem(int position) {
return getPostItems().get(position);
}
@Override
public int getItemCount() {
return getPostItems().size();
}
public void applyItemPadding(View view, int position, int column, Rect rect) {
float density = ResourceUtils.obtainDensity(view);
int paddingOut = (int) (CARD_PADDING_OUT_DP * density);
int paddingIn = (int) (CARD_PADDING_IN_DP * density);
if (!cardsMode) {
rect.set(0, 0, 0, 0);
} else {
int columns = gridMode != null ? gridMode.columns : 1;
int left;
int right;
if (columns >= 2) {
int paddingInExtra = (int) ((CARD_PADDING_IN_DP + CARD_PADDING_IN_EXTRA_DP) * density);
int total = 2 * paddingOut + (columns - 1) * paddingInExtra;
float average = (float) total / columns;
left = (int) AnimationUtils.lerp(paddingOut, average - paddingOut, (float) column / (columns - 1));
right = (int) average - left;
} else {
left = paddingOut;
right = paddingOut;
}
boolean firstRow = position - column == 0;
boolean lastRow = position + columns - column >= getItemCount();
rect.set(left, firstRow ? paddingOut : paddingIn, right, lastRow ? paddingOut : 0);
}
}
public DividerItemDecoration.Configuration configureDivider
(DividerItemDecoration.Configuration configuration, int position) {
if (cardsMode) {
return configuration.need(false);
} else {
PostItem current = getItem(position);
PostItem next = position + 1 < getItemCount() ? getItem(position + 1) : null;
float density = ResourceUtils.obtainDensity(context);
float scale = Preferences.getThumbnailsScale();
int padding = (int) (LIST_PADDING * density);
int imagePadding = (int) ((10 + 64 * scale + 10) * density);
boolean currentImage = current.hasAttachments() &&
!configurationSet.postStateProvider.isHiddenResolve(current);
boolean nextImage = next != null && next.hasAttachments() &&
!configurationSet.postStateProvider.isHiddenResolve(next);
return configuration.need(true).horizontal(currentImage && nextImage ? imagePadding : padding, padding);
}
}
public void setItems(Collection<List<PostItem>> postItemsCollection, boolean catalog) {
postItems.clear();
for (List<PostItem> postItems : postItemsCollection) {
appendItemsInternal(postItems);
}
this.catalog = catalog;
applyCurrentSortingAndFilter(true, true);
notifyDataSetChanged();
}
public void appendItems(List<PostItem> postItems) {
appendItemsInternal(postItems);
applyCurrentSortingAndFilter(true, true);
notifyDataSetChanged();
}
public void notifyNotModified() {
for (PostItem postItem : postItems) {
postItem.setHidden(PostItem.HideState.UNDEFINED, null);
}
notifyDataSetChanged();
}
private void appendItemsInternal(List<PostItem> postItems) {
boolean displayHidden = Preferences.isDisplayHiddenThreads();
if (postItems != null) {
for (PostItem postItem : postItems) {
if (displayHidden || !configurationSet.postStateProvider.isHiddenResolve(postItem)) {
this.postItems.add(postItem);
}
}
}
}
public void applyFilter(String text) {
if (!StringUtils.emptyIfNull(filterText).equals(StringUtils.emptyIfNull(text))) {
filterText = text;
applyCurrentSortingAndFilter(false, true);
notifyDataSetChanged();
}
}
public void setCatalogSort(Preferences.CatalogSort catalogSort) {
if (this.catalogSort != catalogSort) {
this.catalogSort = catalogSort;
if (catalog) {
applyCurrentSortingAndFilter(true, false);
notifyDataSetChanged();
}
}
}
private void applyCurrentSortingAndFilter(boolean sorting, boolean filter) {
if (sorting) {
Comparator<Preferences.CatalogSort.Comparable> comparator =
catalogSort != null ? catalogSort.comparator : null;
if (catalog && comparator != null) {
if (catalogSortedPostItems == null) {
catalogSortedPostItems = new ArrayList<>(postItems);
} else {
catalogSortedPostItems.clear();
catalogSortedPostItems.addAll(postItems);
}
Collections.sort(catalogSortedPostItems, comparator);
} else {
catalogSortedPostItems = null;
}
}
if (sorting || filter) {
String text = filterText;
if (!StringUtils.isEmpty(text)) {
if (filteredPostItems == null) {
filteredPostItems = new ArrayList<>();
} else {
filteredPostItems.clear();
}
text = text.toLowerCase(Locale.getDefault());
Chan chan = Chan.get(configurationSet.chanName);
Locale locale = Locale.getDefault();
for (PostItem postItem : (catalogSortedPostItems != null ? catalogSortedPostItems : postItems)) {
boolean add = postItem.getSubject().toLowerCase(locale).contains(text) ||
postItem.getComment(chan).toString().toLowerCase(locale).contains(text);
if (add) {
filteredPostItems.add(postItem);
}
}
} else {
filteredPostItems = null;
}
}
}
public void reloadAttachment(AttachmentItem attachmentItem) {
for (int i = 0; i < getItemCount(); i++) {
PostItem postItem = getItem(i);
if (postItem.getPostNumber().equals(attachmentItem.getPostNumber())) {
notifyItemChanged(i, attachmentItem);
}
}
}
private static int calculateColumnsCount(int totalWidth, int minWidth, int paddingOut, int paddingInExtra) {
return Math.min(Math.max(1, (totalWidth - 2 * paddingOut + paddingInExtra)
/ (minWidth + paddingInExtra)), 6);
}
public int setThreadsView(Preferences.ThreadsView threadsView) {
if (threadsView == Preferences.ThreadsView.LARGE_GRID ||
threadsView == Preferences.ThreadsView.SMALL_GRID) {
float density = ResourceUtils.obtainDensity(context);
int totalWidth = (int) (context.getResources().getConfiguration().screenWidthDp * density);
int minWidthSmall = (int) (CARD_MIN_WIDTH_SMALL_DP * density);
int minWidthLarge = (int) (CARD_MIN_WIDTH_LARGE_DP * density);
int paddingOut = (int) (CARD_PADDING_OUT_DP * density);
int paddingInExtra = (int) ((CARD_PADDING_IN_DP + CARD_PADDING_IN_EXTRA_DP) * density);
int smallColumns = calculateColumnsCount(totalWidth, minWidthSmall, paddingOut, paddingInExtra);
int largeColumns = calculateColumnsCount(totalWidth, minWidthLarge, paddingOut, paddingInExtra);
if (smallColumns == largeColumns) {
smallColumns++;
}
boolean small = threadsView == Preferences.ThreadsView.SMALL_GRID;
int columns = small ? smallColumns : largeColumns;
int contentWidth = (totalWidth - 2 * paddingOut - (columns - 1) * paddingInExtra) / columns;
int contentHeight = (int) (contentWidth * (small ? 1.35f : 1.5f));
cardsMode = true;
gridMode = new GridMode(columns, small, contentHeight);
return columns;
} else {
cardsMode = threadsView == Preferences.ThreadsView.CARDS;
gridMode = null;
return 1;
}
}
}