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

Unified Diff: ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm

Issue 2741343005: ContentSuggestions collection adds items with animation (Closed)
Patch Set: Rebase on correct head Created 3 years, 9 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: 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];

Powered by Google App Engine
This is Rietveld 408576698