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

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

Issue 2400783003: Ntp: show AllDismissedItem when all sections have been dismissed. (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 b4a1c98fdc6d348901bd46893807d3a96bbfc501..e81875fe310441f251763a30ae4fd59a3024ad3a 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
@@ -60,6 +60,7 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
private final List<ItemGroup> mGroups = new ArrayList<>();
private final AboveTheFoldItem mAboveTheFold = new AboveTheFoldItem();
private final SigninPromoItem mSigninPromo = new SigninPromoItem();
+ private final AllDismissedItem mAllDismissed = new AllDismissedItem();
private final Footer mFooter = new Footer();
private final SpacingItem mBottomSpacer = new SpacingItem();
@@ -157,14 +158,14 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
@VisibleForTesting
void finishInitialization() {
mSigninPromo.setObserver(this);
- resetSections();
+ resetSections(/*alwaysAllowEmptySections=*/false);
mNewTabPageManager.getSuggestionsSource().setObserver(this);
mNewTabPageManager.registerSignInStateObserver(new SignInStateObserver() {
@Override
public void onSignedIn() {
mSigninPromo.hide();
- resetSections();
+ resetSections(/*alwaysAllowEmptySections=*/false);
}
@Override
@@ -174,12 +175,15 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
});
}
- /** Resets the sections, reloading the whole new tab page content. */
- private void resetSections() {
+ /**
+ * Resets the sections, reloading the whole new tab page content.
+ * @param alwaysAllowEmptySections Whether sections are always allowed to be displayed when
+ * they are empty, even when they are normally not.
+ */
+ public void resetSections(boolean alwaysAllowEmptySections) {
mSections.clear();
SuggestionsSource suggestionsSource = mNewTabPageManager.getSuggestionsSource();
-
int[] categories = suggestionsSource.getCategories();
int[] suggestionsPerCategory = new int[categories.length];
int i = 0;
@@ -190,7 +194,8 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
|| categoryStatus == CategoryStatus.CATEGORY_EXPLICITLY_DISABLED)
continue;
- suggestionsPerCategory[i++] = resetSection(category, categoryStatus);
+ suggestionsPerCategory[i++] =
+ resetSection(category, categoryStatus, alwaysAllowEmptySections);
}
mNewTabPageManager.trackSnippetsPageImpression(categories, suggestionsPerCategory);
@@ -198,17 +203,29 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
updateGroups();
}
- private int resetSection(@CategoryInt int category, @CategoryStatusEnum int categoryStatus) {
+ /**
+ * Resets the section for {@code category}. Removes the section if there are no suggestions for
+ * it and it is not allowed to be empty. Otherwise, creates the section if it is not present
+ * yet. Sets the available suggestions on the section.
+ * @param category The category for which the section must be reset.
+ * @param categoryStatus The category status.
+ * @param alwaysAllowEmptySections Whether sections are always allowed to be displayed when
+ * they are empty, even when they are normally not.
+ * @return The number of suggestions for the section.
+ */
+ private int resetSection(@CategoryInt int category, @CategoryStatusEnum int categoryStatus,
+ boolean alwaysAllowEmptySections) {
SuggestionsSource suggestionsSource = mNewTabPageManager.getSuggestionsSource();
List<SnippetArticle> suggestions = suggestionsSource.getSuggestionsForCategory(category);
-
- // Create the new section.
SuggestionsCategoryInfo info = suggestionsSource.getCategoryInfo(category);
- if (suggestions.isEmpty() && !info.showIfEmpty()) {
+
+ // Do not show an empty section if not allowed.
+ if (suggestions.isEmpty() && !info.showIfEmpty() && !alwaysAllowEmptySections) {
mSections.remove(category);
return 0;
}
+ // Create the section if needed.
SuggestionsSection section = mSections.get(category);
if (section == null) {
section = new SuggestionsSection(category, info, this);
@@ -277,7 +294,7 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
return;
case CategoryStatus.SIGNED_OUT:
- resetSection(category, status);
+ resetSection(category, status, /*alwaysAllowEmptySections=*/false);
return;
default:
@@ -338,6 +355,10 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
return new Footer.ViewHolder(mRecyclerView, mNewTabPageManager);
}
+ if (viewType == NewTabPageItem.VIEW_TYPE_ALL_DISMISSED) {
+ return new AllDismissedItem.ViewHolder(mRecyclerView, mNewTabPageManager, this);
+ }
+
return null;
}
@@ -370,7 +391,7 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
return RecyclerView.NO_POSITION;
}
- public int getLastContentItemPosition() {
+ public int getFooterPosition() {
return getGroupPositionOffset(mFooter);
}
@@ -411,14 +432,13 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
private void updateGroups() {
mGroups.clear();
mGroups.add(mAboveTheFold);
- // TODO(treib,bauerb): Preserve the order of categories we got from getCategories.
mGroups.addAll(mSections.values());
mGroups.add(mSigninPromo);
- if (hasVisibleBelowTheFoldItems()) {
- mGroups.add(mFooter);
- mGroups.add(mBottomSpacer);
- }
+ mGroups.add(hasAllBeenDismissed() ? mAllDismissed : mFooter);
+ mGroups.add(mBottomSpacer);
+ // TODO(mvanouwerkerk): Notify about the subset of changed items. At least |mAboveTheFold|
+ // has not changed when refreshing from the all dismissed state.
notifyDataSetChanged();
}
@@ -426,17 +446,15 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
mSections.remove(section.getCategory());
int startPos = getGroupPositionOffset(section);
mGroups.remove(section);
- int removedItems = section.getItems().size();
+ notifyItemRangeRemoved(startPos, section.getItems().size());
- notifyItemRangeRemoved(startPos, removedItems);
-
- if (!hasVisibleBelowTheFoldItems()) {
- mGroups.remove(mFooter);
- mGroups.remove(mBottomSpacer);
- notifyItemRangeRemoved(startPos + removedItems, 2);
- } else {
- notifyItemChanged(getItems().size() - 1); // Refresh the spacer too.
+ if (hasAllBeenDismissed()) {
+ int footerPosition = getFooterPosition();
+ mGroups.set(mGroups.indexOf(mFooter), mAllDismissed);
+ notifyItemChanged(footerPosition);
}
+
+ notifyItemChanged(getBottomSpacerPosition());
}
@Override
@@ -557,11 +575,10 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
// TODO(dgn): accessibility announcement.
mSigninPromo.dismiss();
- if (!hasVisibleBelowTheFoldItems()) {
- int footerPosition = getLastContentItemPosition();
- mGroups.remove(mFooter);
- mGroups.remove(mBottomSpacer);
- notifyItemRangeRemoved(footerPosition, 2);
+ if (hasAllBeenDismissed()) {
+ int footerPosition = getFooterPosition();
+ mGroups.set(mGroups.indexOf(mFooter), mAllDismissed);
+ notifyItemChanged(footerPosition);
}
}
@@ -592,8 +609,8 @@ public class NewTabPageAdapter extends Adapter<NewTabPageViewHolder>
return mRecyclerView.findViewHolderForAdapterPosition(siblingPosDelta + swipePos);
}
- private boolean hasVisibleBelowTheFoldItems() {
- return !mSections.isEmpty() || mSigninPromo.isShown();
+ private boolean hasAllBeenDismissed() {
+ return mSections.isEmpty() && !mSigninPromo.isShown();
}
@VisibleForTesting

Powered by Google App Engine
This is Rietveld 408576698