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

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: 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;
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 :)
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;
113 116 //
Eugene But (OOO till 7-30) 2017/05/18 15:02:00 Please finish the comment :)
gambard 2017/05/18 15:16:07 Done.
117 @property(nonatomic, assign) CGFloat collectionWidth;
114 @end 118 @end
115 119
116 @implementation ContentSuggestionsCollectionUpdater 120 @implementation ContentSuggestionsCollectionUpdater
117 121
118 @synthesize collectionViewController = _collectionViewController; 122 @synthesize collectionViewController = _collectionViewController;
119 @synthesize dataSource = _dataSource; 123 @synthesize dataSource = _dataSource;
120 @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier; 124 @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier;
125 @synthesize collectionWidth = _collectionWidth;
121 126
122 - (instancetype)initWithDataSource: 127 - (instancetype)initWithDataSource:
123 (id<ContentSuggestionsDataSource>)dataSource { 128 (id<ContentSuggestionsDataSource>)dataSource {
124 self = [super init]; 129 self = [super init];
125 if (self) { 130 if (self) {
126 _dataSource = dataSource; 131 _dataSource = dataSource;
127 _dataSource.dataSink = self; 132 _dataSource.dataSink = self;
128 } 133 }
129 return self; 134 return self;
130 } 135 }
131 136
132 #pragma mark - Properties 137 #pragma mark - Properties
133 138
134 - (void)setCollectionViewController: 139 - (void)setCollectionViewController:
135 (ContentSuggestionsViewController*)collectionViewController { 140 (ContentSuggestionsViewController*)collectionViewController {
136 _collectionViewController = collectionViewController; 141 _collectionViewController = collectionViewController;
142 self.collectionWidth =
143 collectionViewController.collectionView.bounds.size.width;
137 144
138 [self reloadAllData]; 145 [self reloadAllData];
139 } 146 }
140 147
141 #pragma mark - ContentSuggestionsDataSink 148 #pragma mark - ContentSuggestionsDataSink
142 149
143 - (void)dataAvailableForSection: 150 - (void)dataAvailableForSection:
144 (ContentSuggestionsSectionInformation*)sectionInfo { 151 (ContentSuggestionsSectionInformation*)sectionInfo {
145 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); 152 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo);
146 153
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
263 NSArray<CSCollectionViewItem*>* existingItems = 270 NSArray<CSCollectionViewItem*>* existingItems =
264 [model itemsInSectionWithIdentifier:sectionIdentifier]; 271 [model itemsInSectionWithIdentifier:sectionIdentifier];
265 if (existingItems.count > 0 && existingItems[0].type == ItemTypeEmpty) { 272 if (existingItems.count > 0 && existingItems[0].type == ItemTypeEmpty) {
266 [model removeItemWithType:ItemTypeEmpty 273 [model removeItemWithType:ItemTypeEmpty
267 fromSectionWithIdentifier:sectionIdentifier]; 274 fromSectionWithIdentifier:sectionIdentifier];
268 emptyItemRemoved = YES; 275 emptyItemRemoved = YES;
269 } 276 }
270 277
271 [suggestions enumerateObjectsUsingBlock:^(CSCollectionViewItem* item, 278 [suggestions enumerateObjectsUsingBlock:^(CSCollectionViewItem* item,
272 NSUInteger index, BOOL* stop) { 279 NSUInteger index, BOOL* stop) {
280 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
281 if ([self isMostVisitedSection:section] &&
282 [model numberOfItemsInSection:section] >=
283 [self maxNumberOfMostVisited]) {
284 return;
285 }
273 ItemType type = ItemTypeForInfo(sectionInfo); 286 ItemType type = ItemTypeForInfo(sectionInfo);
274 item.type = type; 287 item.type = type;
275 NSIndexPath* addedIndexPath = 288 NSIndexPath* addedIndexPath =
276 [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 289 [self addItem:item toSectionWithIdentifier:sectionIdentifier];
277 [self fetchFaviconForItem:item]; 290 [self fetchFaviconForItem:item];
278 item.delegate = self; 291 item.delegate = self;
279 292
280 if (!emptyItemRemoved || index > 0) { 293 if (!emptyItemRemoved || index > 0) {
281 [indexPaths addObject:addedIndexPath]; 294 [indexPaths addObject:addedIndexPath];
282 } else { 295 } else {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
341 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo]; 354 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo];
342 return [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 355 return [self addItem:item toSectionWithIdentifier:sectionIdentifier];
343 } 356 }
344 357
345 - (BOOL)isMostVisitedSection:(NSInteger)section { 358 - (BOOL)isMostVisitedSection:(NSInteger)section {
346 return 359 return
347 [self.collectionViewController.collectionViewModel 360 [self.collectionViewController.collectionViewModel
348 sectionIdentifierForSection:section] == SectionIdentifierMostVisited; 361 sectionIdentifierForSection:section] == SectionIdentifierMostVisited;
349 } 362 }
350 363
364 - (void)updateMostVisitedForSize:(CGSize)size {
365 self.collectionWidth = size.width;
366
367 CSCollectionViewModel* model =
368 self.collectionViewController.collectionViewModel;
369 if (![model hasSectionForSectionIdentifier:SectionIdentifierMostVisited])
370 return;
371
372 NSInteger mostVisitedSection =
373 [model sectionForSectionIdentifier:SectionIdentifierMostVisited];
374 ContentSuggestionsSectionInformation* mostVisitedSectionInfo =
375 self.sectionInfoBySectionIdentifier[@(SectionIdentifierMostVisited)];
376 NSArray<CSCollectionViewItem*>* mostVisited =
377 [self.dataSource itemsForSectionInfo:mostVisitedSectionInfo];
378 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.
379 static_cast<NSInteger>(mostVisited.count));
380 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.
381
382 if (currentNumber == futureNumber)
383 return;
384
385 // If the animations are enabled, the items are added then the rotation
386 // animation is triggered, creating a weird sequenced animation.
387 [UIView setAnimationsEnabled:NO];
388 if (currentNumber > futureNumber) {
389 for (NSInteger i = futureNumber; i < currentNumber; i++) {
390 [self.collectionViewController
391 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.
392 indexPathForItem:futureNumber
393 inSection:mostVisitedSection]];
394 }
395 } else {
396 NSMutableArray* itemsToBeAdded = [NSMutableArray array];
397 for (NSInteger i = currentNumber; i < futureNumber; 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
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
592 // collection.
593 - (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
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