Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3050)

Unified Diff: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java

Issue 2434263002: 📰 Refresh the visiblity of AllDismissed when items change (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
index c00cf332f8721018cf024b6ebf5eee8cfaddfeb2..4ba9f41115b5462154467e02b7580fc43b3d47c0 100644
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/NewTabPageAdapter.java
@@ -26,6 +26,7 @@
import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
import org.chromium.chrome.browser.ntp.snippets.SnippetArticleViewHolder;
import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge;
+import org.chromium.chrome.browser.ntp.snippets.SnippetsConfig;
import org.chromium.chrome.browser.ntp.snippets.SuggestionsSource;
import java.util.ArrayList;
@@ -138,6 +139,24 @@ public NewTabPageAdapter(NewTabPageManager manager, View aboveTheFoldView, UiCon
protected List<TreeNode> getChildren() {
return mChildren;
}
+
+ @Override
+ public void onItemRangeChanged(TreeNode child, int index, int count) {
+ if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
+ super.onItemRangeChanged(child, index, count);
+ }
+
+ @Override
+ public void onItemRangeInserted(TreeNode child, int index, int count) {
+ if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
+ super.onItemRangeInserted(child, index, count);
+ }
+
+ @Override
+ public void onItemRangeRemoved(TreeNode child, int index, int count) {
+ if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
+ super.onItemRangeRemoved(child, index, count);
+ }
};
mSigninPromo = new SignInPromo(mRoot, this);
@@ -155,6 +174,7 @@ public NewTabPageAdapter(NewTabPageManager manager, View aboveTheFoldView, UiCon
*/
public void resetSections(boolean alwaysAllowEmptySections) {
mSections.clear();
+ mChildren.clear();
SuggestionsSource suggestionsSource = mNewTabPageManager.getSuggestionsSource();
int[] categories = suggestionsSource.getCategories();
@@ -341,11 +361,7 @@ public int getAboveTheFoldPosition() {
}
public int getFirstHeaderPosition() {
- int count = getItemCount();
- for (int i = 0; i < count; i++) {
- if (getItemViewType(i) == ItemViewType.HEADER) return i;
- }
- return RecyclerView.NO_POSITION;
+ return getFirstPositionForType(ItemViewType.HEADER);
}
public int getFirstCardPosition() {
@@ -355,10 +371,6 @@ public int getFirstCardPosition() {
return RecyclerView.NO_POSITION;
}
- public int getFooterPosition() {
- return getChildPositionOffset(mFooter);
- }
-
public int getBottomSpacerPosition() {
return getChildPositionOffset(mBottomSpacer);
}
@@ -409,17 +421,27 @@ private void updateChildren() {
notifyDataSetChanged();
}
+ private void updateAllDismissedVisibility() {
+ boolean showAllDismissed = hasAllBeenDismissed();
+ if (showAllDismissed == mChildren.contains(mAllDismissed)) return;
+
+ if (showAllDismissed) {
+ assert mChildren.contains(mFooter);
+ mChildren.set(mChildren.indexOf(mFooter), mAllDismissed);
+ } else {
+ assert mChildren.contains(mAllDismissed);
+ mChildren.set(mChildren.indexOf(mAllDismissed), mFooter);
+ }
+ notifyItemChanged(getLastContentItemPosition());
+ }
+
private void removeSection(SuggestionsSection section) {
mSections.remove(section.getCategory());
int startPos = getChildPositionOffset(section);
mChildren.remove(section);
notifyItemRangeRemoved(startPos, section.getItemCount());
- if (hasAllBeenDismissed()) {
- int footerPosition = getFooterPosition();
- mChildren.set(mChildren.indexOf(mFooter), mAllDismissed);
- notifyItemChanged(footerPosition);
- }
+ updateAllDismissedVisibility();
notifyItemChanged(getBottomSpacerPosition());
}
@@ -427,24 +449,25 @@ private void removeSection(SuggestionsSection section) {
@Override
public void onItemRangeChanged(TreeNode child, int itemPosition, int itemCount) {
assert child == mRoot;
- if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
notifyItemRangeChanged(itemPosition, itemCount);
}
@Override
public void onItemRangeInserted(TreeNode child, int itemPosition, int itemCount) {
assert child == mRoot;
- if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
notifyItemRangeInserted(itemPosition, itemCount);
notifyItemChanged(getItemCount() - 1); // Refresh the spacer too.
+
+ updateAllDismissedVisibility();
}
@Override
public void onItemRangeRemoved(TreeNode child, int itemPosition, int itemCount) {
assert child == mRoot;
- if (mChildren.isEmpty()) return; // The sections have not been initialised yet.
notifyItemRangeRemoved(itemPosition, itemCount);
notifyItemChanged(getItemCount() - 1); // Refresh the spacer too.
+
+ updateAllDismissedVisibility();
}
@Override
@@ -492,6 +515,7 @@ public void dismissItem(int position) {
}
private void dismissSection(SuggestionsSection section) {
+ assert SnippetsConfig.isSectionDismissalEnabled();
mNewTabPageManager.getSuggestionsSource().dismissCategory(section.getCategory());
removeSection(section);
}
@@ -517,12 +541,6 @@ private void dismissSuggestion(int position) {
private void dismissPromo() {
// TODO(dgn): accessibility announcement.
mSigninPromo.dismiss();
-
- if (hasAllBeenDismissed()) {
- int footerPosition = getFooterPosition();
- mChildren.set(mChildren.indexOf(mFooter), mAllDismissed);
- notifyItemChanged(footerPosition);
- }
}
/**
@@ -567,6 +585,15 @@ SnippetArticle getSuggestionAt(int position) {
return mRoot.getSuggestionAt(position);
}
+ @VisibleForTesting
+ int getFirstPositionForType(@ItemViewType int viewType) {
+ int count = getItemCount();
+ for (int i = 0; i < count; i++) {
+ if (getItemViewType(i) == viewType) return i;
+ }
+ return RecyclerView.NO_POSITION;
+ }
+
private void announceItemRemoved(String suggestionTitle) {
// In tests the RecyclerView can be null.
if (mRecyclerView == null) return;

Powered by Google App Engine
This is Rietveld 408576698