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

Side by Side Diff: ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm

Issue 2888403002: Limit the number of Most Visited lines to two (Closed)
Patch Set: Address comments Created 3 years, 7 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collectio n_updater.h" 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collectio n_updater.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/mac/foundation_util.h" 8 #include "base/mac/foundation_util.h"
9 #include "base/strings/sys_string_conversions.h" 9 #include "base/strings/sys_string_conversions.h"
10 #include "base/time/time.h" 10 #include "base/time/time.h"
11 #include "components/strings/grit/components_strings.h" 11 #include "components/strings/grit/components_strings.h"
12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collec tion_view_controller.h" 12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collec tion_view_controller.h"
13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h " 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h "
14 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 14 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
16 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_foo ter_item.h" 16 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_foo ter_item.h"
17 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h" 17 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h"
18 #import "ios/chrome/browser/ui/content_suggestions/cells/suggested_content.h" 18 #import "ios/chrome/browser/ui/content_suggestions/cells/suggested_content.h"
19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collectio n_utils.h"
19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h" 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h"
21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h"
22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h" 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h"
23 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h" 24 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h"
24 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h" 25 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h"
25 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h" 26 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h"
26 #include "ui/base/l10n/l10n_util.h" 27 #include "ui/base/l10n/l10n_util.h"
27 28
28 #if !defined(__has_feature) || !__has_feature(objc_arc) 29 #if !defined(__has_feature) || !__has_feature(objc_arc)
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
94 case ContentSuggestionsSectionReadingList: 95 case ContentSuggestionsSectionReadingList:
95 return SectionIdentifierReadingList; 96 return SectionIdentifierReadingList;
96 case ContentSuggestionsSectionMostVisited: 97 case ContentSuggestionsSectionMostVisited:
97 return SectionIdentifierMostVisited; 98 return SectionIdentifierMostVisited;
98 99
99 case ContentSuggestionsSectionUnknown: 100 case ContentSuggestionsSectionUnknown:
100 return SectionIdentifierDefault; 101 return SectionIdentifierDefault;
101 } 102 }
102 } 103 }
103 104
105 const CGFloat kNumberOfMostVisitedLines = 2;
106
104 } // namespace 107 } // namespace
105 108
106 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink, 109 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink,
107 SuggestedContentDelegate> 110 SuggestedContentDelegate>
108 111
109 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; 112 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource;
110 @property(nonatomic, strong) 113 @property(nonatomic, strong)
111 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* 114 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>*
112 sectionInfoBySectionIdentifier; 115 sectionInfoBySectionIdentifier;
116 // Width of the collection. Upon size change, it reflects the new size.
117 @property(nonatomic, assign) CGFloat collectionWidth;
113 118
114 @end 119 @end
115 120
116 @implementation ContentSuggestionsCollectionUpdater 121 @implementation ContentSuggestionsCollectionUpdater
117 122
118 @synthesize collectionViewController = _collectionViewController; 123 @synthesize collectionViewController = _collectionViewController;
119 @synthesize dataSource = _dataSource; 124 @synthesize dataSource = _dataSource;
120 @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier; 125 @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier;
126 @synthesize collectionWidth = _collectionWidth;
121 127
122 - (instancetype)initWithDataSource: 128 - (instancetype)initWithDataSource:
123 (id<ContentSuggestionsDataSource>)dataSource { 129 (id<ContentSuggestionsDataSource>)dataSource {
124 self = [super init]; 130 self = [super init];
125 if (self) { 131 if (self) {
126 _dataSource = dataSource; 132 _dataSource = dataSource;
127 _dataSource.dataSink = self; 133 _dataSource.dataSink = self;
128 } 134 }
129 return self; 135 return self;
130 } 136 }
131 137
132 #pragma mark - Properties 138 #pragma mark - Properties
133 139
134 - (void)setCollectionViewController: 140 - (void)setCollectionViewController:
135 (ContentSuggestionsViewController*)collectionViewController { 141 (ContentSuggestionsViewController*)collectionViewController {
136 _collectionViewController = collectionViewController; 142 _collectionViewController = collectionViewController;
143 self.collectionWidth =
144 collectionViewController.collectionView.bounds.size.width;
137 145
138 [self reloadAllData]; 146 [self reloadAllData];
139 } 147 }
140 148
141 #pragma mark - ContentSuggestionsDataSink 149 #pragma mark - ContentSuggestionsDataSink
142 150
143 - (void)dataAvailableForSection: 151 - (void)dataAvailableForSection:
144 (ContentSuggestionsSectionInformation*)sectionInfo { 152 (ContentSuggestionsSectionInformation*)sectionInfo {
145 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); 153 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo);
146 154
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 NSArray<CSCollectionViewItem*>* existingItems = 271 NSArray<CSCollectionViewItem*>* existingItems =
264 [model itemsInSectionWithIdentifier:sectionIdentifier]; 272 [model itemsInSectionWithIdentifier:sectionIdentifier];
265 if (existingItems.count > 0 && existingItems[0].type == ItemTypeEmpty) { 273 if (existingItems.count > 0 && existingItems[0].type == ItemTypeEmpty) {
266 [model removeItemWithType:ItemTypeEmpty 274 [model removeItemWithType:ItemTypeEmpty
267 fromSectionWithIdentifier:sectionIdentifier]; 275 fromSectionWithIdentifier:sectionIdentifier];
268 emptyItemRemoved = YES; 276 emptyItemRemoved = YES;
269 } 277 }
270 278
271 [suggestions enumerateObjectsUsingBlock:^(CSCollectionViewItem* item, 279 [suggestions enumerateObjectsUsingBlock:^(CSCollectionViewItem* item,
272 NSUInteger index, BOOL* stop) { 280 NSUInteger index, BOOL* stop) {
281 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
282 if ([self isMostVisitedSection:section] &&
283 [model numberOfItemsInSection:section] >=
284 [self mostVisitedPlaceCount]) {
285 return;
286 }
273 ItemType type = ItemTypeForInfo(sectionInfo); 287 ItemType type = ItemTypeForInfo(sectionInfo);
274 item.type = type; 288 item.type = type;
275 NSIndexPath* addedIndexPath = 289 NSIndexPath* addedIndexPath =
276 [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 290 [self addItem:item toSectionWithIdentifier:sectionIdentifier];
277 [self fetchFaviconForItem:item]; 291 [self fetchFaviconForItem:item];
278 item.delegate = self; 292 item.delegate = self;
279 293
280 if (!emptyItemRemoved || index > 0) { 294 if (!emptyItemRemoved || index > 0) {
281 [indexPaths addObject:addedIndexPath]; 295 [indexPaths addObject:addedIndexPath];
282 } else { 296 } else {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo]; 355 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo];
342 return [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 356 return [self addItem:item toSectionWithIdentifier:sectionIdentifier];
343 } 357 }
344 358
345 - (BOOL)isMostVisitedSection:(NSInteger)section { 359 - (BOOL)isMostVisitedSection:(NSInteger)section {
346 return 360 return
347 [self.collectionViewController.collectionViewModel 361 [self.collectionViewController.collectionViewModel
348 sectionIdentifierForSection:section] == SectionIdentifierMostVisited; 362 sectionIdentifierForSection:section] == SectionIdentifierMostVisited;
349 } 363 }
350 364
365 - (void)updateMostVisitedForSize:(CGSize)size {
366 self.collectionWidth = size.width;
367
368 CSCollectionViewModel* model =
369 self.collectionViewController.collectionViewModel;
370 if (![model hasSectionForSectionIdentifier:SectionIdentifierMostVisited])
371 return;
372
373 NSInteger mostVisitedSection =
374 [model sectionForSectionIdentifier:SectionIdentifierMostVisited];
375 ContentSuggestionsSectionInformation* mostVisitedSectionInfo =
376 self.sectionInfoBySectionIdentifier[@(SectionIdentifierMostVisited)];
377 NSArray<CSCollectionViewItem*>* mostVisited =
378 [self.dataSource itemsForSectionInfo:mostVisitedSectionInfo];
379 NSInteger newCount = MIN([self mostVisitedPlaceCount],
380 static_cast<NSInteger>(mostVisited.count));
381 NSInteger currentCount = [model numberOfItemsInSection:mostVisitedSection];
382
383 if (currentCount == newCount)
384 return;
385
386 // If the animations are enabled, the items are added then the rotation
387 // animation is triggered, creating a weird sequenced animation.
388 [UIView setAnimationsEnabled:NO];
389 if (currentCount > newCount) {
390 for (NSInteger i = newCount; i < currentCount; i++) {
391 NSIndexPath* itemToRemove =
392 [NSIndexPath indexPathForItem:newCount inSection:mostVisitedSection];
393 [self.collectionViewController dismissEntryAtIndexPath:itemToRemove];
394 }
395 } else {
396 NSMutableArray* itemsToBeAdded = [NSMutableArray array];
397 for (NSInteger i = currentCount; i < newCount; i++) {
398 [itemsToBeAdded addObject:mostVisited[i]];
399 }
400 [self.collectionViewController addSuggestions:itemsToBeAdded
401 toSectionInfo:mostVisitedSectionInfo];
402 }
403 [UIView setAnimationsEnabled:YES];
404 }
405
351 #pragma mark - SuggestedContentDelegate 406 #pragma mark - SuggestedContentDelegate
352 407
353 - (void)loadImageForSuggestedItem:(CSCollectionViewItem*)suggestedItem { 408 - (void)loadImageForSuggestedItem:(CSCollectionViewItem*)suggestedItem {
354 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; 409 __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
355 __weak CSCollectionViewItem* weakItem = suggestedItem; 410 __weak CSCollectionViewItem* weakItem = suggestedItem;
356 411
357 void (^imageFetchedCallback)(UIImage*) = ^(UIImage* image) { 412 void (^imageFetchedCallback)(UIImage*) = ^(UIImage* image) {
358 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; 413 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf;
359 CSCollectionViewItem* strongItem = weakItem; 414 CSCollectionViewItem* strongItem = weakItem;
360 if (!strongSelf || !strongItem) { 415 if (!strongSelf || !strongItem) {
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
526 toSectionWithIdentifier:(NSInteger)sectionIdentifier { 581 toSectionWithIdentifier:(NSInteger)sectionIdentifier {
527 CSCollectionViewModel* model = 582 CSCollectionViewModel* model =
528 self.collectionViewController.collectionViewModel; 583 self.collectionViewController.collectionViewModel;
529 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; 584 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
530 NSInteger itemNumber = [model numberOfItemsInSection:section]; 585 NSInteger itemNumber = [model numberOfItemsInSection:section];
531 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; 586 [model addItem:item toSectionWithIdentifier:sectionIdentifier];
532 587
533 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; 588 return [NSIndexPath indexPathForItem:itemNumber inSection:section];
534 } 589 }
535 590
591 // Returns the maximum number of Most Visited tiles to be displayed in the
592 // collection.
593 - (NSInteger)mostVisitedPlaceCount {
594 return content_suggestions::numberOfTilesForWidth(self.collectionWidth) *
595 kNumberOfMostVisitedLines;
596 }
597
536 @end 598 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698