| OLD | NEW |
| 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/ui/collection_view/cells/collection_view_text_item.h
" | 11 #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" | 12 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 13 #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" | 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_but
ton_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_exp
andable_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_fav
icon_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_foo
ter_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_sta
ck_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_tex
t_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/content_suggestion.h" | 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" | 22 #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" | 23 #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" | 24 #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" | 25 #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" | 26 #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" | 27 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion
s_section_information.h" |
| 28 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h" |
| 28 #include "ui/base/l10n/l10n_util.h" | 29 #include "ui/base/l10n/l10n_util.h" |
| 29 #include "ui/gfx/image/image.h" | 30 #include "ui/gfx/image/image.h" |
| 30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
| 31 | 32 |
| 32 #if !defined(__has_feature) || !__has_feature(objc_arc) | 33 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 33 #error "This file requires ARC support." | 34 #error "This file requires ARC support." |
| 34 #endif | 35 #endif |
| 35 | 36 |
| 36 namespace { | 37 namespace { |
| 37 | 38 |
| (...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 Loading... |
| 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 fetchFaviconAttributesForURL:articleItem.articleURL |
| 447 completion:completionBlock]; |
| 448 |
| 449 return articleItem; |
| 450 } |
| 451 |
| 424 // Adds |item| to |sectionIdentifier| section of the model of the | 452 // Adds |item| to |sectionIdentifier| section of the model of the |
| 425 // CollectionView. Returns the IndexPath of the newly added item. | 453 // CollectionView. Returns the IndexPath of the newly added item. |
| 426 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item | 454 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item |
| 427 toSectionWithIdentifier:(NSInteger)sectionIdentifier { | 455 toSectionWithIdentifier:(NSInteger)sectionIdentifier { |
| 428 CSCollectionViewModel* model = | 456 CSCollectionViewModel* model = |
| 429 self.collectionViewController.collectionViewModel; | 457 self.collectionViewController.collectionViewModel; |
| 430 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; | 458 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| 431 NSInteger itemNumber = [model numberOfItemsInSection:section]; | 459 NSInteger itemNumber = [model numberOfItemsInSection:section]; |
| 432 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; | 460 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| 433 | 461 |
| 434 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; | 462 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; |
| 435 } | 463 } |
| 436 | 464 |
| 465 // Reconfigures the |item| in the collection view. |
| 466 - (void)reconfigure:(CSCollectionViewItem*)item { |
| 467 CSCollectionViewModel* model = |
| 468 self.collectionViewController.collectionViewModel; |
| 469 |
| 470 for (NSInteger sectionNumber = 0; sectionNumber < [model numberOfSections]; |
| 471 sectionNumber++) { |
| 472 NSInteger sectionIdentifier = |
| 473 [model sectionIdentifierForSection:sectionNumber]; |
| 474 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) { |
| 475 [self.collectionViewController |
| 476 reconfigureCellsForItems:@[ item ] |
| 477 inSectionWithIdentifier:sectionIdentifier]; |
| 478 } |
| 479 } |
| 480 } |
| 481 |
| 437 @end | 482 @end |
| OLD | NEW |