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; |
(...skipping 17 matching lines...) Expand all Loading... |
28 /** | 28 /** |
29 * A group of suggestions, with a header, a status card, and a progress indicato
r. This is | 29 * A group of suggestions, with a header, a status card, and a progress indicato
r. This is |
30 * responsible for tracking whether its suggestions have been saved offline. | 30 * responsible for tracking whether its suggestions have been saved offline. |
31 */ | 31 */ |
32 public class SuggestionsSection extends InnerNode { | 32 public class SuggestionsSection extends InnerNode { |
33 private static final String TAG = "NtpCards"; | 33 private static final String TAG = "NtpCards"; |
34 | 34 |
35 private final Delegate mDelegate; | 35 private final Delegate mDelegate; |
36 private final SuggestionsCategoryInfo mCategoryInfo; | 36 private final SuggestionsCategoryInfo mCategoryInfo; |
37 private final OfflinePageBridge mOfflinePageBridge; | 37 private final OfflinePageBridge mOfflinePageBridge; |
38 private final SuggestionsRanker mSuggestionsRanker; | |
39 | 38 |
40 // Children | 39 // Children |
41 private final SectionHeader mHeader; | 40 private final SectionHeader mHeader; |
42 private final SuggestionsList mSuggestionsList; | 41 private final SuggestionsList mSuggestionsList; |
43 private final StatusItem mStatus; | 42 private final StatusItem mStatus; |
44 private final ActionItem mMoreButton; | 43 private final ActionItem mMoreButton; |
45 private final ProgressItem mProgressIndicator; | 44 private final ProgressItem mProgressIndicator; |
46 | 45 |
47 private boolean mIsNtpDestroyed; | 46 private boolean mIsNtpDestroyed; |
48 | 47 |
(...skipping 11 matching lines...) Expand all Loading... |
60 public interface Delegate { | 59 public interface Delegate { |
61 void dismissSection(SuggestionsSection section); | 60 void dismissSection(SuggestionsSection section); |
62 } | 61 } |
63 | 62 |
64 public SuggestionsSection(Delegate delegate, NewTabPageManager manager, | 63 public SuggestionsSection(Delegate delegate, NewTabPageManager manager, |
65 SuggestionsRanker ranker, OfflinePageBridge offlinePageBridge, | 64 SuggestionsRanker ranker, OfflinePageBridge offlinePageBridge, |
66 SuggestionsCategoryInfo info) { | 65 SuggestionsCategoryInfo info) { |
67 mDelegate = delegate; | 66 mDelegate = delegate; |
68 mCategoryInfo = info; | 67 mCategoryInfo = info; |
69 mOfflinePageBridge = offlinePageBridge; | 68 mOfflinePageBridge = offlinePageBridge; |
70 mSuggestionsRanker = ranker; | |
71 | 69 |
72 mHeader = new SectionHeader(info.getTitle()); | 70 mHeader = new SectionHeader(info.getTitle()); |
73 mSuggestionsList = new SuggestionsList(manager, info); | 71 mSuggestionsList = new SuggestionsList(manager, ranker, info); |
74 mStatus = StatusItem.createNoSuggestionsItem(info); | 72 mStatus = StatusItem.createNoSuggestionsItem(info); |
75 mMoreButton = new ActionItem(this, ranker); | 73 mMoreButton = new ActionItem(this, ranker); |
76 mProgressIndicator = new ProgressItem(); | 74 mProgressIndicator = new ProgressItem(); |
77 addChildren(mHeader, mSuggestionsList, mStatus, mMoreButton, mProgressIn
dicator); | 75 addChildren(mHeader, mSuggestionsList, mStatus, mMoreButton, mProgressIn
dicator); |
78 | 76 |
79 setupOfflinePageBridgeObserver(manager); | 77 setupOfflinePageBridgeObserver(manager); |
80 refreshChildrenVisibility(); | 78 refreshChildrenVisibility(); |
81 } | 79 } |
82 | 80 |
83 private static class SuggestionsList extends ChildNode implements Iterable<S
nippetArticle> { | 81 private static class SuggestionsList extends ChildNode implements Iterable<S
nippetArticle> { |
84 private final List<SnippetArticle> mSuggestions = new ArrayList<>(); | 82 private final List<SnippetArticle> mSuggestions = new ArrayList<>(); |
85 private final NewTabPageManager mNewTabPageManager; | 83 private final NewTabPageManager mNewTabPageManager; |
| 84 private final SuggestionsRanker mSuggestionsRanker; |
86 private final SuggestionsCategoryInfo mCategoryInfo; | 85 private final SuggestionsCategoryInfo mCategoryInfo; |
87 | 86 |
88 public SuggestionsList(NewTabPageManager newTabPageManager, | 87 public SuggestionsList(NewTabPageManager newTabPageManager, |
89 SuggestionsCategoryInfo categoryInfo) { | 88 SuggestionsRanker ranker, SuggestionsCategoryInfo categoryInfo)
{ |
90 mNewTabPageManager = newTabPageManager; | 89 mNewTabPageManager = newTabPageManager; |
| 90 mSuggestionsRanker = ranker; |
91 mCategoryInfo = categoryInfo; | 91 mCategoryInfo = categoryInfo; |
92 } | 92 } |
93 | 93 |
94 @Override | 94 @Override |
95 public int getItemCount() { | 95 public int getItemCount() { |
96 return mSuggestions.size(); | 96 return mSuggestions.size(); |
97 } | 97 } |
98 | 98 |
99 @Override | 99 @Override |
100 @ItemViewType | 100 @ItemViewType |
101 public int getItemViewType(int position) { | 101 public int getItemViewType(int position) { |
102 checkIndex(position); | 102 checkIndex(position); |
103 return ItemViewType.SNIPPET; | 103 return ItemViewType.SNIPPET; |
104 } | 104 } |
105 | 105 |
106 @Override | 106 @Override |
107 public void onBindViewHolder( | 107 public void onBindViewHolder( |
108 NewTabPageViewHolder holder, int position, List<Object> payloads
) { | 108 NewTabPageViewHolder holder, int position, List<Object> payloads
) { |
109 checkIndex(position); | 109 checkIndex(position); |
110 assert holder instanceof SnippetArticleViewHolder; | 110 assert holder instanceof SnippetArticleViewHolder; |
| 111 SnippetArticle suggestion = getSuggestionAt(position); |
| 112 mSuggestionsRanker.rankSuggestion(suggestion); |
111 ((SnippetArticleViewHolder) holder) | 113 ((SnippetArticleViewHolder) holder) |
112 .onBindViewHolder(getSuggestionAt(position), mCategoryInfo,
payloads); | 114 .onBindViewHolder(suggestion, mCategoryInfo, payloads); |
113 } | 115 } |
114 | 116 |
115 @Override | 117 @Override |
116 public SnippetArticle getSuggestionAt(int position) { | 118 public SnippetArticle getSuggestionAt(int position) { |
117 return mSuggestions.get(position); | 119 return mSuggestions.get(position); |
118 } | 120 } |
119 | 121 |
120 @Override | 122 @Override |
121 public int getDismissSiblingPosDelta(int position) { | 123 public int getDismissSiblingPosDelta(int position) { |
122 checkIndex(position); | 124 checkIndex(position); |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
346 Log.d(TAG, "setSuggestions: removing all suggestions"); | 348 Log.d(TAG, "setSuggestions: removing all suggestions"); |
347 mSuggestionsList.clear(); | 349 mSuggestionsList.clear(); |
348 } | 350 } |
349 } | 351 } |
350 | 352 |
351 mProgressIndicator.setVisible(SnippetsBridge.isCategoryLoading(status)); | 353 mProgressIndicator.setVisible(SnippetsBridge.isCategoryLoading(status)); |
352 | 354 |
353 mSuggestionsList.addAll(suggestions); | 355 mSuggestionsList.addAll(suggestions); |
354 | 356 |
355 for (SnippetArticle article : suggestions) { | 357 for (SnippetArticle article : suggestions) { |
356 mSuggestionsRanker.rankSuggestion(article); | |
357 if (!article.requiresExactOfflinePage()) { | 358 if (!article.requiresExactOfflinePage()) { |
358 updateSnippetOfflineAvailability(article); | 359 updateSnippetOfflineAvailability(article); |
359 } | 360 } |
360 } | 361 } |
361 | 362 |
362 refreshChildrenVisibility(); | 363 refreshChildrenVisibility(); |
363 } | 364 } |
364 | 365 |
365 private void updateSnippetOfflineAvailability(final SnippetArticle article)
{ | 366 private void updateSnippetOfflineAvailability(final SnippetArticle article)
{ |
366 // This method is not applicable to articles for which the exact offline
id must specified. | 367 // This method is not applicable to articles for which the exact offline
id must specified. |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
446 @VisibleForTesting | 447 @VisibleForTesting |
447 ActionItem getActionItem() { | 448 ActionItem getActionItem() { |
448 return mMoreButton; | 449 return mMoreButton; |
449 } | 450 } |
450 | 451 |
451 @VisibleForTesting | 452 @VisibleForTesting |
452 StatusItem getStatusItem() { | 453 StatusItem getStatusItem() { |
453 return mStatus; | 454 return mStatus; |
454 } | 455 } |
455 } | 456 } |
OLD | NEW |