Chromium Code Reviews| Index: ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm |
| diff --git a/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm b/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm |
| index 6d5c0700309de111532350438604c0132b6f8f92..4b69d9c8ebd5760244b87cdc2798c844b85c89c9 100644 |
| --- a/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm |
| +++ b/ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm |
| @@ -7,6 +7,7 @@ |
| #include "base/logging.h" |
| #include "base/mac/foundation_util.h" |
| #include "base/time/time.h" |
| +#include "components/strings/grit/components_strings.h" |
| #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h" |
| #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| @@ -16,6 +17,7 @@ |
| #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_favicon_item.h" |
| #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_footer_item.h" |
| #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_stack_item.h" |
| +#import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_text_item.h" |
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink.h" |
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_source.h" |
| @@ -23,6 +25,7 @@ |
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_controller.h" |
| #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion_identifier.h" |
| #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestions_section_information.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| #include "ui/gfx/image/image.h" |
| #include "url/gurl.h" |
| @@ -32,6 +35,10 @@ |
| namespace { |
| +using CSCollectionViewItem = |
| + CollectionViewItem<ContentSuggestionIdentification>; |
| +using CSCollectionViewModel = CollectionViewModel<CSCollectionViewItem*>; |
| + |
| // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. |
| typedef NS_ENUM(NSInteger, ItemType) { |
| ItemTypeText = kItemTypeEnumZero, |
| @@ -41,6 +48,7 @@ typedef NS_ENUM(NSInteger, ItemType) { |
| ItemTypeFavicon, |
| ItemTypeFooter, |
| ItemTypeHeader, |
| + ItemTypeEmpty, |
| }; |
| typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| @@ -53,16 +61,20 @@ ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { |
| switch (type) { |
| case ContentSuggestionTypeArticle: |
| return ItemTypeArticle; |
| + case ContentSuggestionTypeEmpty: |
| + return ItemTypeEmpty; |
| } |
| } |
| ContentSuggestionType ContentSuggestionTypeForItemType(NSInteger type) { |
| if (type == ItemTypeArticle) |
| return ContentSuggestionTypeArticle; |
| + if (type == ItemTypeEmpty) |
| + return ContentSuggestionTypeEmpty; |
| // Add new type here |
| // Default type. |
| - return ContentSuggestionTypeArticle; |
| + return ContentSuggestionTypeEmpty; |
| } |
| // Returns the section identifier corresponding to the section |info|. |
| @@ -124,7 +136,7 @@ SectionIdentifier SectionIdentifierForInfo( |
| (ContentSuggestionsSectionInformation*)sectionInfo { |
| SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| - CollectionViewModel* model = |
| + CSCollectionViewModel* model = |
| self.collectionViewController.collectionViewModel; |
| if ([model hasSectionForSectionIdentifier:sectionIdentifier] && |
| [model itemsInSectionWithIdentifier:sectionIdentifier].count > 0) { |
| @@ -144,13 +156,12 @@ SectionIdentifier SectionIdentifierForInfo( |
| return; |
| } |
| - NSArray<CollectionViewItem<ContentSuggestionIdentification>*>* |
| - itemsInSection = [self.collectionViewController.collectionViewModel |
| + NSArray<CSCollectionViewItem*>* itemsInSection = |
| + [self.collectionViewController.collectionViewModel |
| itemsInSectionWithIdentifier:sectionIdentifier]; |
| - CollectionViewItem<ContentSuggestionIdentification>* correspondingItem = nil; |
| - for (CollectionViewItem<ContentSuggestionIdentification>* item in |
| - itemsInSection) { |
| + CSCollectionViewItem* correspondingItem = nil; |
| + for (CSCollectionViewItem* item in itemsInSection) { |
| if (item.suggestionIdentifier == suggestionIdentifier) { |
| correspondingItem = item; |
| break; |
| @@ -206,33 +217,47 @@ SectionIdentifier SectionIdentifierForInfo( |
| return [NSArray array]; |
| } |
| - CollectionViewModel* model = |
| - self.collectionViewController.collectionViewModel; |
| - |
| NSMutableArray<NSIndexPath*>* indexPaths = [NSMutableArray array]; |
| for (ContentSuggestion* suggestion in suggestions) { |
| - NSInteger sectionIdentifier = |
| - SectionIdentifierForInfo(suggestion.suggestionIdentifier.sectionInfo); |
| - |
| - ContentSuggestionsArticleItem* articleItem = |
| - [[ContentSuggestionsArticleItem alloc] |
| - initWithType:ItemTypeForContentSuggestionType(suggestion.type) |
| - title:suggestion.title |
| - subtitle:suggestion.text |
| - delegate:self |
| - url:suggestion.url]; |
| - |
| - articleItem.publisher = suggestion.publisher; |
| - articleItem.publishDate = suggestion.publishDate; |
| - |
| - articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; |
| - |
| - NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| - NSInteger itemNumber = [model numberOfItemsInSection:section]; |
| - [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier]; |
| - |
| - [indexPaths |
| - addObject:[NSIndexPath indexPathForItem:itemNumber inSection:section]]; |
| + ContentSuggestionsSectionInformation* sectionInfo = |
| + suggestion.suggestionIdentifier.sectionInfo; |
| + NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| + CSCollectionViewModel* model = |
| + self.collectionViewController.collectionViewModel; |
| + |
| + switch (suggestion.type) { |
| + case ContentSuggestionTypeEmpty: { |
| + if ([model hasSectionForSectionIdentifier:sectionIdentifier] && |
| + [model numberOfItemsInSection:[model sectionForSectionIdentifier: |
| + sectionIdentifier]] == 0) { |
| + CSCollectionViewItem* item = |
| + [self emptyItemForSectionInfo:sectionInfo]; |
| + NSIndexPath* addedIndexPath = |
| + [self addItem:item toSectionWithIdentifier:sectionIdentifier]; |
|
lpromero
2017/03/23 10:54:37
Is there a code path that would have two empty ite
gambard
2017/03/23 15:25:49
Yes. This is why I am testing to know if the secti
|
| + [indexPaths addObject:addedIndexPath]; |
|
lpromero
2017/03/23 10:54:37
You could reuse -addEmptyItemForSection: once it r
gambard
2017/03/23 15:25:49
I am not sure it makes sense here addEmptyItemForS
|
| + } |
| + break; |
| + } |
| + case ContentSuggestionTypeArticle: { |
| + ContentSuggestionsArticleItem* articleItem = |
| + [[ContentSuggestionsArticleItem alloc] |
| + initWithType:ItemTypeForContentSuggestionType(suggestion.type) |
| + title:suggestion.title |
| + subtitle:suggestion.text |
| + delegate:self |
| + url:suggestion.url]; |
| + |
| + articleItem.publisher = suggestion.publisher; |
| + articleItem.publishDate = suggestion.publishDate; |
| + |
| + articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; |
| + |
| + NSIndexPath* addedIndexPath = [self addItem:articleItem |
| + toSectionWithIdentifier:sectionIdentifier]; |
| + [indexPaths addObject:addedIndexPath]; |
| + break; |
| + } |
| + } |
| } |
| return indexPaths; |
| @@ -242,25 +267,39 @@ SectionIdentifier SectionIdentifierForInfo( |
| (NSArray<ContentSuggestion*>*)suggestions { |
| NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet]; |
| - CollectionViewModel* model = |
| + CSCollectionViewModel* model = |
| self.collectionViewController.collectionViewModel; |
| for (ContentSuggestion* suggestion in suggestions) { |
| ContentSuggestionsSectionInformation* sectionInfo = |
| suggestion.suggestionIdentifier.sectionInfo; |
| NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| - if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { |
| - [model addSectionWithIdentifier:sectionIdentifier]; |
| - self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; |
| - [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; |
| - |
| - [self addHeader:suggestion.suggestionIdentifier.sectionInfo]; |
| - [self addFooterIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; |
| + if ([model hasSectionForSectionIdentifier:sectionIdentifier] || |
| + (suggestion.type == ContentSuggestionTypeEmpty && |
| + !sectionInfo.showIfEmpty)) { |
| + continue; |
| } |
| + |
| + [model addSectionWithIdentifier:sectionIdentifier]; |
| + self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; |
| + [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; |
| + |
| + [self addHeader:sectionInfo]; |
| + [self addFooterIfNeeded:sectionInfo]; |
| } |
| return indexSet; |
| } |
| +- (void)addEmptyItemForSection:(NSInteger)section { |
|
lpromero
2017/03/23 10:54:37
Return the index path of the newly added item.
gambard
2017/03/23 15:25:49
Done.
|
| + CSCollectionViewModel* model = |
| + self.collectionViewController.collectionViewModel; |
| + NSInteger sectionIdentifier = [model sectionIdentifierForSection:section]; |
| + ContentSuggestionsSectionInformation* sectionInfo = |
| + self.sectionInfoBySectionIdentifier[@(sectionIdentifier)]; |
| + CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo]; |
| + [model addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| +} |
| + |
| #pragma mark - ContentSuggestionsArticleItemDelegate |
| - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
| @@ -345,12 +384,13 @@ SectionIdentifier SectionIdentifierForInfo( |
| NSMutableArray<ContentSuggestionIdentifier*>* knownSuggestionIdentifiers = |
| [NSMutableArray array]; |
| - NSArray<CollectionViewItem<ContentSuggestionIdentification>*>* |
| - knownSuggestions = [self.collectionViewController.collectionViewModel |
| + NSArray<CSCollectionViewItem*>* knownSuggestions = |
| + [self.collectionViewController.collectionViewModel |
| itemsInSectionWithIdentifier:sectionIdentifier]; |
| - for (CollectionViewItem<ContentSuggestionIdentification>* suggestion in |
| - knownSuggestions) { |
| - [knownSuggestionIdentifiers addObject:suggestion.suggestionIdentifier]; |
| + for (CSCollectionViewItem* suggestion in knownSuggestions) { |
| + if (suggestion.type != ItemTypeEmpty) { |
| + [knownSuggestionIdentifiers addObject:suggestion.suggestionIdentifier]; |
| + } |
| } |
| __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| @@ -368,4 +408,29 @@ SectionIdentifier SectionIdentifierForInfo( |
| [self.collectionViewController addSuggestions:suggestions]; |
| } |
| +// Returns a item to be displayed when the section identified by |sectionInfo| |
| +// is empty. |
| +- (CSCollectionViewItem*)emptyItemForSectionInfo: |
| + (ContentSuggestionsSectionInformation*)sectionInfo { |
| + ContentSuggestionsTextItem* item = |
| + [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty]; |
| + item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS); |
| + item.detailText = sectionInfo.emptyText; |
| + |
| + return item; |
| +} |
| + |
| +// Adds |item| to |sectionIdentifier| section of the model of the |
| +// CollectionView. Returns the IndexPath of the newly added item. |
| +- (NSIndexPath*)addItem:(CSCollectionViewItem*)item |
| + toSectionWithIdentifier:(NSInteger)sectionIdentifier { |
| + CSCollectionViewModel* model = |
| + self.collectionViewController.collectionViewModel; |
| + NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| + NSInteger itemNumber = [model numberOfItemsInSection:section]; |
| + [model addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| + |
| + return [NSIndexPath indexPathForItem:itemNumber inSection:section]; |
| +} |
| + |
| @end |