| 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 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" | 10 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier
.h" | 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier
.h" |
| 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" | 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" |
| 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" | 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" |
| 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour
ce.h" | 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour
ce.h" |
| 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl
e_item.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl
e_item.h" |
| 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" | 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" |
| 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_footer_it
em.h" |
| 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet
cher.h" | 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet
cher.h" |
| 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i
nformation.h" | 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i
nformation.h" |
| 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite
m.h" | 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite
m.h" |
| 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_text_item
.h" | 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_text_item
.h" |
| 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont
roller.h" | 24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont
roller.h" |
| 24 #include "ui/gfx/image/image.h" | 25 #include "ui/gfx/image/image.h" |
| 25 #include "url/gurl.h" | 26 #include "url/gurl.h" |
| 26 | 27 |
| 27 #if !defined(__has_feature) || !__has_feature(objc_arc) | 28 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 28 #error "This file requires ARC support." | 29 #error "This file requires ARC support." |
| 29 #endif | 30 #endif |
| 30 | 31 |
| 31 namespace { | 32 namespace { |
| 32 | 33 |
| 33 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. | 34 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. |
| 34 typedef NS_ENUM(NSInteger, ItemType) { | 35 typedef NS_ENUM(NSInteger, ItemType) { |
| 35 ItemTypeText = kItemTypeEnumZero, | 36 ItemTypeText = kItemTypeEnumZero, |
| 36 ItemTypeArticle, | 37 ItemTypeArticle, |
| 37 ItemTypeExpand, | 38 ItemTypeExpand, |
| 38 ItemTypeStack, | 39 ItemTypeStack, |
| 39 ItemTypeFavicon, | 40 ItemTypeFavicon, |
| 41 ItemTypeFooter, |
| 40 }; | 42 }; |
| 41 | 43 |
| 42 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 44 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 43 SectionIdentifierBookmarks = kSectionIdentifierEnumZero, | 45 SectionIdentifierBookmarks = kSectionIdentifierEnumZero, |
| 44 SectionIdentifierArticles, | 46 SectionIdentifierArticles, |
| 45 SectionIdentifierDefault, | 47 SectionIdentifierDefault, |
| 46 }; | 48 }; |
| 47 | 49 |
| 48 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { | 50 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { |
| 49 switch (type) { | 51 switch (type) { |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 self.collectionViewController.collectionViewModel; | 239 self.collectionViewController.collectionViewModel; |
| 238 for (ContentSuggestion* suggestion in suggestions) { | 240 for (ContentSuggestion* suggestion in suggestions) { |
| 239 ContentSuggestionsSectionInformation* sectionInfo = | 241 ContentSuggestionsSectionInformation* sectionInfo = |
| 240 suggestion.suggestionIdentifier.sectionInfo; | 242 suggestion.suggestionIdentifier.sectionInfo; |
| 241 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); | 243 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| 242 | 244 |
| 243 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { | 245 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { |
| 244 [model addSectionWithIdentifier:sectionIdentifier]; | 246 [model addSectionWithIdentifier:sectionIdentifier]; |
| 245 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; | 247 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; |
| 246 [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; | 248 [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; |
| 249 |
| 250 [self addFooterIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; |
| 247 } | 251 } |
| 248 } | 252 } |
| 249 return indexSet; | 253 return indexSet; |
| 250 } | 254 } |
| 251 | 255 |
| 252 #pragma mark - ContentSuggestionsArticleItemDelegate | 256 #pragma mark - ContentSuggestionsArticleItemDelegate |
| 253 | 257 |
| 254 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { | 258 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
| 255 NSInteger sectionIdentifier = | 259 NSInteger sectionIdentifier = |
| 256 SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo); | 260 SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 274 inSectionWithIdentifier:sectionIdentifier]; | 278 inSectionWithIdentifier:sectionIdentifier]; |
| 275 }; | 279 }; |
| 276 | 280 |
| 277 [self.dataSource.imageFetcher | 281 [self.dataSource.imageFetcher |
| 278 fetchImageForSuggestion:articleItem.suggestionIdentifier | 282 fetchImageForSuggestion:articleItem.suggestionIdentifier |
| 279 callback:imageFetchedCallback]; | 283 callback:imageFetchedCallback]; |
| 280 } | 284 } |
| 281 | 285 |
| 282 #pragma mark - Private methods | 286 #pragma mark - Private methods |
| 283 | 287 |
| 288 // Adds a footer to the section identified by |sectionInfo| if there is none |
| 289 // present and the section info contains a title for it. |
| 290 - (void)addFooterIfNeeded:(ContentSuggestionsSectionInformation*)sectionInfo { |
| 291 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| 292 |
| 293 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 294 if (sectionInfo.footerTitle && |
| 295 ![self.collectionViewController.collectionViewModel |
| 296 footerForSectionWithIdentifier:sectionIdentifier]) { |
| 297 ContentSuggestionsFooterItem* footer = [[ContentSuggestionsFooterItem alloc] |
| 298 initWithType:ItemTypeFooter |
| 299 title:sectionInfo.footerTitle |
| 300 block:^{ |
| 301 [weakSelf runAdditionalActionForSection:sectionInfo]; |
| 302 }]; |
| 303 |
| 304 [self.collectionViewController.collectionViewModel |
| 305 setFooter:footer |
| 306 forSectionWithIdentifier:sectionIdentifier]; |
| 307 } |
| 308 } |
| 309 |
| 284 // Resets the models, removing the current CollectionViewItem and the | 310 // Resets the models, removing the current CollectionViewItem and the |
| 285 // SectionInfo. | 311 // SectionInfo. |
| 286 - (void)resetModels { | 312 - (void)resetModels { |
| 287 [self.collectionViewController loadModel]; | 313 [self.collectionViewController loadModel]; |
| 288 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; | 314 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; |
| 289 } | 315 } |
| 290 | 316 |
| 317 // Runs the additional action for the section identified by |sectionInfo|. |
| 318 - (void)runAdditionalActionForSection: |
| 319 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 320 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| 321 |
| 322 NSMutableArray<ContentSuggestionIdentifier*>* knownSuggestionIdentifiers = |
| 323 [NSMutableArray array]; |
| 324 |
| 325 NSArray<CollectionViewItem<ContentSuggestionIdentification>*>* |
| 326 knownSuggestions = [self.collectionViewController.collectionViewModel |
| 327 itemsInSectionWithIdentifier:sectionIdentifier]; |
| 328 for (CollectionViewItem<ContentSuggestionIdentification>* suggestion in |
| 329 knownSuggestions) { |
| 330 [knownSuggestionIdentifiers addObject:suggestion.suggestionIdentifier]; |
| 331 } |
| 332 |
| 333 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 334 [self.dataSource |
| 335 fetchMoreSuggestionsKnowing:knownSuggestionIdentifiers |
| 336 fromSectionInfo:sectionInfo |
| 337 callback:^(NSArray<ContentSuggestion*>* suggestions) { |
| 338 [weakSelf moreSuggestionsFetched:suggestions]; |
| 339 }]; |
| 340 } |
| 341 |
| 342 // Adds the |suggestions| to the collection view. All the suggestions must have |
| 343 // the same sectionInfo. |
| 344 - (void)moreSuggestionsFetched:(NSArray<ContentSuggestion*>*)suggestions { |
| 345 [self.collectionViewController addSuggestions:suggestions]; |
| 346 } |
| 347 |
| 291 @end | 348 @end |
| OLD | NEW |