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..d987bc90e976c3a6262d9052fcf545dc62f99db0 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; |
Theresa
2017/02/03 22:36:45
It looks like you need to sync and rebase this CL.
shaktisahu
2017/02/04 18:57:43
Done.
|
/** See {@link #mPosition}. */ |
- private final void setPosition(int position) { |
+ public final void setPosition(int position) { |
mPosition = position; |
} |
@@ -115,9 +114,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 +144,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,6 +168,14 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
return compareDate(mDate, otherDate) == 0; |
} |
+ protected boolean isListHeaderOrFooter() { |
Theresa
2017/02/03 19:53:06
Where is this called?
shaktisahu
2017/02/04 18:57:43
Done. Removed. Thanks!
|
+ return mIsListHeaderOrFooter; |
+ } |
+ |
+ protected Date getDate() { |
+ return mDate; |
+ } |
+ |
/** |
* @return The size of this group. |
*/ |
@@ -186,26 +198,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 +236,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_SUGGESTED_OFFLINE_PAGES_HEADER = 2; |
Theresa
2017/02/03 19:53:06
DateDividedAdapter shouldn't know about offline pa
shaktisahu
2017/02/04 18:57:43
Yes, I changed this to TYPE_SUBSECTION which can b
|
private int mSize; |
private boolean mHasListHeader; |
@@ -274,28 +295,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 +433,13 @@ public abstract class DateDividedAdapter extends Adapter<RecyclerView.ViewHolder |
} else if (pair.second == 0) { |
return TYPE_DATE; |
Theresa
2017/02/03 19:53:06
If we're moving the responsibility of knowing the
shaktisahu
2017/02/04 18:57:43
Done.
|
} 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 +455,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); |
@@ -474,18 +500,24 @@ 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--; |
} |
+ 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 +538,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); |