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 3c10048252ca4e2cc54549019eac13f6913cc7a2..00a31678ba994fc8e222c6a060e17180f2945b78 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 |
| @@ -149,10 +149,23 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| } |
| } |
| + protected static class SubsectionHeaderViewHolder extends RecyclerView.ViewHolder { |
| + private View mView; |
| + |
| + public SubsectionHeaderViewHolder(View itemView) { |
| + super(itemView); |
| + mView = itemView; |
| + } |
| + |
| + public View getView() { |
| + return mView; |
| + } |
| + } |
| + |
| /** |
| * A bucket of items with the same date. |
| */ |
| - protected static class ItemGroup { |
| + public static class ItemGroup { |
|
gone
2017/02/15 23:58:10
Make this protected again.
shaktisahu
2017/02/16 06:07:07
I am subclassing this from DownloadItemGroup which
|
| private final Date mDate; |
| private final List<TimedItem> mItems = new ArrayList<>(); |
| @@ -227,26 +240,30 @@ 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; |
| + } |
| + } |
| } |
| // Cached async tasks to get the two Calendar objects, which are used when comparing dates. |
| @@ -257,6 +274,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_SUBSECTION_HEADER = 2; |
| private int mSize; |
| private boolean mHasListHeader; |
| @@ -282,6 +300,14 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| protected abstract ViewHolder createViewHolder(ViewGroup parent); |
| /** |
| + * Creates a {@link ViewHolder} for a subsection in the given view parent. |
| + * @see #onCreateViewHolder(ViewGroup, int) |
| + */ |
| + protected SubsectionHeaderViewHolder createSubsectionHeader(ViewGroup parent) { |
|
gone
2017/02/15 23:58:10
@Nullable?
shaktisahu
2017/02/16 06:07:07
Not sure if it adds any value. Each of the followi
gone
2017/02/17 19:21:16
Yeah, they really should be.
shaktisahu
2017/02/17 19:55:10
Done.
|
| + return null; |
| + } |
| + |
| + /** |
| * Creates a {@link BasicViewHolder} in the given view parent for the header. |
| * @see #onCreateViewHolder(ViewGroup, int) |
| */ |
| @@ -313,11 +339,22 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| protected abstract void bindViewHolderForTimedItem(ViewHolder viewHolder, TimedItem item); |
| /** |
| + * Binds the {@link SubsectionHeaderViewHolder} with the given {@link TimedItem}. |
| + * @see #onBindViewHolder(ViewHolder, int) |
| + */ |
| + protected void bindViewHolderForSubsectionHeader( |
| + SubsectionHeaderViewHolder holder, TimedItem timedItem) {} |
| + |
| + /** |
| * Gets the resource id of the view showing the date header. |
| * Contract for subclasses: this view should be a {@link TextView}. |
| */ |
| protected abstract int getTimedItemViewResId(); |
| + protected boolean isSubsectionHeader(TimedItem timedItem) { |
| + return false; |
| + } |
|
gone
2017/02/15 23:58:10
Group your subsection header-related functions tog
shaktisahu
2017/02/16 06:07:07
Done.
|
| + |
| /** |
| * Loads a list of {@link TimedItem}s to this adapter. Previous data will not be removed. Call |
| * {@link #clear(boolean)} to remove previous items. |
| @@ -337,7 +374,7 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| 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; |
| @@ -348,6 +385,10 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| notifyDataSetChanged(); |
| } |
| + protected ItemGroup createGroup(long timestamp) { |
|
gone
2017/02/15 23:58:10
Javadoc for protected & public methods
shaktisahu
2017/02/16 06:07:06
Done.
|
| + return new ItemGroup(timestamp); |
| + } |
| + |
| /** |
| * Tells each group where they start in the list. |
| */ |
| @@ -457,19 +498,22 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| @Override |
| public final int getItemViewType(int position) { |
| Pair<ItemGroup, Integer> pair = getGroupAt(position); |
| + ItemGroup group = pair.first; |
| if (pair.second == TYPE_HEADER) { |
| return TYPE_HEADER; |
| } else if (pair.second == TYPE_FOOTER) { |
| return TYPE_FOOTER; |
| } else if (pair.second == 0) { |
| return TYPE_DATE; |
| + } else if (isSubsectionHeader(group.getItemAt(pair.second))) { |
| + return TYPE_SUBSECTION_HEADER; |
| } else { |
| return TYPE_NORMAL; |
| } |
| } |
| @Override |
| - public final RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| + public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
|
gone
2017/02/15 23:58:10
Make this function final again
shaktisahu
2017/02/16 06:07:06
Done.
|
| if (viewType == TYPE_DATE) { |
| return createDateViewHolder(parent); |
| } else if (viewType == TYPE_NORMAL) { |
| @@ -478,16 +522,20 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
| return createHeader(parent); |
| } else if (viewType == TYPE_FOOTER) { |
| return createFooter(parent); |
| + } else if (viewType == TYPE_SUBSECTION_HEADER) { |
| + return createSubsectionHeader(parent); |
| } |
| assert false; |
| return null; |
| } |
| @Override |
| - public final void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| + public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
|
gone
2017/02/15 23:58:10
Make this function final again
shaktisahu
2017/02/16 06:07:07
Done.
|
| Pair<Date, TimedItem> pair = getItemAt(position); |
| if (holder instanceof DateViewHolder) { |
| ((DateViewHolder) holder).setDate(pair.first); |
| + } else if (holder instanceof SubsectionHeaderViewHolder) { |
| + bindViewHolderForSubsectionHeader((SubsectionHeaderViewHolder) holder, pair.second); |
| } else if (!(holder instanceof BasicViewHolder)) { |
| bindViewHolderForTimedItem(holder, pair.second); |
| } |
| @@ -563,7 +611,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); |