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

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

Issue 2782613004: Add FaviconAttributesProvider to ContentSuggestions (Closed)
Patch Set: Rebase Created 3 years, 8 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" 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_but ton_item.h" 16 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_but ton_item.h"
17 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_exp andable_item.h" 17 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_exp andable_item.h"
18 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_fav icon_item.h" 18 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_fav icon_item.h"
19 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_foo ter_item.h" 19 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_foo ter_item.h"
20 #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_rea ding_list_item.h"
21 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_sta ck_item.h" 21 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_sta ck_item.h"
22 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h" 22 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h"
23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h"
24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
25 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h" 25 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h"
26 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" 26 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h"
27 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h" 27 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h"
28 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h" 28 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion _identifier.h"
29 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h" 29 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion s_section_information.h"
30 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h"
30 #include "ui/base/l10n/l10n_util.h" 31 #include "ui/base/l10n/l10n_util.h"
31 #include "ui/gfx/image/image.h" 32 #include "ui/gfx/image/image.h"
32 #include "url/gurl.h" 33 #include "url/gurl.h"
33 34
34 #if !defined(__has_feature) || !__has_feature(objc_arc) 35 #if !defined(__has_feature) || !__has_feature(objc_arc)
35 #error "This file requires ARC support." 36 #error "This file requires ARC support."
36 #endif 37 #endif
37 38
38 namespace { 39 namespace {
39 40
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after
260 CSCollectionViewItem* item = 261 CSCollectionViewItem* item =
261 [self emptyItemForSectionInfo:sectionInfo]; 262 [self emptyItemForSectionInfo:sectionInfo];
262 NSIndexPath* addedIndexPath = 263 NSIndexPath* addedIndexPath =
263 [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 264 [self addItem:item toSectionWithIdentifier:sectionIdentifier];
264 [indexPaths addObject:addedIndexPath]; 265 [indexPaths addObject:addedIndexPath];
265 } 266 }
266 break; 267 break;
267 } 268 }
268 case ContentSuggestionTypeArticle: { 269 case ContentSuggestionTypeArticle: {
269 ContentSuggestionsArticleItem* articleItem = 270 ContentSuggestionsArticleItem* articleItem =
270 [[ContentSuggestionsArticleItem alloc] 271 [self articleItemForSuggestion:suggestion];
271 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
272 title:suggestion.title
273 subtitle:suggestion.text
274 delegate:self
275 url:suggestion.url];
276
277 articleItem.publisher = suggestion.publisher;
278 articleItem.publishDate = suggestion.publishDate;
279
280 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
281 272
282 NSIndexPath* addedIndexPath = [self addItem:articleItem 273 NSIndexPath* addedIndexPath = [self addItem:articleItem
283 toSectionWithIdentifier:sectionIdentifier]; 274 toSectionWithIdentifier:sectionIdentifier];
284 [indexPaths addObject:addedIndexPath]; 275 [indexPaths addObject:addedIndexPath];
285 break; 276 break;
286 } 277 }
287 case ContentSuggestionTypeReadingList: { 278 case ContentSuggestionTypeReadingList: {
288 ContentSuggestionsReadingListItem* readingListItem = 279 ContentSuggestionsReadingListItem* readingListItem =
289 [[ContentSuggestionsReadingListItem alloc] 280 [[ContentSuggestionsReadingListItem alloc]
290 initWithType:ItemTypeReadingList 281 initWithType:ItemTypeReadingList
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
457 - (CSCollectionViewItem*)emptyItemForSectionInfo: 448 - (CSCollectionViewItem*)emptyItemForSectionInfo:
458 (ContentSuggestionsSectionInformation*)sectionInfo { 449 (ContentSuggestionsSectionInformation*)sectionInfo {
459 ContentSuggestionsTextItem* item = 450 ContentSuggestionsTextItem* item =
460 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty]; 451 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty];
461 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS); 452 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS);
462 item.detailText = sectionInfo.emptyText; 453 item.detailText = sectionInfo.emptyText;
463 454
464 return item; 455 return item;
465 } 456 }
466 457
458 // Returns an article build with the |suggestion|.
459 - (ContentSuggestionsArticleItem*)articleItemForSuggestion:
460 (ContentSuggestion*)suggestion {
461 ContentSuggestionsArticleItem* articleItem =
462 [[ContentSuggestionsArticleItem alloc]
463 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
464 title:suggestion.title
465 subtitle:suggestion.text
466 delegate:self
467 url:suggestion.url];
468
469 articleItem.publisher = suggestion.publisher;
470 articleItem.publishDate = suggestion.publishDate;
471
472 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
473
474 __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
475 __weak ContentSuggestionsArticleItem* weakItem = articleItem;
476 void (^completionBlock)(FaviconAttributes* attributes) =
477 ^(FaviconAttributes* attributes) {
478 ContentSuggestionsArticleItem* strongItem = weakItem;
479 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf;
480 if (!strongSelf || !strongItem) {
481 return;
482 }
483
484 strongItem.attributes = attributes;
485
486 [strongSelf reconfigure:strongItem];
487 };
488
489 [self.dataSource fetchFaviconAttributesForURL:articleItem.articleURL
490 completion:completionBlock];
491
492 return articleItem;
493 }
494
467 // Adds |item| to |sectionIdentifier| section of the model of the 495 // Adds |item| to |sectionIdentifier| section of the model of the
468 // CollectionView. Returns the IndexPath of the newly added item. 496 // CollectionView. Returns the IndexPath of the newly added item.
469 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item 497 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item
470 toSectionWithIdentifier:(NSInteger)sectionIdentifier { 498 toSectionWithIdentifier:(NSInteger)sectionIdentifier {
471 CSCollectionViewModel* model = 499 CSCollectionViewModel* model =
472 self.collectionViewController.collectionViewModel; 500 self.collectionViewController.collectionViewModel;
473 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; 501 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
474 NSInteger itemNumber = [model numberOfItemsInSection:section]; 502 NSInteger itemNumber = [model numberOfItemsInSection:section];
475 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; 503 [model addItem:item toSectionWithIdentifier:sectionIdentifier];
476 504
477 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; 505 return [NSIndexPath indexPathForItem:itemNumber inSection:section];
478 } 506 }
479 507
508 // Reconfigures the |item| in the collection view.
509 - (void)reconfigure:(CSCollectionViewItem*)item {
510 CSCollectionViewModel* model =
511 self.collectionViewController.collectionViewModel;
512
513 for (NSInteger sectionNumber = 0; sectionNumber < [model numberOfSections];
514 sectionNumber++) {
515 NSInteger sectionIdentifier =
516 [model sectionIdentifierForSection:sectionNumber];
517 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) {
518 [self.collectionViewController
519 reconfigureCellsForItems:@[ item ]
520 inSectionWithIdentifier:sectionIdentifier];
521 }
522 }
523 }
524
480 @end 525 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698