Chromium Code Reviews| 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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 ContentSuggestionsArticleItem* articleItem = | 269 ContentSuggestionsArticleItem* articleItem = |
| 270 [self articleItemForSuggestion:suggestion]; | 270 [self articleItemForSuggestion:suggestion]; |
| 271 | 271 |
| 272 NSIndexPath* addedIndexPath = [self addItem:articleItem | 272 NSIndexPath* addedIndexPath = [self addItem:articleItem |
| 273 toSectionWithIdentifier:sectionIdentifier]; | 273 toSectionWithIdentifier:sectionIdentifier]; |
| 274 [indexPaths addObject:addedIndexPath]; | 274 [indexPaths addObject:addedIndexPath]; |
| 275 break; | 275 break; |
| 276 } | 276 } |
| 277 case ContentSuggestionTypeReadingList: { | 277 case ContentSuggestionTypeReadingList: { |
| 278 ContentSuggestionsReadingListItem* readingListItem = | 278 ContentSuggestionsReadingListItem* readingListItem = |
| 279 [[ContentSuggestionsReadingListItem alloc] | 279 [self readingListItemForSuggestion:suggestion]; |
| 280 initWithType:ItemTypeReadingList | |
| 281 url:suggestion.url | |
| 282 distillationState:suggestion.readingListExtra.status]; | |
| 283 readingListItem.title = suggestion.title; | |
| 284 readingListItem.subtitle = suggestion.publisher; | |
| 285 | |
| 286 readingListItem.suggestionIdentifier = suggestion.suggestionIdentifier; | |
| 287 | 280 |
| 288 NSIndexPath* addedIndexPath = [self addItem:readingListItem | 281 NSIndexPath* addedIndexPath = [self addItem:readingListItem |
| 289 toSectionWithIdentifier:sectionIdentifier]; | 282 toSectionWithIdentifier:sectionIdentifier]; |
| 290 [indexPaths addObject:addedIndexPath]; | 283 [indexPaths addObject:addedIndexPath]; |
| 291 break; | 284 break; |
| 292 } | 285 } |
| 293 } | 286 } |
| 294 } | 287 } |
| 295 | 288 |
| 296 return indexPaths; | 289 return indexPaths; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 460 title:suggestion.title | 453 title:suggestion.title |
| 461 subtitle:suggestion.text | 454 subtitle:suggestion.text |
| 462 delegate:self | 455 delegate:self |
| 463 url:suggestion.url]; | 456 url:suggestion.url]; |
| 464 | 457 |
| 465 articleItem.publisher = suggestion.publisher; | 458 articleItem.publisher = suggestion.publisher; |
| 466 articleItem.publishDate = suggestion.publishDate; | 459 articleItem.publishDate = suggestion.publishDate; |
| 467 | 460 |
| 468 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; | 461 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; |
| 469 | 462 |
| 463 __weak ContentSuggestionsArticleItem* weakItem = articleItem; | |
| 464 [self fetchFaviconForItem:articleItem | |
| 465 withURL:articleItem.articleURL | |
| 466 callback:^void(FaviconAttributes* attributes) { | |
| 467 weakItem.attributes = attributes; | |
| 468 }]; | |
| 469 | |
| 470 return articleItem; | |
| 471 } | |
| 472 | |
| 473 // Returns a reading list item build with the |suggestion|. | |
|
lpromero
2017/04/05 13:14:14
*built
gambard
2017/04/06 11:17:47
Done.
| |
| 474 - (ContentSuggestionsReadingListItem*)readingListItemForSuggestion: | |
| 475 (ContentSuggestion*)suggestion { | |
| 476 ContentSuggestionsReadingListItem* readingListItem = | |
| 477 [[ContentSuggestionsReadingListItem alloc] | |
| 478 initWithType:ItemTypeReadingList | |
| 479 url:suggestion.url | |
| 480 distillationState:suggestion.readingListExtra.status]; | |
| 481 | |
| 482 readingListItem.title = suggestion.title; | |
| 483 readingListItem.subtitle = suggestion.publisher; | |
| 484 | |
| 485 readingListItem.suggestionIdentifier = suggestion.suggestionIdentifier; | |
| 486 | |
| 487 __weak ContentSuggestionsReadingListItem* weakItem = readingListItem; | |
| 488 [self fetchFaviconForItem:readingListItem | |
| 489 withURL:readingListItem.url | |
| 490 callback:^void(FaviconAttributes* attributes) { | |
| 491 weakItem.attributes = attributes; | |
| 492 }]; | |
| 493 | |
| 494 return readingListItem; | |
| 495 } | |
| 496 | |
| 497 // Fetches the favicon associated with the |URL|, call the |callback| with the | |
| 498 // attributes then reconfigure the |item|. | |
| 499 - (void)fetchFaviconForItem:(CSCollectionViewItem*)item | |
| 500 withURL:(const GURL&)URL | |
| 501 callback:(void (^)(FaviconAttributes*))callback { | |
| 502 if (!callback) | |
| 503 return; | |
| 504 | |
| 470 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; | 505 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 471 __weak ContentSuggestionsArticleItem* weakItem = articleItem; | 506 __weak CSCollectionViewItem* weakItem = item; |
| 472 void (^completionBlock)(FaviconAttributes* attributes) = | 507 void (^completionBlock)(FaviconAttributes* attributes) = |
| 473 ^(FaviconAttributes* attributes) { | 508 ^(FaviconAttributes* attributes) { |
| 474 ContentSuggestionsArticleItem* strongItem = weakItem; | 509 CSCollectionViewItem* strongItem = weakItem; |
| 475 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; | 510 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; |
| 476 if (!strongSelf || !strongItem) { | 511 if (!strongSelf || !strongItem) { |
| 477 return; | 512 return; |
| 478 } | 513 } |
| 479 | 514 |
| 480 strongItem.attributes = attributes; | 515 callback(attributes); |
| 481 | 516 |
| 482 [strongSelf reconfigure:strongItem]; | 517 [strongSelf reconfigure:strongItem]; |
| 483 }; | 518 }; |
| 484 | 519 |
| 485 [self.dataSource fetchFaviconAttributesForURL:articleItem.articleURL | 520 [self.dataSource fetchFaviconAttributesForURL:URL completion:completionBlock]; |
| 486 completion:completionBlock]; | |
| 487 | |
| 488 return articleItem; | |
| 489 } | 521 } |
| 490 | 522 |
| 491 // Adds |item| to |sectionIdentifier| section of the model of the | 523 // Adds |item| to |sectionIdentifier| section of the model of the |
| 492 // CollectionView. Returns the IndexPath of the newly added item. | 524 // CollectionView. Returns the IndexPath of the newly added item. |
| 493 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item | 525 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item |
| 494 toSectionWithIdentifier:(NSInteger)sectionIdentifier { | 526 toSectionWithIdentifier:(NSInteger)sectionIdentifier { |
| 495 CSCollectionViewModel* model = | 527 CSCollectionViewModel* model = |
| 496 self.collectionViewController.collectionViewModel; | 528 self.collectionViewController.collectionViewModel; |
| 497 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; | 529 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| 498 NSInteger itemNumber = [model numberOfItemsInSection:section]; | 530 NSInteger itemNumber = [model numberOfItemsInSection:section]; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 512 [model sectionIdentifierForSection:sectionNumber]; | 544 [model sectionIdentifierForSection:sectionNumber]; |
| 513 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) { | 545 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) { |
| 514 [self.collectionViewController | 546 [self.collectionViewController |
| 515 reconfigureCellsForItems:@[ item ] | 547 reconfigureCellsForItems:@[ item ] |
| 516 inSectionWithIdentifier:sectionIdentifier]; | 548 inSectionWithIdentifier:sectionIdentifier]; |
| 517 } | 549 } |
| 518 } | 550 } |
| 519 } | 551 } |
| 520 | 552 |
| 521 @end | 553 @end |
| OLD | NEW |