Index: chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSection.java |
diff --git a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSection.java b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSection.java |
index 63482506e42039b7365ad72cc92c74231d399186..7c8f5dcba3f59f51c5f6be1cb51e70efe43b9476 100644 |
--- a/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSection.java |
+++ b/chrome/android/java/src/org/chromium/chrome/browser/ntp/cards/SuggestionsSection.java |
@@ -28,11 +28,24 @@ |
private final ActionItem mMoreButton; |
@CategoryInt |
private final int mCategory; |
+ private final Observer mObserver; |
public SuggestionsSection(@CategoryInt int category, SuggestionsCategoryInfo info, |
final NewTabPageAdapter adapter) { |
+ this(category, info, adapter, new ActionDelegate() { |
+ @Override |
+ public void onButtonTapped() { |
+ adapter.reloadSnippets(); |
+ } |
+ }); |
+ } |
+ |
+ @VisibleForTesting |
+ SuggestionsSection(@CategoryInt int category, SuggestionsCategoryInfo info, Observer observer, |
+ ActionDelegate actionDelegate) { |
mHeader = new SectionHeader(info.getTitle()); |
mCategory = category; |
+ mObserver = observer; |
// TODO(dgn): Properly define strings, actions, etc. for each section and category type. |
if (info.hasMoreButton()) { |
@@ -40,12 +53,7 @@ public SuggestionsSection(@CategoryInt int category, SuggestionsCategoryInfo inf |
mActionDelegate = null; |
} else { |
mMoreButton = null; |
- mActionDelegate = new ActionDelegate() { |
- @Override |
- public void onButtonTapped() { |
- adapter.reloadSnippets(); |
- } |
- }; |
+ mActionDelegate = actionDelegate; |
} |
} |
@@ -63,8 +71,20 @@ public void onButtonTapped() { |
} |
public void removeSuggestion(SnippetArticle suggestion) { |
- mSuggestions.remove(suggestion); |
+ int removedIndex = mSuggestions.indexOf(suggestion); |
+ if (removedIndex == -1) return; |
+ mSuggestions.remove(removedIndex); |
if (mMoreButton != null) mMoreButton.setDismissable(!hasSuggestions()); |
+ if (hasSuggestions()) { |
+ mObserver.notifyItemRemoved( |
+ this, removedIndex + 1 /* Header has index 0 in the section */); |
+ } else { |
+ int itemCountAfter = getItems().size(); |
+ // When we remove the last suggestion, we add 2 items: status card and progress |
+ // indicator, so the count after has one more item than the count before. |
+ int itemCountBefore = itemCountAfter - 1; |
+ mObserver.notifyGroupChanged(this, itemCountBefore, itemCountAfter); |
+ } |
} |
public void removeSuggestionById(String suggestionId) { |
@@ -87,7 +107,8 @@ public int getSuggestionsCount() { |
public void setSuggestions(List<SnippetArticle> suggestions, @CategoryStatusEnum int status) { |
copyThumbnails(suggestions); |
- setStatus(status); |
+ int itemCountBefore = getItems().size(); |
+ setStatusInternal(status); |
mSuggestions.clear(); |
mSuggestions.addAll(suggestions); |
@@ -95,10 +116,17 @@ public void setSuggestions(List<SnippetArticle> suggestions, @CategoryStatusEnum |
if (mMoreButton != null) { |
mMoreButton.setPosition(mSuggestions.size()); |
} |
+ mObserver.notifyGroupChanged(this, itemCountBefore, getItems().size()); |
} |
/** Sets the status for the section. Some statuses can cause the suggestions to be cleared. */ |
public void setStatus(@CategoryStatusEnum int status) { |
+ int itemCountBefore = getItems().size(); |
+ setStatusInternal(status); |
+ mObserver.notifyGroupChanged(this, itemCountBefore, getItems().size()); |
+ } |
+ |
+ private void setStatusInternal(@CategoryStatusEnum int status) { |
mStatus = StatusItem.create(status, mActionDelegate); |
if (!SnippetsBridge.isCategoryStatusAvailable(status)) mSuggestions.clear(); |