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

Unified Diff: components/ntp_snippets/content_suggestions_service.cc

Issue 2277743002: [NTP Snippets] Always show the bookmarks section, but at the end if it's empty (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@fix_proguard
Patch Set: fix Linux debug build Created 4 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: components/ntp_snippets/content_suggestions_service.cc
diff --git a/components/ntp_snippets/content_suggestions_service.cc b/components/ntp_snippets/content_suggestions_service.cc
index 1da49f5cfd53dda8781c150fa113ec6be6c49cc9..5a983f423832d440025859ada0dec20eeb99ca3f 100644
--- a/components/ntp_snippets/content_suggestions_service.cc
+++ b/components/ntp_snippets/content_suggestions_service.cc
@@ -179,6 +179,11 @@ void ContentSuggestionsService::OnNewSuggestions(
suggestions_by_category_[category] = std::move(new_suggestions);
+ // The positioning of the bookmarks category depends on whether it's empty.
+ // TODO(treib): Remove this temporary hack, crbug.com/640568.
+ if (category.IsKnownCategory(KnownCategories::BOOKMARKS))
+ SortCategories();
+
FOR_EACH_OBSERVER(Observer, observers_, OnNewSuggestions(category));
}
@@ -227,10 +232,7 @@ bool ContentSuggestionsService::RegisterCategoryIfRequired(
providers_by_category_[category] = provider;
categories_.push_back(category);
- std::sort(categories_.begin(), categories_.end(),
- [this](const Category& left, const Category& right) {
- return category_factory_.CompareCategories(left, right);
- });
+ SortCategories();
if (IsCategoryStatusAvailable(provider->GetCategoryStatus(category))) {
suggestions_by_category_.insert(
std::make_pair(category, std::vector<ContentSuggestion>()));
@@ -252,6 +254,12 @@ bool ContentSuggestionsService::RemoveSuggestionByID(
if (position == suggestions->end())
return false;
suggestions->erase(position);
+
+ // The positioning of the bookmarks category depends on whether it's empty.
+ // TODO(treib): Remove this temporary hack, crbug.com/640568.
+ if (category.IsKnownCategory(KnownCategories::BOOKMARKS))
+ SortCategories();
+
return true;
}
@@ -261,4 +269,24 @@ void ContentSuggestionsService::NotifyCategoryStatusChanged(Category category) {
OnCategoryStatusChanged(category, GetCategoryStatus(category)));
}
+void ContentSuggestionsService::SortCategories() {
+ auto it = suggestions_by_category_.find(
+ category_factory_.FromKnownCategory(KnownCategories::BOOKMARKS));
+ bool bookmarks_empty =
+ (it == suggestions_by_category_.end() || it->second.empty());
+ std::sort(
+ categories_.begin(), categories_.end(),
+ [this, bookmarks_empty](const Category& left, const Category& right) {
+ // If the bookmarks section is empty, put it at the end.
+ // TODO(treib): This is a temporary hack, see crbug.com/640568.
+ if (bookmarks_empty) {
+ if (left.IsKnownCategory(KnownCategories::BOOKMARKS))
+ return false;
+ if (right.IsKnownCategory(KnownCategories::BOOKMARKS))
+ return true;
+ }
+ return category_factory_.CompareCategories(left, right);
+ });
+}
+
} // namespace ntp_snippets
« no previous file with comments | « components/ntp_snippets/content_suggestions_service.h ('k') | components/ntp_snippets/content_suggestions_service_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698