Bug 1277978 - Part 5 - Once we can access the panelStateChangeListener, use it to retrieve the cached tabs count when initialising the HistoryAdapter. r=liuche
While passing the cached tabs count to the HistoryAdapter in its constructor greatly simplifies getting the cached count into the adapter before the RecyclerView initialises, this relies on the History panel having the panelStateChangeListener available before the HistoryAdapter is created in onCreate().
MozReview-Commit-ID: 64IbAe6SaEq
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryAdapter.java
@@ -47,18 +47,19 @@ public class CombinedHistoryAdapter exte
private RecentTabsUpdateHandler recentTabsUpdateHandler;
private int recentTabsCount = 0;
private LinearLayoutManager linearLayoutManager; // Only used on the UI thread, so no need to be volatile.
// We use a sparse array to store each section header's position in the panel [more cheaply than a HashMap].
private final SparseArray<SectionHeader> sectionHeaders;
- public CombinedHistoryAdapter(Resources resources) {
+ public CombinedHistoryAdapter(Resources resources, int cachedRecentTabsCount) {
super();
+ recentTabsCount = cachedRecentTabsCount;
sectionHeaders = new SparseArray<>();
HistorySectionsHelper.updateRecentSectionOffset(resources, sectionDateRangeArray);
this.setHasStableIds(true);
}
public void setPanelStateChangeListener(
HomeFragment.PanelStateChangeListener panelStateChangeListener) {
this.panelStateChangeListener = panelStateChangeListener;
@@ -105,16 +106,25 @@ public class CombinedHistoryAdapter exte
@Override
public void onRecentTabsCountUpdated(final int count, final boolean countReliable) {
// Now that other items can move around depending on the visibility of the
// Recent Tabs folder, only update the recentTabsCount on the UI thread.
ThreadUtils.postToUiThread(new Runnable() {
@UiThread
@Override
public void run() {
+ if (!countReliable && count <= recentTabsCount) {
+ // The final tab count (where countReliable = true) is normally >= than
+ // previous values with countReliable = false. Hence we only want to
+ // update the displayed tab count with a preliminary value if it's larger
+ // than the previous count, so as to avoid the displayed count jumping
+ // downwards and then back up, as well as unnecessary folder animations.
+ return;
+ }
+
final boolean prevFolderVisibility = isRecentTabsFolderVisible();
recentTabsCount = count;
final boolean folderVisible = isRecentTabsFolderVisible();
if (prevFolderVisibility == folderVisible) {
if (prevFolderVisibility) {
notifyItemChanged(RECENT_TABS_SMARTFOLDER_INDEX);
}
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
@@ -105,20 +105,25 @@ public class CombinedHistoryPanel extend
*/
boolean changeLevel(PanelLevel level);
}
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
- mHistoryAdapter = new CombinedHistoryAdapter(getResources());
+ int cachedRecentTabsCount = 0;
+ if (mPanelStateChangeListener != null ) {
+ cachedRecentTabsCount = mPanelStateChangeListener.getCachedRecentTabsCount();
+ }
+ mHistoryAdapter = new CombinedHistoryAdapter(getResources(), cachedRecentTabsCount);
if (mPanelStateChangeListener != null) {
mHistoryAdapter.setPanelStateChangeListener(mPanelStateChangeListener);
}
+
mClientsAdapter = new ClientsAdapter(getContext());
// The RecentTabsAdapter doesn't use a cursor and therefore can't use the CursorLoader's
// onLoadFinished() callback for updating the panel state when the closed tab count changes.
// Instead, we provide it with independent callbacks as necessary.
mRecentTabsAdapter = new RecentTabsAdapter(getContext(),
mHistoryAdapter.getRecentTabsUpdateHandler(), getPanelStateUpdateHandler());
mSyncStatusListener = new RemoteTabsSyncListener();