| 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/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" |
| (...skipping 246 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 257 ContentSuggestionsArticleItem* articleItem = | 257 ContentSuggestionsArticleItem* articleItem = |
| 258 [self articleItemForSuggestion:suggestion]; | 258 [self articleItemForSuggestion:suggestion]; |
| 259 | 259 |
| 260 NSIndexPath* addedIndexPath = [self addItem:articleItem | 260 NSIndexPath* addedIndexPath = [self addItem:articleItem |
| 261 toSectionWithIdentifier:sectionIdentifier]; | 261 toSectionWithIdentifier:sectionIdentifier]; |
| 262 [indexPaths addObject:addedIndexPath]; | 262 [indexPaths addObject:addedIndexPath]; |
| 263 break; | 263 break; |
| 264 } | 264 } |
| 265 case ContentSuggestionTypeReadingList: { | 265 case ContentSuggestionTypeReadingList: { |
| 266 ContentSuggestionsReadingListItem* readingListItem = | 266 ContentSuggestionsReadingListItem* readingListItem = |
| 267 [[ContentSuggestionsReadingListItem alloc] | 267 [self readingListItemForSuggestion:suggestion]; |
| 268 initWithType:ItemTypeReadingList | |
| 269 url:suggestion.url | |
| 270 distillationState:suggestion.readingListExtra.status]; | |
| 271 readingListItem.title = suggestion.title; | |
| 272 readingListItem.subtitle = suggestion.publisher; | |
| 273 | |
| 274 readingListItem.suggestionIdentifier = suggestion.suggestionIdentifier; | |
| 275 | 268 |
| 276 NSIndexPath* addedIndexPath = [self addItem:readingListItem | 269 NSIndexPath* addedIndexPath = [self addItem:readingListItem |
| 277 toSectionWithIdentifier:sectionIdentifier]; | 270 toSectionWithIdentifier:sectionIdentifier]; |
| 278 [indexPaths addObject:addedIndexPath]; | 271 [indexPaths addObject:addedIndexPath]; |
| 279 break; | 272 break; |
| 280 } | 273 } |
| 281 } | 274 } |
| 282 } | 275 } |
| 283 | 276 |
| 284 return indexPaths; | 277 return indexPaths; |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 432 - (CSCollectionViewItem*)emptyItemForSectionInfo: | 425 - (CSCollectionViewItem*)emptyItemForSectionInfo: |
| 433 (ContentSuggestionsSectionInformation*)sectionInfo { | 426 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 434 ContentSuggestionsTextItem* item = | 427 ContentSuggestionsTextItem* item = |
| 435 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty]; | 428 [[ContentSuggestionsTextItem alloc] initWithType:ItemTypeEmpty]; |
| 436 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS); | 429 item.text = l10n_util::GetNSString(IDS_NTP_TITLE_NO_SUGGESTIONS); |
| 437 item.detailText = sectionInfo.emptyText; | 430 item.detailText = sectionInfo.emptyText; |
| 438 | 431 |
| 439 return item; | 432 return item; |
| 440 } | 433 } |
| 441 | 434 |
| 442 // Returns an article build with the |suggestion|. | 435 // Returns an article built with the |suggestion|. |
| 443 - (ContentSuggestionsArticleItem*)articleItemForSuggestion: | 436 - (ContentSuggestionsArticleItem*)articleItemForSuggestion: |
| 444 (ContentSuggestion*)suggestion { | 437 (ContentSuggestion*)suggestion { |
| 445 ContentSuggestionsArticleItem* articleItem = | 438 ContentSuggestionsArticleItem* articleItem = |
| 446 [[ContentSuggestionsArticleItem alloc] | 439 [[ContentSuggestionsArticleItem alloc] |
| 447 initWithType:ItemTypeForContentSuggestionType(suggestion.type) | 440 initWithType:ItemTypeForContentSuggestionType(suggestion.type) |
| 448 title:suggestion.title | 441 title:suggestion.title |
| 449 subtitle:suggestion.text | 442 subtitle:suggestion.text |
| 450 delegate:self | 443 delegate:self |
| 451 url:suggestion.url]; | 444 url:suggestion.url]; |
| 452 | 445 |
| 453 articleItem.publisher = suggestion.publisher; | 446 articleItem.publisher = suggestion.publisher; |
| 454 articleItem.publishDate = suggestion.publishDate; | 447 articleItem.publishDate = suggestion.publishDate; |
| 455 | 448 |
| 456 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; | 449 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; |
| 457 | 450 |
| 451 __weak ContentSuggestionsArticleItem* weakItem = articleItem; |
| 452 [self fetchFaviconForItem:articleItem |
| 453 withURL:articleItem.articleURL |
| 454 callback:^void(FaviconAttributes* attributes) { |
| 455 weakItem.attributes = attributes; |
| 456 }]; |
| 457 |
| 458 return articleItem; |
| 459 } |
| 460 |
| 461 // Returns a reading list item built with the |suggestion|. |
| 462 - (ContentSuggestionsReadingListItem*)readingListItemForSuggestion: |
| 463 (ContentSuggestion*)suggestion { |
| 464 ContentSuggestionsReadingListItem* readingListItem = |
| 465 [[ContentSuggestionsReadingListItem alloc] |
| 466 initWithType:ItemTypeReadingList |
| 467 url:suggestion.url |
| 468 distillationState:suggestion.readingListExtra.status]; |
| 469 |
| 470 readingListItem.title = suggestion.title; |
| 471 readingListItem.subtitle = suggestion.publisher; |
| 472 |
| 473 readingListItem.suggestionIdentifier = suggestion.suggestionIdentifier; |
| 474 |
| 475 __weak ContentSuggestionsReadingListItem* weakItem = readingListItem; |
| 476 [self fetchFaviconForItem:readingListItem |
| 477 withURL:readingListItem.url |
| 478 callback:^void(FaviconAttributes* attributes) { |
| 479 weakItem.attributes = attributes; |
| 480 }]; |
| 481 |
| 482 return readingListItem; |
| 483 } |
| 484 |
| 485 // Fetches the favicon associated with the |URL|, call the |callback| with the |
| 486 // attributes then reconfigure the |item|. |
| 487 - (void)fetchFaviconForItem:(CSCollectionViewItem*)item |
| 488 withURL:(const GURL&)URL |
| 489 callback:(void (^)(FaviconAttributes*))callback { |
| 490 if (!callback) |
| 491 return; |
| 492 |
| 458 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; | 493 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 459 __weak ContentSuggestionsArticleItem* weakItem = articleItem; | 494 __weak CSCollectionViewItem* weakItem = item; |
| 460 void (^completionBlock)(FaviconAttributes* attributes) = | 495 void (^completionBlock)(FaviconAttributes* attributes) = |
| 461 ^(FaviconAttributes* attributes) { | 496 ^(FaviconAttributes* attributes) { |
| 462 ContentSuggestionsArticleItem* strongItem = weakItem; | 497 CSCollectionViewItem* strongItem = weakItem; |
| 463 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; | 498 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; |
| 464 if (!strongSelf || !strongItem) { | 499 if (!strongSelf || !strongItem) { |
| 465 return; | 500 return; |
| 466 } | 501 } |
| 467 | 502 |
| 468 strongItem.attributes = attributes; | 503 callback(attributes); |
| 469 | 504 |
| 470 [strongSelf reconfigure:strongItem]; | 505 [strongSelf reconfigure:strongItem]; |
| 471 }; | 506 }; |
| 472 | 507 |
| 473 [self.dataSource fetchFaviconAttributesForURL:articleItem.articleURL | 508 [self.dataSource fetchFaviconAttributesForURL:URL completion:completionBlock]; |
| 474 completion:completionBlock]; | |
| 475 | |
| 476 return articleItem; | |
| 477 } | 509 } |
| 478 | 510 |
| 479 // Adds |item| to |sectionIdentifier| section of the model of the | 511 // Adds |item| to |sectionIdentifier| section of the model of the |
| 480 // CollectionView. Returns the IndexPath of the newly added item. | 512 // CollectionView. Returns the IndexPath of the newly added item. |
| 481 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item | 513 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item |
| 482 toSectionWithIdentifier:(NSInteger)sectionIdentifier { | 514 toSectionWithIdentifier:(NSInteger)sectionIdentifier { |
| 483 CSCollectionViewModel* model = | 515 CSCollectionViewModel* model = |
| 484 self.collectionViewController.collectionViewModel; | 516 self.collectionViewController.collectionViewModel; |
| 485 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; | 517 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| 486 NSInteger itemNumber = [model numberOfItemsInSection:section]; | 518 NSInteger itemNumber = [model numberOfItemsInSection:section]; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 500 [model sectionIdentifierForSection:sectionNumber]; | 532 [model sectionIdentifierForSection:sectionNumber]; |
| 501 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) { | 533 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) { |
| 502 [self.collectionViewController | 534 [self.collectionViewController |
| 503 reconfigureCellsForItems:@[ item ] | 535 reconfigureCellsForItems:@[ item ] |
| 504 inSectionWithIdentifier:sectionIdentifier]; | 536 inSectionWithIdentifier:sectionIdentifier]; |
| 505 } | 537 } |
| 506 } | 538 } |
| 507 } | 539 } |
| 508 | 540 |
| 509 @end | 541 @end |
| OLD | NEW |