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

Side by Side 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: oo 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.chrome.browser.ntp.cards;
6
7 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle;
8
9 import java.util.List;
10
11 /**
12 * An inner node in the tree: the root of a subtree, with a list of child nodes.
13 */
14 public abstract class InnerNode extends ChildNode implements NodeParent {
15 public InnerNode(NodeParent parent) {
16 super(parent);
17 }
18
19 protected abstract List<TreeNode> getChildren();
20
21 int getChildIndexForPosition(int position) {
PEConn 2016/10/13 13:48:56 Is this meant to be package private?
Bernhard Bauer 2016/10/13 13:54:37 Yes, I'm calling it from NewTabPageAdapter to expo
22 List<TreeNode> children = getChildren();
23 int numItems = 0;
24 int numChildren = children.size();
25 for (int i = 0; i < numChildren; i++) {
26 numItems += children.get(i).getItemCount();
27 if (position < numItems) return i;
28 }
29 return -1;
30 }
31
32 private int getStartingOffsetForChildIndex(int childIndex) {
33 List<TreeNode> children = getChildren();
34 int offset = 0;
35 for (int i = 0; i < childIndex; i++) {
36 offset += children.get(i).getItemCount();
37 }
38 return offset;
39 }
40
41 int getStartingOffsetForChild(TreeNode child) {
42 return getStartingOffsetForChildIndex(getChildren().indexOf(child));
43 }
44
45 @Override
46 public int getItemCount() {
47 int numItems = 0;
48 for (TreeNode child : getChildren()) {
49 numItems += child.getItemCount();
PEConn 2016/10/13 13:48:56 Can we cache this?
Bernhard Bauer 2016/10/13 13:54:37 I'm planning to do that in one of the next CLs, it
50 }
51 return numItems;
52 }
53
54 @Override
55 @ItemViewType
56 public int getItemViewType(int position) {
57 int index = getChildIndexForPosition(position);
58 return getChildren().get(index).getItemViewType(
59 position - getStartingOffsetForChildIndex(index));
60 }
61
62 @Override
63 public void onBindViewHolder(NewTabPageViewHolder holder, int position) {
64 int index = getChildIndexForPosition(position);
65 getChildren().get(index).onBindViewHolder(
66 holder, position - getStartingOffsetForChildIndex(index));
67 }
68
69 @Override
70 public SnippetArticle getSuggestionAt(int position) {
71 int index = getChildIndexForPosition(position);
72 return getChildren().get(index).getSuggestionAt(
73 position - getStartingOffsetForChildIndex(index));
74 }
75
76 @Override
77 public void onItemRangeChanged(TreeNode child, int index, int count) {
78 notifyItemRangeChanged(getStartingOffsetForChild(child) + index, count);
79 }
80
81 @Override
82 public void onItemRangeInserted(TreeNode child, int index, int count) {
83 notifyItemRangeInserted(getStartingOffsetForChild(child) + index, count) ;
84 }
85
86 @Override
87 public void onItemRangeRemoved(TreeNode child, int index, int count) {
88 notifyItemRangeRemoved(getStartingOffsetForChild(child) + index, count);
89 }
90 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698