Commit 311b9cac authored by bauerb's avatar bauerb Committed by Commit bot

[Android NTP] Move suggestions in SuggestionSection into a separate TreeNode.

This makes SnippetArticle a pure model class that doesn't know about the adapter.

Goals of the overall refactoring:
1) Decoupling the various pieces of logic, by moving them from the adapter into
the tree structure. For example, dismissal is something that could be handled by
a TreeNode instead of doing the big switch statement we have right now in the
Adapter.
2) Stabilizing the structure of the tree. Right now we tend to throw away
everything and rebuild, which means we have to manually figure out what changes
exactly to notify about (cf. removeSuggestion). If we keep the tree structure
stable and just adjust the number of children below each node as necessary (like
SigninPromo already does), we get correct notifications for free, and it will
allow for some optimizations as well (we could cache the number of children for
each node and update it incrementally when a subtree changes. Right now we'd
have to contort ourselves a bit to make that work with things like
resetChildren()).

BUG=616090

Review-Url: https://codereview.chromium.org/2439683003
Cr-Commit-Position: refs/heads/master@{#427060}
parent 9029a43f
...@@ -9,6 +9,7 @@ import org.chromium.chrome.browser.ntp.snippets.CategoryInt; ...@@ -9,6 +9,7 @@ import org.chromium.chrome.browser.ntp.snippets.CategoryInt;
import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnum; import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnum;
import org.chromium.chrome.browser.ntp.snippets.SectionHeader; import org.chromium.chrome.browser.ntp.snippets.SectionHeader;
import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; 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.SnippetsBridge;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -21,6 +22,7 @@ public class SuggestionsSection extends InnerNode { ...@@ -21,6 +22,7 @@ public class SuggestionsSection extends InnerNode {
private final List<TreeNode> mChildren = new ArrayList<>(); private final List<TreeNode> mChildren = new ArrayList<>();
private final List<SnippetArticle> mSuggestions = new ArrayList<>(); private final List<SnippetArticle> mSuggestions = new ArrayList<>();
private final SectionHeader mHeader; private final SectionHeader mHeader;
private final TreeNode mSuggestionsList = new SuggestionsList(this);
private final StatusItem mStatus; private final StatusItem mStatus;
private final ProgressItem mProgressIndicator = new ProgressItem(); private final ProgressItem mProgressIndicator = new ProgressItem();
private final ActionItem mMoreButton; private final ActionItem mMoreButton;
...@@ -35,6 +37,39 @@ public class SuggestionsSection extends InnerNode { ...@@ -35,6 +37,39 @@ public class SuggestionsSection extends InnerNode {
resetChildren(); resetChildren();
} }
private class SuggestionsList extends ChildNode {
public SuggestionsList(NodeParent parent) {
super(parent);
}
@Override
public int getItemCount() {
return mSuggestions.size();
}
@Override
@ItemViewType
public int getItemViewType(int position) {
return ItemViewType.SNIPPET;
}
@Override
public void onBindViewHolder(NewTabPageViewHolder holder, int position) {
assert holder instanceof SnippetArticleViewHolder;
((SnippetArticleViewHolder) holder).onBindViewHolder(getSuggestionAt(position));
}
@Override
public SnippetArticle getSuggestionAt(int position) {
return mSuggestions.get(position);
}
@Override
public int getDismissSiblingPosDelta(int position) {
return 0;
}
}
@Override @Override
protected List<TreeNode> getChildren() { protected List<TreeNode> getChildren() {
return mChildren; return mChildren;
...@@ -43,7 +78,7 @@ public class SuggestionsSection extends InnerNode { ...@@ -43,7 +78,7 @@ public class SuggestionsSection extends InnerNode {
private void resetChildren() { private void resetChildren() {
mChildren.clear(); mChildren.clear();
mChildren.add(mHeader); mChildren.add(mHeader);
mChildren.addAll(mSuggestions); mChildren.add(mSuggestionsList);
if (mSuggestions.isEmpty()) mChildren.add(mStatus); if (mSuggestions.isEmpty()) mChildren.add(mStatus);
if (mCategoryInfo.hasMoreButton() || mSuggestions.isEmpty()) mChildren.add(mMoreButton); if (mCategoryInfo.hasMoreButton() || mSuggestions.isEmpty()) mChildren.add(mMoreButton);
......
...@@ -5,15 +5,12 @@ package org.chromium.chrome.browser.ntp.snippets; ...@@ -5,15 +5,12 @@ package org.chromium.chrome.browser.ntp.snippets;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import org.chromium.chrome.browser.ntp.cards.ItemViewType;
import org.chromium.chrome.browser.ntp.cards.Leaf;
import org.chromium.chrome.browser.ntp.cards.NewTabPageViewHolder;
import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout.ContentSuggestionsCardLayoutEnum; import org.chromium.chrome.browser.ntp.snippets.ContentSuggestionsCardLayout.ContentSuggestionsCardLayoutEnum;
/** /**
* Represents the data for an article card on the NTP. * Represents the data for an article card on the NTP.
*/ */
public class SnippetArticle extends Leaf { public class SnippetArticle {
/** The category of this article. */ /** The category of this article. */
public final int mCategory; public final int mCategory;
...@@ -88,25 +85,6 @@ public class SnippetArticle extends Leaf { ...@@ -88,25 +85,6 @@ public class SnippetArticle extends Leaf {
return mCategory ^ mIdWithinCategory.hashCode(); return mCategory ^ mIdWithinCategory.hashCode();
} }
@Override
@ItemViewType
public int getItemViewType() {
return ItemViewType.SNIPPET;
}
@Override
protected void onBindViewHolder(NewTabPageViewHolder holder) {
assert holder instanceof SnippetArticleViewHolder;
((SnippetArticleViewHolder) holder).onBindViewHolder(this);
}
@Override
public SnippetArticle getSuggestionAt(int position) {
if (position != 0) throw new IndexOutOfBoundsException();
return this;
}
/** /**
* Returns this article's thumbnail as a {@link Bitmap}. Can return {@code null} as it is * Returns this article's thumbnail as a {@link Bitmap}. Can return {@code null} as it is
* initially unset. * initially unset.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment