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

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

Issue 2865183003: Use the same design for all suggestions (Closed)
Patch Set: Reviewable 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_text_item.h " 12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h "
13 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 13 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
14 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 14 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
15 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_art icle_item.h"
16 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_foo ter_item.h" 15 #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_ite m.h"
17 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited.h" 17 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited.h"
18 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited_item.h" 18 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_mos t_visited_item.h"
19 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_rea ding_list_item.h"
20 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h" 19 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h"
21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h"
22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h" 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h"
24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h"
25 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h" 24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h"
26 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h" 25 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h"
27 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h" 26 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h"
28 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h" 27 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h"
29 #include "ui/base/l10n/l10n_util.h" 28 #include "ui/base/l10n/l10n_util.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
99 return SectionIdentifierMostVisited; 98 return SectionIdentifierMostVisited;
100 99
101 case ContentSuggestionsSectionUnknown: 100 case ContentSuggestionsSectionUnknown:
102 return SectionIdentifierDefault; 101 return SectionIdentifierDefault;
103 } 102 }
104 } 103 }
105 104
106 } // namespace 105 } // namespace
107 106
108 @interface ContentSuggestionsCollectionUpdater ()< 107 @interface ContentSuggestionsCollectionUpdater ()<
109 ContentSuggestionsArticleItemDelegate, 108 ContentSuggestionsItemDelegate,
110 ContentSuggestionsDataSink> 109 ContentSuggestionsDataSink>
111 110
112 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; 111 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource;
113 @property(nonatomic, strong) 112 @property(nonatomic, strong)
114 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* 113 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>*
115 sectionInfoBySectionIdentifier; 114 sectionInfoBySectionIdentifier;
116 115
117 @end 116 @end
118 117
119 @implementation ContentSuggestionsCollectionUpdater 118 @implementation ContentSuggestionsCollectionUpdater
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
268 sectionIdentifier]] == 0) { 267 sectionIdentifier]] == 0) {
269 CSCollectionViewItem* item = 268 CSCollectionViewItem* item =
270 [self emptyItemForSectionInfo:sectionInfo]; 269 [self emptyItemForSectionInfo:sectionInfo];
271 NSIndexPath* addedIndexPath = 270 NSIndexPath* addedIndexPath =
272 [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 271 [self addItem:item toSectionWithIdentifier:sectionIdentifier];
273 [indexPaths addObject:addedIndexPath]; 272 [indexPaths addObject:addedIndexPath];
274 } 273 }
275 break; 274 break;
276 } 275 }
277 case ContentSuggestionTypeArticle: { 276 case ContentSuggestionTypeArticle: {
278 ContentSuggestionsArticleItem* articleItem = 277 ContentSuggestionsItem* articleItem =
279 [self articleItemForSuggestion:suggestion]; 278 [self suggestionItemForSuggestion:suggestion];
279
280 articleItem.hasImage = YES;
281
282 __weak ContentSuggestionsItem* weakItem = articleItem;
283 [self fetchFaviconForItem:articleItem
284 withURL:articleItem.URL
285 callback:^void(FaviconAttributes* attributes) {
286 weakItem.attributes = attributes;
287 }];
288
289 __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
290 [self.dataSource
291 fetchFaviconImageForSuggestion:articleItem.suggestionIdentifier
292 completion:^void(UIImage* favicon) {
293 if (!weakItem || !weakSelf)
jif 2017/05/10 09:06:19 weakItem could be released after line 293; it shou
gambard 2017/05/10 09:23:27 Done.
294 return;
295
296 weakItem.attributes = [FaviconAttributes
297 attributesWithImage:favicon];
298 [weakSelf.collectionViewController
299 reconfigureCellsForItems:@[ weakItem ]];
300 }];
280 301
281 NSIndexPath* addedIndexPath = [self addItem:articleItem 302 NSIndexPath* addedIndexPath = [self addItem:articleItem
282 toSectionWithIdentifier:sectionIdentifier]; 303 toSectionWithIdentifier:sectionIdentifier];
283 [indexPaths addObject:addedIndexPath]; 304 [indexPaths addObject:addedIndexPath];
284 break; 305 break;
285 } 306 }
286 case ContentSuggestionTypeReadingList: { 307 case ContentSuggestionTypeReadingList: {
287 ContentSuggestionsReadingListItem* readingListItem = 308 ContentSuggestionsItem* readingListItem =
288 [self readingListItemForSuggestion:suggestion]; 309 [self suggestionItemForSuggestion:suggestion];
310
311 __weak ContentSuggestionsItem* weakItem = readingListItem;
312 [self fetchFaviconForItem:readingListItem
313 withURL:readingListItem.URL
314 callback:^void(FaviconAttributes* attributes) {
315 weakItem.attributes = attributes;
316 }];
289 317
290 NSIndexPath* addedIndexPath = [self addItem:readingListItem 318 NSIndexPath* addedIndexPath = [self addItem:readingListItem
291 toSectionWithIdentifier:sectionIdentifier]; 319 toSectionWithIdentifier:sectionIdentifier];
292 [indexPaths addObject:addedIndexPath]; 320 [indexPaths addObject:addedIndexPath];
293 break; 321 break;
294 } 322 }
295 case ContentSuggestionTypeMostVisited: { 323 case ContentSuggestionTypeMostVisited: {
296 NSInteger section = 324 NSInteger section =
297 [model sectionForSectionIdentifier:SectionIdentifierMostVisited]; 325 [model sectionForSectionIdentifier:SectionIdentifierMostVisited];
298 NSIndexPath* indexPath = 326 NSIndexPath* indexPath =
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
358 CSCollectionViewModel* model = 386 CSCollectionViewModel* model =
359 self.collectionViewController.collectionViewModel; 387 self.collectionViewController.collectionViewModel;
360 NSInteger sectionIdentifier = [model sectionIdentifierForSection:section]; 388 NSInteger sectionIdentifier = [model sectionIdentifierForSection:section];
361 ContentSuggestionsSectionInformation* sectionInfo = 389 ContentSuggestionsSectionInformation* sectionInfo =
362 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)]; 390 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)];
363 391
364 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo]; 392 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo];
365 return [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 393 return [self addItem:item toSectionWithIdentifier:sectionIdentifier];
366 } 394 }
367 395
368 #pragma mark - ContentSuggestionsArticleItemDelegate 396 #pragma mark - ContentSuggestionsItemDelegate
369 397
370 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { 398 - (void)loadImageForSuggestionItem:(ContentSuggestionsItem*)suggestionItem {
371 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; 399 __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
372 __weak ContentSuggestionsArticleItem* weakArticle = articleItem; 400 __weak ContentSuggestionsItem* weakArticle = suggestionItem;
373 401
374 void (^imageFetchedCallback)(UIImage*) = ^(UIImage* image) { 402 void (^imageFetchedCallback)(UIImage*) = ^(UIImage* image) {
375 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; 403 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf;
376 ContentSuggestionsArticleItem* strongArticle = weakArticle; 404 ContentSuggestionsItem* strongArticle = weakArticle;
377 if (!strongSelf || !strongArticle) { 405 if (!strongSelf || !strongArticle) {
378 return; 406 return;
379 } 407 }
380 408
381 strongArticle.image = image; 409 strongArticle.image = image;
382 [strongSelf.collectionViewController 410 [strongSelf.collectionViewController
383 reconfigureCellsForItems:@[ strongArticle ]]; 411 reconfigureCellsForItems:@[ strongArticle ]];
384 }; 412 };
385 413
386 [self.dataSource.imageFetcher 414 [self.dataSource.imageFetcher
387 fetchImageForSuggestion:articleItem.suggestionIdentifier 415 fetchImageForSuggestion:suggestionItem.suggestionIdentifier
388 callback:imageFetchedCallback]; 416 callback:imageFetchedCallback];
389 } 417 }
390 418
391 #pragma mark - Private methods 419 #pragma mark - Private methods
392 420
393 // Adds a footer to the section identified by |sectionInfo| if there is none 421 // Adds a footer to the section identified by |sectionInfo| if there is none
394 // present and the section info contains a title for it. 422 // present and the section info contains a title for it.
395 - (void)addFooterIfNeeded:(ContentSuggestionsSectionInformation*)sectionInfo { 423 - (void)addFooterIfNeeded:(ContentSuggestionsSectionInformation*)sectionInfo {
396 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); 424 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo);
397 425
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
471 - (CSCollectionViewItem*)emptyItemForSectionInfo: 499 - (CSCollectionViewItem*)emptyItemForSectionInfo:
472 (ContentSuggestionsSectionInformation*)sectionInfo { 500 (ContentSuggestionsSectionInformation*)sectionInfo {
473 ContentSuggestionsTextItem* item = 501 ContentSuggestionsTextItem* item =
474 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty]; 502 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty];
475 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS); 503 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS);
476 item.detailText = sectionInfo.emptyText; 504 item.detailText = sectionInfo.emptyText;
477 505
478 return item; 506 return item;
479 } 507 }
480 508
481 // Returns an article built with the |suggestion|. 509 // Returns an suggestion item built with the |suggestion|.
jif 2017/05/10 09:06:19 Returns a suggestion
gambard 2017/05/10 09:23:27 Done.
482 - (ContentSuggestionsArticleItem*)articleItemForSuggestion: 510 - (ContentSuggestionsItem*)suggestionItemForSuggestion:
483 (ContentSuggestion*)suggestion { 511 (ContentSuggestion*)suggestion {
484 ContentSuggestionsArticleItem* articleItem = 512 ContentSuggestionsItem* suggestionItem = [[ContentSuggestionsItem alloc]
485 [[ContentSuggestionsArticleItem alloc] 513 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
486 initWithType:ItemTypeForContentSuggestionType(suggestion.type) 514 title:suggestion.title
487 title:suggestion.title 515 subtitle:suggestion.text
488 subtitle:suggestion.text 516 delegate:self
489 delegate:self 517 url:suggestion.url];
490 url:suggestion.url];
491 518
492 articleItem.publisher = suggestion.publisher; 519 suggestionItem.publisher = suggestion.publisher;
493 articleItem.publishDate = suggestion.publishDate; 520 suggestionItem.publishDate = suggestion.publishDate;
521 suggestionItem.availableOffline = suggestion.availableOffline;
494 522
495 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; 523 suggestionItem.suggestionIdentifier = suggestion.suggestionIdentifier;
496 524
497 __weak ContentSuggestionsArticleItem* weakItem = articleItem; 525 return suggestionItem;
498 [self fetchFaviconForItem:articleItem
499 withURL:articleItem.articleURL
500 callback:^void(FaviconAttributes* attributes) {
501 weakItem.attributes = attributes;
502 }];
503
504 __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
505 [self.dataSource
506 fetchFaviconImageForSuggestion:articleItem.suggestionIdentifier
507 completion:^void(UIImage* favicon) {
508 if (!weakItem || !weakSelf)
509 return;
510
511 weakItem.attributes =
512 [FaviconAttributes attributesWithImage:favicon];
513 [weakSelf.collectionViewController
514 reconfigureCellsForItems:@[ weakItem ]];
515 }];
516
517 return articleItem;
518 }
519
520 // Returns a reading list item built with the |suggestion|.
521 - (ContentSuggestionsReadingListItem*)readingListItemForSuggestion:
522 (ContentSuggestion*)suggestion {
523 ContentSuggestionsReadingListItem* readingListItem =
524 [[ContentSuggestionsReadingListItem alloc]
525 initWithType:ItemTypeReadingList
526 url:suggestion.url
527 distillationState:suggestion.readingListExtra.status];
528
529 readingListItem.title = suggestion.title;
530 readingListItem.subtitle = suggestion.publisher;
531
532 readingListItem.suggestionIdentifier = suggestion.suggestionIdentifier;
533
534 __weak ContentSuggestionsReadingListItem* weakItem = readingListItem;
535 [self fetchFaviconForItem:readingListItem
536 withURL:readingListItem.url
537 callback:^void(FaviconAttributes* attributes) {
538 weakItem.attributes = attributes;
539 }];
540
541 return readingListItem;
542 } 526 }
543 527
544 // Fetches the favicon associated with the |URL|, call the |callback| with the 528 // Fetches the favicon associated with the |URL|, call the |callback| with the
545 // attributes then reconfigure the |item|. 529 // attributes then reconfigure the |item|.
546 - (void)fetchFaviconForItem:(CSCollectionViewItem*)item 530 - (void)fetchFaviconForItem:(CSCollectionViewItem*)item
547 withURL:(const GURL&)URL 531 withURL:(const GURL&)URL
548 callback:(void (^)(FaviconAttributes*))callback { 532 callback:(void (^)(FaviconAttributes*))callback {
549 if (!callback) 533 if (!callback)
550 return; 534 return;
551 535
(...skipping 23 matching lines...) Expand all
575 CSCollectionViewModel* model = 559 CSCollectionViewModel* model =
576 self.collectionViewController.collectionViewModel; 560 self.collectionViewController.collectionViewModel;
577 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; 561 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
578 NSInteger itemNumber = [model numberOfItemsInSection:section]; 562 NSInteger itemNumber = [model numberOfItemsInSection:section];
579 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; 563 [model addItem:item toSectionWithIdentifier:sectionIdentifier];
580 564
581 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; 565 return [NSIndexPath indexPathForItem:itemNumber inSection:section];
582 } 566 }
583 567
584 @end 568 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698