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

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

Issue 1812293002: Add new NTP layout with snippet cards and hide it behind a flag (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase + CR comments + added background color to new NTP Created 4 years, 9 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/NewTabPageCardsManager.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageCardsManager.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageCardsManager.java
new file mode 100644
index 0000000000000000000000000000000000000000..65f90ea4a154c58dd70ec27fd299d1a8fafc5bb6
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/NewTabPageCardsManager.java
@@ -0,0 +1,150 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package org.chromium.chrome.browser.ntp;
+
+import android.support.v7.widget.RecyclerView.Adapter;
+import android.view.ViewGroup;
+
+import org.chromium.chrome.browser.ntp.snippets.SnippetCardViewHolder;
+import org.chromium.chrome.browser.ntp.snippets.SnippetHeaderViewHolder;
+import org.chromium.chrome.browser.ntp.snippets.SnippetsManager;
+import org.chromium.chrome.browser.ntp.snippets.SnippetsManager.SnippetsManagerObserver;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * A class that handles merging above the fold elements and below the fold cards into an adapter
+ * that will be used to back the NTP RecyclerView. The first element in the adapter should always be
+ * the above-the-fold view (containing the logo, search box, and most visited tiles) and subsequent
+ * elements will be the cards shown to the user
+ */
+public class NewTabPageCardsManager extends Adapter<NewTabPageViewHolder>
newt (away) 2016/03/24 19:47:38 I think NewTabPageAdapter is a better name here. T
dgn 2016/03/28 21:33:02 Done.
+ implements SnippetsManagerObserver {
+ /**
+ * Describes the above the fold item
newt (away) 2016/03/24 19:47:38 This comment is rather vague. Could you make it mo
dgn 2016/03/28 21:33:02 Done.
+ */
+ public static final int SNIPPET_ITEM_ABOVE_THE_FOLD = 1;
newt (away) 2016/03/24 19:47:38 can these type constants be private?
dgn 2016/03/28 21:33:02 It needs to be used at least by NewTabPageListItem
+ /**
+ * Describes the header of a group of similar card snippets
+ */
+ public static final int SNIPPET_ITEM_TYPE_HEADER = 2;
+ /**
+ * Describes a single card snippet
+ */
+ public static final int SNIPPET_ITEM_TYPE_SNIPPET = 3;
+
+ /**
+ * Base type for anything to add to the new tab page
+ */
+ public interface NewTabPageListItem {
+ /**
+ * Returns the type of this snippet item (SNIPPET_ITEM_TYPE_HEADER or
+ * SNIPPET_ITEM_TYPE_SNIPPET). This is so we can distinguish between different elements
+ * that are held in a single RecyclerView holder.
+ *
+ * @return the type of this list item.
+ */
+ public int getType();
+ }
+
+ /**
+ * Represents the an item in the RecyclerView that will hold the above the fold contents for the
+ * NTP
+ */
+ static class AboveTheFoldListItem implements NewTabPageListItem {
newt (away) 2016/03/24 19:47:38 make this class and the class below private, if po
dgn 2016/03/28 21:33:02 Done.
+ public AboveTheFoldListItem() {}
+
+ @Override
+ public int getType() {
+ return NewTabPageCardsManager.SNIPPET_ITEM_ABOVE_THE_FOLD;
+ }
+ }
+
+ static class NewTabPageAboveTheFoldViewHolder extends NewTabPageViewHolder {
newt (away) 2016/03/24 19:47:38 this name should parallel the class name above.
dgn 2016/03/28 21:33:02 Done.
+ public NewTabPageAboveTheFoldViewHolder(NewTabPageLayout view, SnippetsManager manager) {
+ super(view, manager);
+ }
+
+ @Override
+ public void onBindViewHolder(NewTabPageListItem snippetItem) {
+ // nothing to do
+ }
+ }
+
+ private final NewTabPageLayout mNewTabPageLayout;
+ private final AboveTheFoldListItem mNewTabPageAboveTheFoldItem;
+ private final SnippetsManager mSnippetsManager;
+ private List<NewTabPageListItem> mNewTabPageListItems;
newt (away) 2016/03/24 19:47:38 might as well make this final too
dgn 2016/03/28 21:33:02 Done.
+
+ /**
+ * Constructor to create the manager for all the cards to display on the NTP
+ *
+ * @param snippetsManager the SnippetsManager to use
+ * @param newTabPageLayout the layout encapsulating all the above-the-fold elements
+ * (logo, search box, most visited tiles)
+ */
+ public NewTabPageCardsManager(
+ SnippetsManager snippetsManager, NewTabPageLayout newTabPageLayout) {
+ mNewTabPageLayout = newTabPageLayout;
+ mSnippetsManager = snippetsManager;
+ mNewTabPageAboveTheFoldItem = new AboveTheFoldListItem();
newt (away) 2016/03/24 19:47:38 Likewise, this member variable name should match t
dgn 2016/03/28 21:33:02 Done.
+ mNewTabPageListItems = new ArrayList<NewTabPageListItem>();
+ mNewTabPageListItems.add(mNewTabPageAboveTheFoldItem);
+
+ mSnippetsManager.setObserver(this);
+ }
+
+ @Override
+ public void onSnippetsReceived(List<NewTabPageListItem> listSnippets) {
+ setSnippetListItems(listSnippets);
+ }
+
+ /**
+ * Sets the list of items to display in the snippets RecyclerView
+ *
+ * @param listItems the new list of items to display
+ */
+ public void setSnippetListItems(List<NewTabPageListItem> listItems) {
+ mNewTabPageListItems.clear();
+ mNewTabPageListItems.add(mNewTabPageAboveTheFoldItem);
+ mNewTabPageListItems.addAll(listItems);
+ notifyDataSetChanged();
+ }
+
+ @Override
+ public int getItemViewType(int position) {
+ return mNewTabPageListItems.get(position).getType();
+ }
+
+ @Override
+ public NewTabPageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
+ if (viewType == NewTabPageCardsManager.SNIPPET_ITEM_ABOVE_THE_FOLD) {
+ return new NewTabPageAboveTheFoldViewHolder(mNewTabPageLayout, mSnippetsManager);
+ }
+
+ if (viewType == NewTabPageCardsManager.SNIPPET_ITEM_TYPE_HEADER) {
+ return new SnippetHeaderViewHolder(
+ SnippetHeaderViewHolder.createView(parent), mSnippetsManager);
+ }
+
+ if (viewType == NewTabPageCardsManager.SNIPPET_ITEM_TYPE_SNIPPET) {
+ return new SnippetCardViewHolder(
+ SnippetCardViewHolder.createView(parent), mSnippetsManager);
+ }
+
+ return null;
+ }
+
+ @Override
+ public void onBindViewHolder(NewTabPageViewHolder holder, final int position) {
+ holder.onBindViewHolder(mNewTabPageListItems.get(position));
+ }
+
+ @Override
+ public int getItemCount() {
+ return mNewTabPageListItems.size();
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698