Chromium Code Reviews| Index: chrome/android/java/src/org/chromium/chrome/browser/widget/DateDividedAdapter.java |
| diff --git a/chrome/android/java/src/org/chromium/chrome/browser/widget/DateDividedAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/widget/DateDividedAdapter.java |
| index ee707031eb1d11075098289c351d2c33d21edbf3..347c7cc9e2f4ad6b020a8437ba4084ada056415f 100644 |
| --- a/chrome/android/java/src/org/chromium/chrome/browser/widget/DateDividedAdapter.java |
| +++ b/chrome/android/java/src/org/chromium/chrome/browser/widget/DateDividedAdapter.java |
| @@ -16,7 +16,6 @@ import android.view.ViewGroup; |
| import android.widget.TextView; |
| import org.chromium.chrome.R; |
| -import org.chromium.chrome.browser.widget.DateDividedAdapter.ItemGroup; |
| import java.util.ArrayList; |
| import java.util.Calendar; |
| @@ -50,7 +49,7 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| private int mPosition = INVALID_POSITION; |
| /** See {@link #mPosition}. */ |
| - private final void setPosition(int position) { |
| + public final void setPosition(int position) { |
| mPosition = position; |
| } |
| @@ -69,6 +68,11 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| * @return ID that can uniquely identify the TimedItem. |
| */ |
| public abstract long getStableId(); |
| + |
| + /** @return Whether this item is an offline page. */ |
| + public boolean isOfflinePage() { |
|
gone
2017/02/02 20:49:55
This class should really have no idea what an offl
shaktisahu
2017/02/02 23:29:38
Okay. In that case, I can move this function to Do
|
| + return false; |
| + } |
| } |
| private static class DateViewHolder extends RecyclerView.ViewHolder { |
| @@ -115,9 +119,9 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| /** |
| * A bucket of items with the same date. |
| */ |
| - protected static class ItemGroup { |
| + public static class ItemGroup { |
| private final Date mDate; |
| - private final List<TimedItem> mItems = new ArrayList<>(); |
| + protected final List<TimedItem> mItems = new ArrayList<>(); |
| /** Index of the header, relative to the full list. Must be set only once.*/ |
| private int mIndex; |
| @@ -145,9 +149,14 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| mIndex = index; |
| sortIfNeeded(); |
| + setPositionForItems(index + 1); |
| + } |
| + |
| + protected void setPositionForItems(int startIndex) { |
| + int index = startIndex; |
| for (TimedItem item : mItems) { |
| - index += 1; |
| item.setPosition(index); |
| + index += 1; |
| } |
| } |
| @@ -164,9 +173,14 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| return compareDate(mDate, otherDate) == 0; |
| } |
| - /** |
| - * @return The size of this group. |
| - */ |
|
gone
2017/02/02 23:59:48
Why'd you remove this?
shaktisahu
2017/02/04 18:57:43
Done. Was accidentally removed.
|
| + protected boolean isListHeaderOrFooter() { |
| + return mIsListHeaderOrFooter; |
| + } |
| + |
| + protected Date getDate() { |
| + return mDate; |
| + } |
| + |
| public int size() { |
| if (mIsListHeaderOrFooter) return 1; |
| @@ -186,26 +200,34 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| * Rather than sorting the list each time a new item is added, the list is sorted when |
| * something requires a correct ordering of the items. |
| */ |
| - private void sortIfNeeded() { |
| + protected void sortIfNeeded() { |
| if (mIsSorted) return; |
| mIsSorted = true; |
| Collections.sort(mItems, new Comparator<TimedItem>() { |
| @Override |
| public int compare(TimedItem lhs, TimedItem rhs) { |
| - // More recent items are listed first. Ideally we'd use Long.compare, but that |
| - // is an API level 19 call for some inexplicable reason. |
| - long timeDelta = lhs.getTimestamp() - rhs.getTimestamp(); |
| - if (timeDelta > 0) { |
| - return -1; |
| - } else if (timeDelta == 0) { |
| - return 0; |
| - } else { |
| - return 1; |
| - } |
| + return compareItem(lhs, rhs); |
| } |
| }); |
| } |
| + |
| + protected int compareItem(TimedItem lhs, TimedItem rhs) { |
| + // More recent items are listed first. Ideally we'd use Long.compare, but that |
| + // is an API level 19 call for some inexplicable reason. |
| + long timeDelta = lhs.getTimestamp() - rhs.getTimestamp(); |
| + if (timeDelta > 0) { |
| + return -1; |
| + } else if (timeDelta == 0) { |
| + return 0; |
| + } else { |
| + return 1; |
| + } |
| + } |
| + |
| + public int getItemViewType(int position) { |
| + return TYPE_NORMAL; |
| + } |
| } |
| // Cached async tasks to get the two Calendar objects, which are used when comparing dates. |
| @@ -216,6 +238,7 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| public static final int TYPE_HEADER = -1; |
| public static final int TYPE_DATE = 0; |
| public static final int TYPE_NORMAL = 1; |
| + public static final int TYPE_OFFLINE_HEADER = 2; |
|
gone
2017/02/02 23:59:48
automatic offline header or something
shaktisahu
2017/02/04 18:57:43
Done.
TYPE_SUGGESTED_OFFLINE_PAGES_HEADER
|
| private int mSize; |
| private boolean mHasListHeader; |
| @@ -274,28 +297,32 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| if (group.isSameDay(date)) { |
| found = true; |
| group.addItem(timedItem); |
| - mSize++; |
| break; |
| } |
| } |
| if (!found) { |
| // Create a new ItemGroup with the date for the new item. This increases the |
| // size by two because we add new views for the date and the item itself. |
| - ItemGroup newGroup = new ItemGroup(timedItem.getTimestamp()); |
| + ItemGroup newGroup = createGroup(timedItem.getTimestamp()); |
| newGroup.addItem(timedItem); |
| mGroups.add(newGroup); |
| - mSize += 2; |
| } |
| } |
| + computeItemCount(); |
| setGroupPositions(); |
| notifyDataSetChanged(); |
| } |
| + protected ItemGroup createGroup(long timeStamp) { |
| + ItemGroup group = new ItemGroup(timeStamp); |
| + return group; |
| + } |
| + |
| /** |
| * Tells each group where they start in the list. |
| */ |
| - private void setGroupPositions() { |
| + protected void setGroupPositions() { |
| int startIndex = 0; |
| for (ItemGroup group : mGroups) { |
| group.resetPosition(); |
| @@ -408,12 +435,13 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| } else if (pair.second == 0) { |
| return TYPE_DATE; |
| } else { |
| - return TYPE_NORMAL; |
| + ItemGroup group = pair.first; |
| + return group.getItemViewType(pair.second); |
| } |
| } |
| @Override |
| - public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| if (viewType == TYPE_DATE) { |
| return new DateViewHolder(LayoutInflater.from(parent.getContext()).inflate( |
| getTimedItemViewResId(), parent, false)); |
| @@ -429,7 +457,7 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| } |
| @Override |
| - public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| Pair<Date, TimedItem> pair = getItemAt(position); |
| if (holder instanceof DateViewHolder) { |
| ((DateViewHolder) holder).setDate(pair.first); |
| @@ -464,6 +492,8 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| return new Pair<>(group, i); |
| } |
| } |
| + System.out.println( |
| + ("shakti, position " + position + " groups " + mGroups.size() + " msize " + mSize)); |
| assert false; |
| return null; |
| } |
| @@ -474,18 +504,25 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| protected void removeItem(TimedItem item) { |
| ItemGroup group = getGroupAt(item.getPosition()).first; |
| group.removeItem(item); |
| - mSize--; |
| // Remove the group if only the date header is left. |
| if (group.size() == 1) { |
| mGroups.remove(group); |
| - mSize--; |
| } |
| + // Somewhat inefficient if number of groups is large. |
|
gone
2017/02/02 23:59:48
Can't you recompute the size based on the item/gro
|
| + computeItemCount(); |
| setGroupPositions(); |
| notifyDataSetChanged(); |
| } |
| + protected void computeItemCount() { |
| + mSize = 0; |
| + for (ItemGroup group : mGroups) { |
| + mSize += group.size(); |
| + } |
| + } |
| + |
| /** |
| * Creates a long ID that identifies a particular day in history. |
| * @param date Date to process. |
| @@ -506,7 +543,7 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| * {@link #compareCalendar(Calendar, Calendar)} instead. |
| * @return 0 if date1 and date2 are in the same day; 1 if date1 is before date2; -1 otherwise. |
| */ |
| - private static int compareDate(Date date1, Date date2) { |
| + protected static int compareDate(Date date1, Date date2) { |
| Pair<Calendar, Calendar> pair = getCachedCalendars(); |
| Calendar cal1 = pair.first, cal2 = pair.second; |
| cal1.setTime(date1); |