-
Notifications
You must be signed in to change notification settings - Fork 215
/
HistoryAdapter.java
310 lines (253 loc) · 11.4 KB
/
HistoryAdapter.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
package org.mozilla.vrbrowser.ui.adapters;
import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.DiffUtil;
import androidx.recyclerview.widget.RecyclerView;
import org.mozilla.vrbrowser.R;
import org.mozilla.vrbrowser.databinding.HistoryItemBinding;
import org.mozilla.vrbrowser.databinding.HistoryItemHeaderBinding;
import org.mozilla.vrbrowser.ui.callbacks.HistoryItemCallback;
import org.mozilla.vrbrowser.ui.widgets.WidgetPlacement;
import org.mozilla.vrbrowser.utils.AnimationHelper;
import org.mozilla.vrbrowser.utils.SystemUtils;
import java.util.List;
import java.util.Objects;
import mozilla.components.concept.storage.VisitInfo;
import mozilla.components.concept.storage.VisitType;
public class HistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
static final String LOGTAG = SystemUtils.createLogtag(HistoryAdapter.class);
private static final int TYPE_HEADER = 0;
private static final int TYPE_ITEM = 1;
private static final int ICON_ANIMATION_DURATION = 200;
private List<? extends VisitInfo> mHistoryList;
private int mMinPadding;
private int mMaxPadding;
private boolean mIsNarrowLayout;
@Nullable
private final HistoryItemCallback mHistoryItemCallback;
public HistoryAdapter(@Nullable HistoryItemCallback clickCallback, Context aContext) {
mHistoryItemCallback = clickCallback;
mMinPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.library_icon_padding_min);
mMaxPadding = WidgetPlacement.pixelDimension(aContext, R.dimen.library_icon_padding_max);
mIsNarrowLayout = false;
setHasStableIds(true);
}
public void setNarrow(boolean isNarrow) {
if (mIsNarrowLayout != isNarrow) {
mIsNarrowLayout = isNarrow;
notifyDataSetChanged();
}
}
public void setHistoryList(final List<? extends VisitInfo> historyList) {
if (mHistoryList == null) {
mHistoryList = historyList;
notifyItemRangeInserted(0, historyList.size());
} else {
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return mHistoryList.size();
}
@Override
public int getNewListSize() {
return historyList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return mHistoryList.get(oldItemPosition).getVisitTime() == historyList.get(newItemPosition).getVisitTime();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
VisitInfo newHistoryItem = historyList.get(newItemPosition);
VisitInfo oldHistoryItem = mHistoryList.get(oldItemPosition);
return newHistoryItem.getVisitTime() == oldHistoryItem.getVisitTime()
&& Objects.equals(newHistoryItem.getTitle(), oldHistoryItem.getTitle())
&& Objects.equals(newHistoryItem.getUrl(), oldHistoryItem.getUrl());
}
});
mHistoryList = historyList;
result.dispatchUpdatesTo(this);
}
}
public void removeItem(VisitInfo historyItem) {
int position = mHistoryList.indexOf(historyItem);
if (position >= 0) {
mHistoryList.remove(position);
notifyItemRemoved(position);
}
}
public int itemCount() {
if (mHistoryList != null) {
return mHistoryList.stream().allMatch(item ->
item.getVisitType() == VisitType.NOT_A_VISIT) ?
0 :
mHistoryList.size();
}
return 0;
}
public int getItemPosition(long id) {
for (int position=0; position<mHistoryList.size(); position++)
if (mHistoryList.get(position).getVisitTime() == id)
return position;
return 0;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
if (viewType == TYPE_ITEM) {
HistoryItemBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.history_item,
parent, false);
binding.setCallback(mHistoryItemCallback);
binding.setIsHovered(false);
binding.setIsNarrow(mIsNarrowLayout);
binding.layout.setOnHoverListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
binding.setIsHovered(true);
return false;
case MotionEvent.ACTION_HOVER_EXIT:
binding.setIsHovered(false);
return false;
}
return false;
});
binding.layout.setOnTouchListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_UP:
return false;
case MotionEvent.ACTION_DOWN:
binding.more.setImageState(new int[]{android.R.attr.state_active},false);
binding.trash.setImageState(new int[]{android.R.attr.state_active},false);
binding.setIsHovered(true);
return false;
case MotionEvent.ACTION_CANCEL:
binding.setIsHovered(false);
return false;
}
return false;
});
binding.more.setOnHoverListener(mIconHoverListener);
binding.more.setOnTouchListener((view, motionEvent) -> {
binding.setIsHovered(true);
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_UP:
if (mHistoryItemCallback != null) {
mHistoryItemCallback.onMore(view, binding.getItem());
}
binding.more.setImageState(new int[]{android.R.attr.state_active},true);
return true;
case MotionEvent.ACTION_DOWN:
binding.more.setImageState(new int[]{android.R.attr.state_pressed},true);
return true;
case MotionEvent.ACTION_CANCEL:
binding.more.setImageState(new int[]{android.R.attr.state_active},true);
return false;
}
return false;
});
binding.trash.setOnHoverListener(mIconHoverListener);
binding.trash.setOnTouchListener((view, motionEvent) -> {
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_UP:
binding.setIsHovered(true);
if (mHistoryItemCallback != null) {
mHistoryItemCallback.onDelete(view, binding.getItem());
}
binding.trash.setImageState(new int[]{android.R.attr.state_active},true);
return true;
case MotionEvent.ACTION_DOWN:
binding.trash.setImageState(new int[]{android.R.attr.state_pressed},true);
return true;
case MotionEvent.ACTION_CANCEL:
binding.trash.setImageState(new int[]{android.R.attr.state_active},true);
return false;
}
return false;
});
return new HistoryItemViewHolder(binding);
} else if (viewType == TYPE_HEADER){
HistoryItemHeaderBinding binding = DataBindingUtil
.inflate(LayoutInflater.from(parent.getContext()), R.layout.history_item_header,
parent, false);
return new HistoryItemViewHeaderHolder(binding);
}
throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
if (holder instanceof HistoryItemViewHolder) {
HistoryItemViewHolder item = (HistoryItemViewHolder) holder;
item.binding.setItem(mHistoryList.get(position));
item.binding.setIsNarrow(mIsNarrowLayout);
} else if (holder instanceof HistoryItemViewHeaderHolder) {
HistoryItemViewHeaderHolder item = (HistoryItemViewHeaderHolder) holder;
item.binding.setTitle(mHistoryList.get(position).getTitle());
}
}
@Override
public int getItemCount() {
return mHistoryList == null ? 0 : mHistoryList.size();
}
@Override
public long getItemId(int position) {
VisitInfo historyItem = mHistoryList.get(position);
return historyItem.getVisitTime();
}
@Override
public int getItemViewType(int position) {
if (isPositionHeader(position))
return TYPE_HEADER;
return TYPE_ITEM;
}
private boolean isPositionHeader(int position) {
return mHistoryList.get(position).getVisitType() == VisitType.NOT_A_VISIT;
}
private View.OnHoverListener mIconHoverListener = (view, motionEvent) -> {
ImageView icon = (ImageView)view;
int ev = motionEvent.getActionMasked();
switch (ev) {
case MotionEvent.ACTION_HOVER_ENTER:
icon.setImageState(new int[]{android.R.attr.state_hovered},true);
AnimationHelper.animateViewPadding(view,
mMaxPadding,
mMinPadding,
ICON_ANIMATION_DURATION);
return false;
case MotionEvent.ACTION_HOVER_EXIT:
icon.setImageState(new int[]{android.R.attr.state_active},true);
AnimationHelper.animateViewPadding(view,
mMinPadding,
mMaxPadding,
ICON_ANIMATION_DURATION);
return false;
}
return false;
};
static class HistoryItemViewHolder extends RecyclerView.ViewHolder {
final HistoryItemBinding binding;
HistoryItemViewHolder(@NonNull HistoryItemBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
static class HistoryItemViewHeaderHolder extends RecyclerView.ViewHolder {
final HistoryItemHeaderBinding binding;
HistoryItemViewHeaderHolder(@NonNull HistoryItemHeaderBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
}
}