Bug 1278644 - Stable getItemId implementation for CombinedHistoryAdapter r=sebastian

MozReview-Commit-ID: EylBNeI9Xwp

--HG--
extra : rebase_source : 2da5ae91c3f8cc13078a6b1b252463cf2681d2e7
This commit is contained in:
Grigory Kruglov 2016-06-28 15:16:59 -07:00
parent 56a8127a68
commit 76dad02030

View File

@ -50,6 +50,7 @@ public class CombinedHistoryAdapter extends RecyclerView.Adapter<CombinedHistory
super();
sectionHeaders = new SparseArray<>();
HistorySectionsHelper.updateRecentSectionOffset(resources, sectionDateRangeArray);
this.setHasStableIds(true);
}
public void setHistory(Cursor history) {
@ -189,6 +190,52 @@ public class CombinedHistoryAdapter extends RecyclerView.Adapter<CombinedHistory
return historySize + sectionHeaders.size() + CombinedHistoryPanel.NUM_SMART_FOLDERS;
}
/**
* Returns stable ID for each position. Data behind historyCursor is a sorted Combined view.
*
* @param position view item position for which to generate a stable ID
* @return stable ID for given position
*/
@Override
public long getItemId(int position) {
// Two randomly selected large primes used to generate non-clashing IDs.
final long PRIME_BOOKMARKS = 32416189867L;
final long PRIME_SECTION_HEADERS = 32416187737L;
// RecyclerView.NO_ID is -1, so let's start from -2 for our hard-coded IDs.
final int RECENT_TABS_ID = -2;
final int SYNCED_DEVICES_ID = -3;
switch (getItemTypeForPosition(position)) {
case RECENT_TABS:
return RECENT_TABS_ID;
case SYNCED_DEVICES:
return SYNCED_DEVICES_ID;
case SECTION_HEADER:
// We might have multiple section headers, so we try get unique IDs for them.
return position * PRIME_SECTION_HEADERS;
case HISTORY:
if (!historyCursor.moveToPosition(position)) {
return RecyclerView.NO_ID;
}
final int historyIdCol = historyCursor.getColumnIndexOrThrow(BrowserContract.Combined.HISTORY_ID);
final long historyId = historyCursor.getLong(historyIdCol);
if (historyId != -1) {
return historyId;
}
final int bookmarkIdCol = historyCursor.getColumnIndexOrThrow(BrowserContract.Combined.BOOKMARK_ID);
final long bookmarkId = historyCursor.getLong(bookmarkIdCol);
// Avoid clashing with historyId.
return bookmarkId * PRIME_BOOKMARKS;
default:
throw new IllegalStateException("Unexpected Home Panel item type");
}
}
/**
* Add only the SectionHeaders that have history items within their range to a SparseArray, where the
* array index is the position of the header in the history-only (no clients) ordering.