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

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

Issue 2396523002: Unify NewTabPageItem and ItemGroup into a single tree-structured interface. (Closed)
Patch Set: review 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/InnerNode.java
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java
new file mode 100644
index 0000000000000000000000000000000000000000..8c0078ae9007e8e6ce91dd69738dc4b2dede56a6
--- /dev/null
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/InnerNode.java
@@ -0,0 +1,90 @@
+// 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.cards;
+
+import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
+
+import java.util.List;
+
+/**
+ * An inner node in the tree: the root of a subtree, with a list of child nodes.
+ */
+public abstract class InnerNode extends ChildNode implements NodeParent {
+ public InnerNode(NodeParent parent) {
+ super(parent);
+ }
+
+ protected abstract List<TreeNode> getChildren();
+
+ int getChildIndexForPosition(int position) {
+ List<TreeNode> children = getChildren();
+ int numItems = 0;
+ int numChildren = children.size();
+ for (int i = 0; i < numChildren; i++) {
+ numItems += children.get(i).getItemCount();
+ if (position < numItems) return i;
+ }
+ return -1;
+ }
+
+ private int getStartingOffsetForChildIndex(int childIndex) {
+ List<TreeNode> children = getChildren();
+ int offset = 0;
+ for (int i = 0; i < childIndex; i++) {
+ offset += children.get(i).getItemCount();
+ }
+ return offset;
+ }
+
+ int getStartingOffsetForChild(TreeNode child) {
+ return getStartingOffsetForChildIndex(getChildren().indexOf(child));
+ }
+
+ @Override
+ public int getItemCount() {
+ int numItems = 0;
+ for (TreeNode child : getChildren()) {
+ numItems += child.getItemCount();
+ }
+ return numItems;
+ }
+
+ @Override
+ @ItemViewType
+ public int getItemViewType(int position) {
+ int index = getChildIndexForPosition(position);
+ return getChildren().get(index).getItemViewType(
+ position - getStartingOffsetForChildIndex(index));
+ }
+
+ @Override
+ public void onBindViewHolder(NewTabPageViewHolder holder, int position) {
+ int index = getChildIndexForPosition(position);
+ getChildren().get(index).onBindViewHolder(
+ holder, position - getStartingOffsetForChildIndex(index));
+ }
+
+ @Override
+ public SnippetArticle getSuggestionAt(int position) {
+ int index = getChildIndexForPosition(position);
+ return getChildren().get(index).getSuggestionAt(
+ position - getStartingOffsetForChildIndex(index));
+ }
+
+ @Override
+ public void onItemRangeChanged(TreeNode child, int index, int count) {
+ notifyItemRangeChanged(getStartingOffsetForChild(child) + index, count);
+ }
+
+ @Override
+ public void onItemRangeInserted(TreeNode child, int index, int count) {
+ notifyItemRangeInserted(getStartingOffsetForChild(child) + index, count);
+ }
+
+ @Override
+ public void onItemRangeRemoved(TreeNode child, int index, int count) {
+ notifyItemRangeRemoved(getStartingOffsetForChild(child) + index, count);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698