| 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 14c8198cab1183931c85438c4df266ac764e3098..081e3570e113b1d469994a8721d1458880293c0f 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
|
| @@ -8,12 +8,15 @@
|
| #include "base/mac/foundation_util.h"
|
| #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
|
| #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
|
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h"
|
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_item.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"
|
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandable_item.h"
|
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_item.h"
|
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_updater.h"
|
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h"
|
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_information.h"
|
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_item.h"
|
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_controller.h"
|
| #include "url/gurl.h"
|
| @@ -22,9 +25,47 @@
|
| #error "This file requires ARC support."
|
| #endif
|
|
|
| -@interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink>
|
| +namespace {
|
| +
|
| +typedef NS_ENUM(NSInteger, SectionIdentifier) {
|
| + SectionIdentifierBookmarks = kSectionIdentifierEnumZero,
|
| + SectionIdentifierArticles,
|
| + SectionIdentifierDefault,
|
| +};
|
| +
|
| +// Returns the section identifier corresponding to the section |info|.
|
| +SectionIdentifier SectionIdentifierForInfo(
|
| + ContentSuggestionsSectionInformation* info) {
|
| + switch (info.ID) {
|
| + case ContentSuggestionsSectionBookmarks:
|
| + return SectionIdentifierBookmarks;
|
| +
|
| + case ContentSuggestionsSectionArticles:
|
| + return SectionIdentifierArticles;
|
| +
|
| + case ContentSuggestionsSectionCount:
|
| + return SectionIdentifierDefault;
|
| + }
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| +@interface ContentSuggestionsCollectionUpdater ()<
|
| + ContentSuggestionsDataSink,
|
| + ContentSuggestionsImageUpdaterDelegate>
|
|
|
| @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource;
|
| +@property(nonatomic, strong)
|
| + NSMutableDictionary* sectionInfoBySectionIdentifier;
|
| +@property(nonatomic, strong) NSMutableSet* imageUpdaterSet;
|
| +
|
| +// Reloads all the data from the data source, deleting all the current items.
|
| +- (void)reloadData;
|
| +// Adds a new section if needed and returns the section identifier.
|
| +- (NSInteger)addSectionIfNeeded:
|
| + (ContentSuggestionsSectionInformation*)sectionInformation;
|
| +// Resets the models and cells.
|
| +- (void)resetModels;
|
|
|
| @end
|
|
|
| @@ -32,6 +73,8 @@
|
|
|
| @synthesize collectionViewController = _collectionViewController;
|
| @synthesize dataSource = _dataSource;
|
| +@synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier;
|
| +@synthesize imageUpdaterSet = _imageUpdaterSet;
|
|
|
| - (instancetype)initWithDataSource:
|
| (id<ContentSuggestionsDataSource>)dataSource {
|
| @@ -48,74 +91,14 @@
|
| - (void)setCollectionViewController:
|
| (ContentSuggestionsViewController*)collectionViewController {
|
| _collectionViewController = collectionViewController;
|
| - [collectionViewController loadModel];
|
| - CollectionViewModel* model = collectionViewController.collectionViewModel;
|
| -
|
| - // TODO(crbug.com/686728): Load the data with the dataSource instead of hard
|
| - // coded value.
|
| -
|
| - NSInteger sectionIdentifier = kSectionIdentifierEnumZero;
|
| -
|
| - // Stack Item.
|
| - [model addSectionWithIdentifier:sectionIdentifier];
|
| - [model addItem:[[ContentSuggestionsStackItem alloc]
|
| - initWithType:ItemTypeStack
|
| - title:@"The title"
|
| - subtitle:@"The subtitle"]
|
| - toSectionWithIdentifier:sectionIdentifier++];
|
| -
|
| - // Favicon Item.
|
| - [model addSectionWithIdentifier:sectionIdentifier];
|
| - ContentSuggestionsFaviconItem* faviconItem =
|
| - [[ContentSuggestionsFaviconItem alloc] initWithType:ItemTypeFavicon];
|
| - for (NSInteger i = 0; i < 6; i++) {
|
| - [faviconItem addFavicon:[UIImage imageNamed:@"bookmark_gray_star_large"]
|
| - withTitle:@"Super website! Incredible!"];
|
| - }
|
| - faviconItem.delegate = _collectionViewController;
|
| - [model addItem:faviconItem toSectionWithIdentifier:sectionIdentifier++];
|
|
|
| - for (NSInteger i = 0; i < 3; i++) {
|
| - [model addSectionWithIdentifier:sectionIdentifier];
|
| -
|
| - // Standard Item.
|
| - [model addItem:[[ContentSuggestionsItem alloc] initWithType:ItemTypeText
|
| - title:@"The title"
|
| - subtitle:@"The subtitle"]
|
| - toSectionWithIdentifier:sectionIdentifier];
|
| -
|
| - // Article Item.
|
| - [model addItem:[[ContentSuggestionsArticleItem alloc]
|
| - initWithType:ItemTypeArticle
|
| - title:@"Title of an Article"
|
| - subtitle:@"This is the subtitle of an article, can "
|
| - @"spawn on multiple lines"
|
| - image:[UIImage imageNamed:@"distillation_success"]
|
| - url:GURL()]
|
| - toSectionWithIdentifier:sectionIdentifier];
|
| -
|
| - // Expandable Item.
|
| - SuggestionsExpandableItem* expandableItem =
|
| - [[SuggestionsExpandableItem alloc]
|
| - initWithType:ItemTypeExpand
|
| - title:@"Title of an Expandable Article"
|
| - subtitle:@"This Article can be expanded to display "
|
| - @"additional information or interaction "
|
| - @"options"
|
| - image:[UIImage imageNamed:@"distillation_fail"]
|
| - detailText:@"Details shown only when the article is "
|
| - @"expanded. It can be displayed on "
|
| - @"multiple lines."];
|
| - expandableItem.delegate = _collectionViewController;
|
| - [model addItem:expandableItem toSectionWithIdentifier:sectionIdentifier];
|
| - sectionIdentifier++;
|
| - }
|
| + [self reloadData];
|
| }
|
|
|
| #pragma mark - ContentSuggestionsDataSink
|
|
|
| - (void)dataAvailable {
|
| - // TODO(crbug.com/686728): Get the new data from the DataSource.
|
| + [self reloadData];
|
| }
|
|
|
| #pragma mark - Public methods
|
| @@ -123,40 +106,115 @@
|
| - (void)addTextItem:(NSString*)title
|
| subtitle:(NSString*)subtitle
|
| toSection:(NSInteger)inputSection {
|
| - DCHECK(_collectionViewController);
|
| - ContentSuggestionsItem* item =
|
| - [[ContentSuggestionsItem alloc] initWithType:ItemTypeText
|
| - title:title
|
| - subtitle:subtitle];
|
| + DCHECK(self.collectionViewController);
|
| + ContentSuggestionsItem* item = [[ContentSuggestionsItem alloc]
|
| + initWithType:ContentSuggestionsItemTypeText
|
| + title:title
|
| + subtitle:subtitle];
|
| NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection;
|
| NSInteger sectionIndex = inputSection;
|
| - CollectionViewModel* model = _collectionViewController.collectionViewModel;
|
| + CollectionViewModel* model =
|
| + self.collectionViewController.collectionViewModel;
|
| if ([model numberOfSections] <= inputSection) {
|
| sectionIndex = [model numberOfSections];
|
| sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex;
|
| - [_collectionViewController.collectionView performBatchUpdates:^{
|
| - [_collectionViewController.collectionViewModel
|
| + [self.collectionViewController.collectionView performBatchUpdates:^{
|
| + [self.collectionViewController.collectionViewModel
|
| addSectionWithIdentifier:sectionIdentifier];
|
| - [_collectionViewController.collectionView
|
| + [self.collectionViewController.collectionView
|
| insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
|
| }
|
| - completion:nil];
|
| + completion:nil];
|
| }
|
| NSInteger numberOfItemsInSection =
|
| [model numberOfItemsInSection:sectionIndex];
|
| - [_collectionViewController.collectionViewModel addItem:item
|
| - toSectionWithIdentifier:sectionIdentifier];
|
| - [_collectionViewController.collectionView performBatchUpdates:^{
|
| - [_collectionViewController.collectionView
|
| + [self.collectionViewController.collectionViewModel addItem:item
|
| + toSectionWithIdentifier:sectionIdentifier];
|
| + [self.collectionViewController.collectionView performBatchUpdates:^{
|
| + [self.collectionViewController.collectionView
|
| insertItemsAtIndexPaths:@[ [NSIndexPath
|
| indexPathForRow:numberOfItemsInSection
|
| inSection:sectionIndex] ]];
|
| }
|
| - completion:nil];
|
| + completion:nil];
|
| }
|
|
|
| - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section {
|
| - return section == 0 || section == 1;
|
| + NSInteger identifier = [self.collectionViewController.collectionViewModel
|
| + sectionIdentifierForSection:section];
|
| + ContentSuggestionsSectionInformation* sectionInformation =
|
| + [self.sectionInfoBySectionIdentifier objectForKey:@(identifier)];
|
| + return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom;
|
| +}
|
| +
|
| +#pragma mark - Private methods
|
| +
|
| +- (void)reloadData {
|
| + [self resetModels];
|
| + CollectionViewModel* model =
|
| + self.collectionViewController.collectionViewModel;
|
| +
|
| + NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions];
|
| +
|
| + for (ContentSuggestion* suggestion in suggestions) {
|
| + NSInteger sectionIdentifier = [self addSectionIfNeeded:suggestion.section];
|
| + ContentSuggestionsArticleItem* articleItem =
|
| + [[ContentSuggestionsArticleItem alloc]
|
| + initWithType:ContentSuggestionsItemTypeArticle
|
| + title:suggestion.title
|
| + subtitle:suggestion.text
|
| + image:suggestion.image
|
| + url:suggestion.url];
|
| +
|
| + ContentSuggestionsImageUpdater* imageUpdater = suggestion.imageUpdater;
|
| + [self.imageUpdaterSet addObject:suggestion.imageUpdater];
|
| + imageUpdater.delegate = self;
|
| + imageUpdater.item = articleItem;
|
| + imageUpdater.sectionIdentifier = sectionIdentifier;
|
| +
|
| + [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
|
| + }
|
| +
|
| + if ([self.collectionViewController isViewLoaded]) {
|
| + [self.collectionViewController.collectionView reloadData];
|
| + }
|
| +}
|
| +
|
| +- (NSInteger)addSectionIfNeeded:
|
| + (ContentSuggestionsSectionInformation*)sectionInformation {
|
| + NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation);
|
| +
|
| + CollectionViewModel* model =
|
| + self.collectionViewController.collectionViewModel;
|
| + if (![model hasSectionForSectionIdentifier:sectionIdentifier]) {
|
| + [model addSectionWithIdentifier:sectionIdentifier];
|
| + self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] =
|
| + sectionInformation;
|
| + [self.sectionInfoBySectionIdentifier setObject:sectionInformation
|
| + forKey:@(sectionIdentifier)];
|
| + }
|
| + return sectionIdentifier;
|
| +}
|
| +
|
| +- (void)resetModels {
|
| + [self.collectionViewController loadModel];
|
| + self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init];
|
| + self.imageUpdaterSet = [[NSMutableSet alloc] init];
|
| +}
|
| +
|
| +#pragma mark - ContentSuggestionsImageUpdaterDelegate
|
| +
|
| +- (void)contentSuggestionsImageUpdater:
|
| + (ContentSuggestionsImageUpdater*)imageUpdater
|
| + receivedImage:(UIImage*)image {
|
| + if (!imageUpdater.item)
|
| + return;
|
| +
|
| + imageUpdater.item.image = image;
|
| + [self.collectionViewController
|
| + reconfigureCellsForItems:@[ imageUpdater.item ]
|
| + inSectionWithIdentifier:imageUpdater.sectionIdentifier];
|
| + [self.imageUpdaterSet removeObject:imageUpdater];
|
| }
|
|
|
| @end
|
|
|