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

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

Issue 2782613004: Add FaviconAttributesProvider to ContentSuggestions (Closed)
Patch Set: Cleanup 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/time/time.h" 9 #include "base/time/time.h"
10 #include "components/strings/grit/components_strings.h" 10 #include "components/strings/grit/components_strings.h"
11 #import "ios/chrome/browser/favicon/favicon_attributes_provider.h"
11 #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 "
12 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 13 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
13 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 14 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
14 #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"
15 #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"
16 #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"
17 #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"
18 #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"
19 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_sta ck_item.h" 20 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_sta ck_item.h"
20 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h" 21 #import "ios/chrome/browser/ui/content_suggestions/cells/content_suggestions_tex t_item.h"
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after
233 CSCollectionViewItem* item = 234 CSCollectionViewItem* item =
234 [self emptyItemForSectionInfo:sectionInfo]; 235 [self emptyItemForSectionInfo:sectionInfo];
235 NSIndexPath* addedIndexPath = 236 NSIndexPath* addedIndexPath =
236 [self addItem:item toSectionWithIdentifier:sectionIdentifier]; 237 [self addItem:item toSectionWithIdentifier:sectionIdentifier];
237 [indexPaths addObject:addedIndexPath]; 238 [indexPaths addObject:addedIndexPath];
238 } 239 }
239 break; 240 break;
240 } 241 }
241 case ContentSuggestionTypeArticle: { 242 case ContentSuggestionTypeArticle: {
242 ContentSuggestionsArticleItem* articleItem = 243 ContentSuggestionsArticleItem* articleItem =
243 [[ContentSuggestionsArticleItem alloc] 244 [self articleItemForSuggestion:suggestion];
244 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
245 title:suggestion.title
246 subtitle:suggestion.text
247 delegate:self
248 url:suggestion.url];
249
250 articleItem.publisher = suggestion.publisher;
251 articleItem.publishDate = suggestion.publishDate;
252
253 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
254 245
255 NSIndexPath* addedIndexPath = [self addItem:articleItem 246 NSIndexPath* addedIndexPath = [self addItem:articleItem
256 toSectionWithIdentifier:sectionIdentifier]; 247 toSectionWithIdentifier:sectionIdentifier];
257 [indexPaths addObject:addedIndexPath]; 248 [indexPaths addObject:addedIndexPath];
258 break; 249 break;
259 } 250 }
260 } 251 }
261 } 252 }
262 253
263 return indexPaths; 254 return indexPaths;
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 - (CSCollectionViewItem*)emptyItemForSectionInfo: 405 - (CSCollectionViewItem*)emptyItemForSectionInfo:
415 (ContentSuggestionsSectionInformation*)sectionInfo { 406 (ContentSuggestionsSectionInformation*)sectionInfo {
416 ContentSuggestionsTextItem* item = 407 ContentSuggestionsTextItem* item =
417 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty]; 408 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty];
418 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS); 409 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS);
419 item.detailText = sectionInfo.emptyText; 410 item.detailText = sectionInfo.emptyText;
420 411
421 return item; 412 return item;
422 } 413 }
423 414
415 // Returns an article build with the |suggestion|.
416 - (ContentSuggestionsArticleItem*)articleItemForSuggestion:
417 (ContentSuggestion*)suggestion {
418 ContentSuggestionsArticleItem* articleItem =
419 [[ContentSuggestionsArticleItem alloc]
420 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
421 title:suggestion.title
422 subtitle:suggestion.text
423 delegate:self
424 url:suggestion.url];
425
426 articleItem.publisher = suggestion.publisher;
427 articleItem.publishDate = suggestion.publishDate;
428
429 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
430
431 __weak ContentSuggestionsCollectionUpdater* weakSelf = self;
432 __weak ContentSuggestionsArticleItem* weakItem = articleItem;
433 void (^completionBlock)(FaviconAttributes* attributes) =
434 ^(FaviconAttributes* attributes) {
435 ContentSuggestionsArticleItem* strongItem = weakItem;
436 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf;
437 if (!strongSelf || !strongItem) {
438 return;
439 }
440
441 strongItem.attributes = attributes;
442
443 [strongSelf reconfigure:strongItem];
444 };
445
446 [self.dataSource.attributesProvider
lpromero 2017/03/29 13:32:07 I would abstract this in a data source method.
gambard 2017/03/30 08:07:11 Done.
447 fetchFaviconAttributesForURL:articleItem.articleURL
448 completion:completionBlock];
449
450 return articleItem;
451 }
452
424 // Adds |item| to |sectionIdentifier| section of the model of the 453 // Adds |item| to |sectionIdentifier| section of the model of the
425 // CollectionView. Returns the IndexPath of the newly added item. 454 // CollectionView. Returns the IndexPath of the newly added item.
426 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item 455 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item
427 toSectionWithIdentifier:(NSInteger)sectionIdentifier { 456 toSectionWithIdentifier:(NSInteger)sectionIdentifier {
428 CSCollectionViewModel* model = 457 CSCollectionViewModel* model =
429 self.collectionViewController.collectionViewModel; 458 self.collectionViewController.collectionViewModel;
430 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; 459 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier];
431 NSInteger itemNumber = [model numberOfItemsInSection:section]; 460 NSInteger itemNumber = [model numberOfItemsInSection:section];
432 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; 461 [model addItem:item toSectionWithIdentifier:sectionIdentifier];
433 462
434 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; 463 return [NSIndexPath indexPathForItem:itemNumber inSection:section];
435 } 464 }
436 465
466 // Reconfigures the |item| in the collection view.
467 - (void)reconfigure:(CSCollectionViewItem*)item {
468 CSCollectionViewModel* model =
469 self.collectionViewController.collectionViewModel;
470
471 for (NSInteger sectionNumber = 0; sectionNumber < [model numberOfSections];
472 sectionNumber++) {
473 NSInteger sectionIdentifier =
474 [model sectionIdentifierForSection:sectionNumber];
475 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) {
476 [self.collectionViewController
477 reconfigureCellsForItems:@[ item ]
478 inSectionWithIdentifier:sectionIdentifier];
479 }
480 }
481 }
482
437 @end 483 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698