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 4e7587f05394772378dc5a5f5413433399c18082..66d93b85b03eb2114c6565abc9b0cc8775d6b083 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 |
| @@ -16,6 +16,7 @@ |
| #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_footer_item.h" |
| #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_text_item.h" |
| #import "ios/chrome/browser/ui/content_suggestions/cells/suggested_content.h" |
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_utils.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_image_fetcher.h" |
| @@ -101,6 +102,8 @@ SectionIdentifier SectionIdentifierForInfo( |
| } |
| } |
| +const CGFloat kNumberOfMostVisitedLines = 2; |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:00
Would it be useful to explain how we came up with
gambard
2017/05/18 15:16:07
I have no idea why we picked 2 :)
|
| + |
| } // namespace |
| @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink, |
| @@ -110,7 +113,8 @@ SectionIdentifier SectionIdentifierForInfo( |
| @property(nonatomic, strong) |
| NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* |
| sectionInfoBySectionIdentifier; |
| - |
| +// |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:00
Please finish the comment :)
gambard
2017/05/18 15:16:07
Done.
|
| +@property(nonatomic, assign) CGFloat collectionWidth; |
| @end |
| @implementation ContentSuggestionsCollectionUpdater |
| @@ -118,6 +122,7 @@ SectionIdentifier SectionIdentifierForInfo( |
| @synthesize collectionViewController = _collectionViewController; |
| @synthesize dataSource = _dataSource; |
| @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier; |
| +@synthesize collectionWidth = _collectionWidth; |
| - (instancetype)initWithDataSource: |
| (id<ContentSuggestionsDataSource>)dataSource { |
| @@ -134,6 +139,8 @@ SectionIdentifier SectionIdentifierForInfo( |
| - (void)setCollectionViewController: |
| (ContentSuggestionsViewController*)collectionViewController { |
| _collectionViewController = collectionViewController; |
| + self.collectionWidth = |
| + collectionViewController.collectionView.bounds.size.width; |
| [self reloadAllData]; |
| } |
| @@ -270,6 +277,12 @@ addSuggestionsToModel:(NSArray<CSCollectionViewItem*>*)suggestions |
| [suggestions enumerateObjectsUsingBlock:^(CSCollectionViewItem* item, |
| NSUInteger index, BOOL* stop) { |
| + NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| + if ([self isMostVisitedSection:section] && |
| + [model numberOfItemsInSection:section] >= |
| + [self maxNumberOfMostVisited]) { |
| + return; |
| + } |
| ItemType type = ItemTypeForInfo(sectionInfo); |
| item.type = type; |
| NSIndexPath* addedIndexPath = |
| @@ -348,6 +361,48 @@ addSuggestionsToModel:(NSArray<CSCollectionViewItem*>*)suggestions |
| sectionIdentifierForSection:section] == SectionIdentifierMostVisited; |
| } |
| +- (void)updateMostVisitedForSize:(CGSize)size { |
| + self.collectionWidth = size.width; |
| + |
| + CSCollectionViewModel* model = |
| + self.collectionViewController.collectionViewModel; |
| + if (![model hasSectionForSectionIdentifier:SectionIdentifierMostVisited]) |
| + return; |
| + |
| + NSInteger mostVisitedSection = |
| + [model sectionForSectionIdentifier:SectionIdentifierMostVisited]; |
| + ContentSuggestionsSectionInformation* mostVisitedSectionInfo = |
| + self.sectionInfoBySectionIdentifier[@(SectionIdentifierMostVisited)]; |
| + NSArray<CSCollectionViewItem*>* mostVisited = |
| + [self.dataSource itemsForSectionInfo:mostVisitedSectionInfo]; |
| + NSInteger futureNumber = MIN([self maxNumberOfMostVisited], |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:01
nit: s/futureNumber/newCount
gambard
2017/05/18 15:16:07
Done.
|
| + static_cast<NSInteger>(mostVisited.count)); |
| + NSInteger currentNumber = [model numberOfItemsInSection:mostVisitedSection]; |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:00
nit: s/currentNumber/currentCount
gambard
2017/05/18 15:16:07
Done.
|
| + |
| + if (currentNumber == futureNumber) |
| + return; |
| + |
| + // If the animations are enabled, the items are added then the rotation |
| + // animation is triggered, creating a weird sequenced animation. |
| + [UIView setAnimationsEnabled:NO]; |
| + if (currentNumber > futureNumber) { |
| + for (NSInteger i = futureNumber; i < currentNumber; i++) { |
| + [self.collectionViewController |
| + dismissEntryAtIndexPath:[NSIndexPath |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:00
Do you want to use a local variable for NSIndexPat
gambard
2017/05/18 15:16:07
Done.
|
| + indexPathForItem:futureNumber |
| + inSection:mostVisitedSection]]; |
| + } |
| + } else { |
| + NSMutableArray* itemsToBeAdded = [NSMutableArray array]; |
| + for (NSInteger i = currentNumber; i < futureNumber; i++) { |
| + [itemsToBeAdded addObject:mostVisited[i]]; |
| + } |
| + [self.collectionViewController addSuggestions:itemsToBeAdded |
| + toSectionInfo:mostVisitedSectionInfo]; |
| + } |
| + [UIView setAnimationsEnabled:YES]; |
| +} |
| + |
| #pragma mark - SuggestedContentDelegate |
| - (void)loadImageForSuggestedItem:(CSCollectionViewItem*)suggestedItem { |
| @@ -533,4 +588,11 @@ addSuggestionsToModel:(NSArray<CSCollectionViewItem*>*)suggestions |
| return [NSIndexPath indexPathForItem:itemNumber inSection:section]; |
| } |
| +// Returns the maximum number of Most Visited tiles to be displayed in the |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:00
Is this also SuggestedContentDelegate method? Shou
gambard
2017/05/18 15:16:07
There is a |#pragma mark - Private methods| in bet
|
| +// collection. |
| +- (NSInteger)maxNumberOfMostVisited { |
|
Eugene But (OOO till 7-30)
2017/05/18 15:02:01
How about |mostVisitedTileCount|?
gambard
2017/05/18 15:16:07
It is the number of places where a Most Visited ti
|
| + return content_suggestions::numberOfTilesForWidth(self.collectionWidth) * |
| + kNumberOfMostVisitedLines; |
| +} |
| + |
| @end |