| 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 9bce87675c3a920f5ac961ccab2f41921f845a03..83866cc2b435f2d28cee5b235171caf7f5b8dad3 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
|
| @@ -87,13 +87,6 @@ SectionIdentifier SectionIdentifierForInfo(
|
| NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>*
|
| sectionInfoBySectionIdentifier;
|
|
|
| -// Adds a new section if needed and returns the section identifier.
|
| -- (NSInteger)addSectionIfNeeded:
|
| - (ContentSuggestionsSectionInformation*)sectionInformation;
|
| -// Resets the models, removing the current CollectionViewItem and the
|
| -// SectionInfo.
|
| -- (void)resetModels;
|
| -
|
| @end
|
|
|
| @implementation ContentSuggestionsCollectionUpdater
|
| @@ -135,7 +128,8 @@ SectionIdentifier SectionIdentifierForInfo(
|
| return;
|
| }
|
|
|
| - [self addSuggestions:[self.dataSource suggestionsForSection:sectionInfo]];
|
| + [self.collectionViewController
|
| + addSuggestions:[self.dataSource suggestionsForSection:sectionInfo]];
|
| }
|
|
|
| - (void)clearSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier {
|
| @@ -170,7 +164,8 @@ SectionIdentifier SectionIdentifierForInfo(
|
|
|
| - (void)reloadAllData {
|
| [self resetModels];
|
| - [self addSuggestions:[self.dataSource allSuggestions]];
|
| + [self.collectionViewController
|
| + addSuggestions:[self.dataSource allSuggestions]];
|
| }
|
|
|
| - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo {
|
| @@ -196,50 +191,20 @@ SectionIdentifier SectionIdentifierForInfo(
|
| return ContentSuggestionTypeForItemType(item.type);
|
| }
|
|
|
| -#pragma mark - ContentSuggestionsArticleItemDelegate
|
| -
|
| -- (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem {
|
| - NSInteger sectionIdentifier =
|
| - SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo);
|
| -
|
| - __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
|
| - __weak ContentSuggestionsArticleItem* weakArticle = articleItem;
|
| - void (^imageFetchedCallback)(const gfx::Image&) = ^(const gfx::Image& image) {
|
| - if (image.IsEmpty()) {
|
| - return;
|
| - }
|
| -
|
| - ContentSuggestionsCollectionUpdater* strongSelf = weakSelf;
|
| - ContentSuggestionsArticleItem* strongArticle = weakArticle;
|
| - if (!strongSelf || !strongArticle) {
|
| - return;
|
| - }
|
| -
|
| - strongArticle.image = image.CopyUIImage();
|
| - [strongSelf.collectionViewController
|
| - reconfigureCellsForItems:@[ strongArticle ]
|
| - inSectionWithIdentifier:sectionIdentifier];
|
| - };
|
| -
|
| - [self.dataSource.imageFetcher
|
| - fetchImageForSuggestion:articleItem.suggestionIdentifier
|
| - callback:imageFetchedCallback];
|
| -}
|
| -
|
| -#pragma mark - Private methods
|
| -
|
| -// Add the |suggestions| to the model and reload the data.
|
| -- (void)addSuggestions:(NSArray<ContentSuggestion*>*)suggestions {
|
| +- (NSArray<NSIndexPath*>*)addSuggestionsToModel:
|
| + (NSArray<ContentSuggestion*>*)suggestions {
|
| if (suggestions.count == 0) {
|
| - return;
|
| + return [NSArray array];
|
| }
|
|
|
| CollectionViewModel* model =
|
| self.collectionViewController.collectionViewModel;
|
|
|
| + NSMutableArray<NSIndexPath*>* indexPaths = [NSMutableArray array];
|
| for (ContentSuggestion* suggestion in suggestions) {
|
| NSInteger sectionIdentifier =
|
| - [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo];
|
| + SectionIdentifierForInfo(suggestion.suggestionIdentifier.sectionInfo);
|
| +
|
| ContentSuggestionsArticleItem* articleItem =
|
| [[ContentSuggestionsArticleItem alloc]
|
| initWithType:ItemTypeForContentSuggestionType(suggestion.type)
|
| @@ -253,30 +218,71 @@ SectionIdentifier SectionIdentifierForInfo(
|
|
|
| articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
|
|
|
| + NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
|
| + NSInteger itemNumber = [model numberOfItemsInSection:section];
|
| [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
|
| - }
|
|
|
| - if ([self.collectionViewController isViewLoaded]) {
|
| - [self.collectionViewController.collectionView reloadData];
|
| + [indexPaths
|
| + addObject:[NSIndexPath indexPathForItem:itemNumber inSection:section]];
|
| }
|
| +
|
| + return indexPaths;
|
| }
|
|
|
| -- (NSInteger)addSectionIfNeeded:
|
| - (ContentSuggestionsSectionInformation*)sectionInformation {
|
| - NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation);
|
| +- (NSIndexSet*)addSectionsForSuggestionsToModel:
|
| + (NSArray<ContentSuggestion*>*)suggestions {
|
| + NSMutableIndexSet* indexSet = [NSMutableIndexSet indexSet];
|
|
|
| CollectionViewModel* model =
|
| self.collectionViewController.collectionViewModel;
|
| - if (![model hasSectionForSectionIdentifier:sectionIdentifier]) {
|
| - [model addSectionWithIdentifier:sectionIdentifier];
|
| - self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] =
|
| - sectionInformation;
|
| - [self.sectionInfoBySectionIdentifier setObject:sectionInformation
|
| - forKey:@(sectionIdentifier)];
|
| + 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]];
|
| + }
|
| }
|
| - return sectionIdentifier;
|
| + return indexSet;
|
| }
|
|
|
| +#pragma mark - ContentSuggestionsArticleItemDelegate
|
| +
|
| +- (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem {
|
| + NSInteger sectionIdentifier =
|
| + SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo);
|
| +
|
| + __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
|
| + __weak ContentSuggestionsArticleItem* weakArticle = articleItem;
|
| + void (^imageFetchedCallback)(const gfx::Image&) = ^(const gfx::Image& image) {
|
| + if (image.IsEmpty()) {
|
| + return;
|
| + }
|
| +
|
| + ContentSuggestionsCollectionUpdater* strongSelf = weakSelf;
|
| + ContentSuggestionsArticleItem* strongArticle = weakArticle;
|
| + if (!strongSelf || !strongArticle) {
|
| + return;
|
| + }
|
| +
|
| + strongArticle.image = image.CopyUIImage();
|
| + [strongSelf.collectionViewController
|
| + reconfigureCellsForItems:@[ strongArticle ]
|
| + inSectionWithIdentifier:sectionIdentifier];
|
| + };
|
| +
|
| + [self.dataSource.imageFetcher
|
| + fetchImageForSuggestion:articleItem.suggestionIdentifier
|
| + callback:imageFetchedCallback];
|
| +}
|
| +
|
| +#pragma mark - Private methods
|
| +
|
| +// Resets the models, removing the current CollectionViewItem and the
|
| +// SectionInfo.
|
| - (void)resetModels {
|
| [self.collectionViewController loadModel];
|
| self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init];
|
|
|