| 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 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 if (item.suggestionIdentifier == suggestionIdentifier) { | 177 if (item.suggestionIdentifier == suggestionIdentifier) { |
| 178 correspondingItem = item; | 178 correspondingItem = item; |
| 179 break; | 179 break; |
| 180 } | 180 } |
| 181 } | 181 } |
| 182 | 182 |
| 183 if (!correspondingItem) | 183 if (!correspondingItem) |
| 184 return; | 184 return; |
| 185 | 185 |
| 186 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel | 186 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel |
| 187 indexPathForItem:correspondingItem | 187 indexPathForItem:correspondingItem]; |
| 188 inSectionWithIdentifier:sectionIdentifier]; | |
| 189 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; | 188 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; |
| 190 } | 189 } |
| 191 | 190 |
| 192 - (void)reloadAllData { | 191 - (void)reloadAllData { |
| 193 [self resetModels]; | 192 [self resetModels]; |
| 194 | 193 |
| 195 // The data is reset, add the new data directly in the model then reload the | 194 // The data is reset, add the new data directly in the model then reload the |
| 196 // collection. | 195 // collection. |
| 197 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; | 196 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; |
| 198 [self addSectionsForSuggestionsToModel:suggestions]; | 197 [self addSectionsForSuggestionsToModel:suggestions]; |
| (...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 332 ContentSuggestionsSectionInformation* sectionInfo = | 331 ContentSuggestionsSectionInformation* sectionInfo = |
| 333 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)]; | 332 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)]; |
| 334 | 333 |
| 335 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo]; | 334 CSCollectionViewItem* item = [self emptyItemForSectionInfo:sectionInfo]; |
| 336 return [self addItem:item toSectionWithIdentifier:sectionIdentifier]; | 335 return [self addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| 337 } | 336 } |
| 338 | 337 |
| 339 #pragma mark - ContentSuggestionsArticleItemDelegate | 338 #pragma mark - ContentSuggestionsArticleItemDelegate |
| 340 | 339 |
| 341 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { | 340 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
| 342 NSInteger sectionIdentifier = | |
| 343 SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo); | |
| 344 | |
| 345 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; | 341 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 346 __weak ContentSuggestionsArticleItem* weakArticle = articleItem; | 342 __weak ContentSuggestionsArticleItem* weakArticle = articleItem; |
| 347 | 343 |
| 348 void (^imageFetchedCallback)(UIImage*) = ^(UIImage* image) { | 344 void (^imageFetchedCallback)(UIImage*) = ^(UIImage* image) { |
| 349 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; | 345 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; |
| 350 ContentSuggestionsArticleItem* strongArticle = weakArticle; | 346 ContentSuggestionsArticleItem* strongArticle = weakArticle; |
| 351 if (!strongSelf || !strongArticle) { | 347 if (!strongSelf || !strongArticle) { |
| 352 return; | 348 return; |
| 353 } | 349 } |
| 354 | 350 |
| 355 strongArticle.image = image; | 351 strongArticle.image = image; |
| 356 [strongSelf.collectionViewController | 352 [strongSelf.collectionViewController |
| 357 reconfigureCellsForItems:@[ strongArticle ] | 353 reconfigureCellsForItems:@[ strongArticle ]]; |
| 358 inSectionWithIdentifier:sectionIdentifier]; | |
| 359 }; | 354 }; |
| 360 | 355 |
| 361 [self.dataSource.imageFetcher | 356 [self.dataSource.imageFetcher |
| 362 fetchImageForSuggestion:articleItem.suggestionIdentifier | 357 fetchImageForSuggestion:articleItem.suggestionIdentifier |
| 363 callback:imageFetchedCallback]; | 358 callback:imageFetchedCallback]; |
| 364 } | 359 } |
| 365 | 360 |
| 366 #pragma mark - Private methods | 361 #pragma mark - Private methods |
| 367 | 362 |
| 368 // Adds a footer to the section identified by |sectionInfo| if there is none | 363 // Adds a footer to the section identified by |sectionInfo| if there is none |
| (...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 | 473 |
| 479 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; | 474 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 480 [self.dataSource | 475 [self.dataSource |
| 481 fetchFaviconImageForSuggestion:articleItem.suggestionIdentifier | 476 fetchFaviconImageForSuggestion:articleItem.suggestionIdentifier |
| 482 completion:^void(UIImage* favicon) { | 477 completion:^void(UIImage* favicon) { |
| 483 if (!weakItem || !weakSelf) | 478 if (!weakItem || !weakSelf) |
| 484 return; | 479 return; |
| 485 | 480 |
| 486 weakItem.attributes = | 481 weakItem.attributes = |
| 487 [FaviconAttributes attributesWithImage:favicon]; | 482 [FaviconAttributes attributesWithImage:favicon]; |
| 488 [weakSelf reconfigure:weakItem]; | 483 [weakSelf.collectionViewController |
| 484 reconfigureCellsForItems:@[ weakItem ]]; |
| 489 }]; | 485 }]; |
| 490 | 486 |
| 491 return articleItem; | 487 return articleItem; |
| 492 } | 488 } |
| 493 | 489 |
| 494 // Returns a reading list item built with the |suggestion|. | 490 // Returns a reading list item built with the |suggestion|. |
| 495 - (ContentSuggestionsReadingListItem*)readingListItemForSuggestion: | 491 - (ContentSuggestionsReadingListItem*)readingListItemForSuggestion: |
| 496 (ContentSuggestion*)suggestion { | 492 (ContentSuggestion*)suggestion { |
| 497 ContentSuggestionsReadingListItem* readingListItem = | 493 ContentSuggestionsReadingListItem* readingListItem = |
| 498 [[ContentSuggestionsReadingListItem alloc] | 494 [[ContentSuggestionsReadingListItem alloc] |
| (...skipping 29 matching lines...) Expand all Loading... |
| 528 void (^completionBlock)(FaviconAttributes* attributes) = | 524 void (^completionBlock)(FaviconAttributes* attributes) = |
| 529 ^(FaviconAttributes* attributes) { | 525 ^(FaviconAttributes* attributes) { |
| 530 CSCollectionViewItem* strongItem = weakItem; | 526 CSCollectionViewItem* strongItem = weakItem; |
| 531 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; | 527 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; |
| 532 if (!strongSelf || !strongItem) { | 528 if (!strongSelf || !strongItem) { |
| 533 return; | 529 return; |
| 534 } | 530 } |
| 535 | 531 |
| 536 callback(attributes); | 532 callback(attributes); |
| 537 | 533 |
| 538 [strongSelf reconfigure:strongItem]; | 534 [strongSelf.collectionViewController |
| 535 reconfigureCellsForItems:@[ strongItem ]]; |
| 539 }; | 536 }; |
| 540 | 537 |
| 541 [self.dataSource fetchFaviconAttributesForURL:URL completion:completionBlock]; | 538 [self.dataSource fetchFaviconAttributesForURL:URL completion:completionBlock]; |
| 542 } | 539 } |
| 543 | 540 |
| 544 // Adds |item| to |sectionIdentifier| section of the model of the | 541 // Adds |item| to |sectionIdentifier| section of the model of the |
| 545 // CollectionView. Returns the IndexPath of the newly added item. | 542 // CollectionView. Returns the IndexPath of the newly added item. |
| 546 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item | 543 - (NSIndexPath*)addItem:(CSCollectionViewItem*)item |
| 547 toSectionWithIdentifier:(NSInteger)sectionIdentifier { | 544 toSectionWithIdentifier:(NSInteger)sectionIdentifier { |
| 548 CSCollectionViewModel* model = | 545 CSCollectionViewModel* model = |
| 549 self.collectionViewController.collectionViewModel; | 546 self.collectionViewController.collectionViewModel; |
| 550 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; | 547 NSInteger section = [model sectionForSectionIdentifier:sectionIdentifier]; |
| 551 NSInteger itemNumber = [model numberOfItemsInSection:section]; | 548 NSInteger itemNumber = [model numberOfItemsInSection:section]; |
| 552 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; | 549 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| 553 | 550 |
| 554 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; | 551 return [NSIndexPath indexPathForItem:itemNumber inSection:section]; |
| 555 } | 552 } |
| 556 | 553 |
| 557 // Reconfigures the |item| in the collection view. | |
| 558 - (void)reconfigure:(CSCollectionViewItem*)item { | |
| 559 CSCollectionViewModel* model = | |
| 560 self.collectionViewController.collectionViewModel; | |
| 561 | |
| 562 for (NSInteger sectionNumber = 0; sectionNumber < [model numberOfSections]; | |
| 563 sectionNumber++) { | |
| 564 NSInteger sectionIdentifier = | |
| 565 [model sectionIdentifierForSection:sectionNumber]; | |
| 566 if ([model hasItem:item inSectionWithIdentifier:sectionIdentifier]) { | |
| 567 [self.collectionViewController | |
| 568 reconfigureCellsForItems:@[ item ] | |
| 569 inSectionWithIdentifier:sectionIdentifier]; | |
| 570 } | |
| 571 } | |
| 572 } | |
| 573 | |
| 574 @end | 554 @end |
| OLD | NEW |