| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.chrome.browser.ntp.cards; | 5 package org.chromium.chrome.browser.ntp.cards; |
| 6 | 6 |
| 7 import org.chromium.base.Callback; | 7 import org.chromium.base.Callback; |
| 8 import org.chromium.base.Log; | 8 import org.chromium.base.Log; |
| 9 import org.chromium.base.VisibleForTesting; | 9 import org.chromium.base.VisibleForTesting; |
| 10 import org.chromium.chrome.browser.ntp.NewTabPage.DestructionObserver; | 10 import org.chromium.chrome.browser.ntp.NewTabPage.DestructionObserver; |
| 11 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; | 11 import org.chromium.chrome.browser.ntp.NewTabPageView.NewTabPageManager; |
| 12 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; | 12 import org.chromium.chrome.browser.ntp.snippets.CategoryInt; |
| 13 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; | 13 import org.chromium.chrome.browser.ntp.snippets.CategoryStatus.CategoryStatusEnu
m; |
| 14 import org.chromium.chrome.browser.ntp.snippets.SectionHeader; | 14 import org.chromium.chrome.browser.ntp.snippets.SectionHeader; |
| 15 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; | 15 import org.chromium.chrome.browser.ntp.snippets.SnippetArticle; |
| 16 import org.chromium.chrome.browser.ntp.snippets.SnippetArticleViewHolder; | 16 import org.chromium.chrome.browser.ntp.snippets.SnippetArticleViewHolder; |
| 17 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; | 17 import org.chromium.chrome.browser.ntp.snippets.SnippetsBridge; |
| 18 import org.chromium.chrome.browser.offlinepages.ClientId; | 18 import org.chromium.chrome.browser.offlinepages.ClientId; |
| 19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; | 19 import org.chromium.chrome.browser.offlinepages.OfflinePageBridge; |
| 20 import org.chromium.chrome.browser.offlinepages.OfflinePageItem; | 20 import org.chromium.chrome.browser.offlinepages.OfflinePageItem; |
| 21 | 21 |
| 22 import java.util.ArrayList; | 22 import java.util.ArrayList; |
| 23 import java.util.Arrays; |
| 23 import java.util.Iterator; | 24 import java.util.Iterator; |
| 24 import java.util.List; | 25 import java.util.List; |
| 25 | 26 |
| 26 /** | 27 /** |
| 27 * A group of suggestions, with a header, a status card, and a progress indicato
r. This is | 28 * A group of suggestions, with a header, a status card, and a progress indicato
r. This is |
| 28 * responsible for tracking whether its suggestions have been saved offline. | 29 * responsible for tracking whether its suggestions have been saved offline. |
| 29 */ | 30 */ |
| 30 public class SuggestionsSection extends InnerNode { | 31 public class SuggestionsSection extends InnerNode { |
| 31 private static final String TAG = "NtpCards"; | 32 private static final String TAG = "NtpCards"; |
| 32 | 33 |
| 33 private final SuggestionsCategoryInfo mCategoryInfo; | 34 private final SuggestionsCategoryInfo mCategoryInfo; |
| 34 private final OfflinePageBridge mOfflinePageBridge; | 35 private final OfflinePageBridge mOfflinePageBridge; |
| 35 private final List<TreeNode> mChildren = new ArrayList<>(); | |
| 36 | 36 |
| 37 // Children | 37 // Children |
| 38 private final SectionHeader mHeader; | 38 private final SectionHeader mHeader; |
| 39 private final SuggestionsList mSuggestionsList; | 39 private final SuggestionsList mSuggestionsList; |
| 40 private final StatusItem mStatus; | 40 private final StatusItem mStatus; |
| 41 private final ActionItem mMoreButton; |
| 41 private final ProgressItem mProgressIndicator; | 42 private final ProgressItem mProgressIndicator; |
| 42 private final ActionItem mMoreButton; | 43 |
| 44 private final List<TreeNode> mChildren; |
| 43 | 45 |
| 44 private boolean mIsNtpDestroyed = false; | 46 private boolean mIsNtpDestroyed = false; |
| 45 | 47 |
| 46 public SuggestionsSection(NodeParent parent, SuggestionsCategoryInfo info, | 48 public SuggestionsSection(NodeParent parent, NewTabPageManager manager, |
| 47 NewTabPageManager manager, OfflinePageBridge offlinePageBridge) { | 49 OfflinePageBridge offlinePageBridge, SuggestionsCategoryInfo info) { |
| 48 super(parent); | 50 super(parent); |
| 49 mCategoryInfo = info; | 51 mCategoryInfo = info; |
| 50 mOfflinePageBridge = offlinePageBridge; | 52 mOfflinePageBridge = offlinePageBridge; |
| 51 | 53 |
| 52 mHeader = new SectionHeader(info.getTitle()); | 54 mHeader = new SectionHeader(info.getTitle()); |
| 53 mSuggestionsList = new SuggestionsList(this, info); | 55 mSuggestionsList = new SuggestionsList(this, info); |
| 54 mStatus = StatusItem.createNoSuggestionsItem(this); | 56 mStatus = StatusItem.createNoSuggestionsItem(this); |
| 55 mMoreButton = new ActionItem(this); | 57 mMoreButton = new ActionItem(this); |
| 56 mProgressIndicator = new ProgressItem(this); | 58 mProgressIndicator = new ProgressItem(this); |
| 57 initializeChildren(); | 59 |
| 60 mChildren = Arrays.asList( |
| 61 mHeader, |
| 62 mSuggestionsList, |
| 63 mStatus, |
| 64 mMoreButton, |
| 65 mProgressIndicator); |
| 58 | 66 |
| 59 setupOfflinePageBridgeObserver(manager); | 67 setupOfflinePageBridgeObserver(manager); |
| 60 } | 68 } |
| 61 | 69 |
| 70 @Override |
| 71 public void init() { |
| 72 super.init(); |
| 73 refreshChildrenVisibility(); |
| 74 } |
| 75 |
| 62 private static class SuggestionsList extends ChildNode implements Iterable<S
nippetArticle> { | 76 private static class SuggestionsList extends ChildNode implements Iterable<S
nippetArticle> { |
| 63 private final List<SnippetArticle> mSuggestions = new ArrayList<>(); | 77 private final List<SnippetArticle> mSuggestions = new ArrayList<>(); |
| 64 private final SuggestionsCategoryInfo mCategoryInfo; | 78 private final SuggestionsCategoryInfo mCategoryInfo; |
| 65 | 79 |
| 66 public SuggestionsList(NodeParent parent, SuggestionsCategoryInfo catego
ryInfo) { | 80 public SuggestionsList(NodeParent parent, SuggestionsCategoryInfo catego
ryInfo) { |
| 67 super(parent); | 81 super(parent); |
| 68 mCategoryInfo = categoryInfo; | 82 mCategoryInfo = categoryInfo; |
| 69 } | 83 } |
| 70 | 84 |
| 71 @Override | 85 @Override |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 public Iterator<SnippetArticle> iterator() { | 138 public Iterator<SnippetArticle> iterator() { |
| 125 return mSuggestions.iterator(); | 139 return mSuggestions.iterator(); |
| 126 } | 140 } |
| 127 } | 141 } |
| 128 | 142 |
| 129 @Override | 143 @Override |
| 130 protected List<TreeNode> getChildren() { | 144 protected List<TreeNode> getChildren() { |
| 131 return mChildren; | 145 return mChildren; |
| 132 } | 146 } |
| 133 | 147 |
| 134 private void initializeChildren() { | |
| 135 mChildren.add(mHeader); | |
| 136 mChildren.add(mSuggestionsList); | |
| 137 | |
| 138 // Optional leaves. | |
| 139 mChildren.add(mStatus); // Needs to be refreshed when the status changes
. | |
| 140 | |
| 141 mChildren.add(mMoreButton); // Needs to be refreshed when the suggestion
s change. | |
| 142 mChildren.add(mProgressIndicator); // Needs to be refreshed when the sug
gestions change. | |
| 143 refreshChildrenVisibility(); | |
| 144 } | |
| 145 | |
| 146 private void setupOfflinePageBridgeObserver(NewTabPageManager manager) { | 148 private void setupOfflinePageBridgeObserver(NewTabPageManager manager) { |
| 147 final OfflinePageBridge.OfflinePageModelObserver observer = | 149 final OfflinePageBridge.OfflinePageModelObserver observer = |
| 148 new OfflinePageBridge.OfflinePageModelObserver() { | 150 new OfflinePageBridge.OfflinePageModelObserver() { |
| 149 @Override | 151 @Override |
| 150 public void offlinePageModelLoaded() { | 152 public void offlinePageModelLoaded() { |
| 151 updateAllSnippetOfflineAvailability(); | 153 updateAllSnippetOfflineAvailability(); |
| 152 } | 154 } |
| 153 | 155 |
| 154 @Override | 156 @Override |
| 155 public void offlinePageAdded(OfflinePageItem addedPage) { | 157 public void offlinePageAdded(OfflinePageItem addedPage) { |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 @VisibleForTesting | 332 @VisibleForTesting |
| 331 ActionItem getActionItem() { | 333 ActionItem getActionItem() { |
| 332 return mMoreButton; | 334 return mMoreButton; |
| 333 } | 335 } |
| 334 | 336 |
| 335 @VisibleForTesting | 337 @VisibleForTesting |
| 336 StatusItem getStatusItem() { | 338 StatusItem getStatusItem() { |
| 337 return mStatus; | 339 return mStatus; |
| 338 } | 340 } |
| 339 } | 341 } |
| OLD | NEW |