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/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_footer_it em.h" |
| 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_header_it em.h" | |
| 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" | 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet cher.h" |
| 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h" | 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h" |
| 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite m.h" | 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite m.h" |
| 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_text_item .h" | 24 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_text_item .h" |
| 24 #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" |
| 25 #include "ui/gfx/image/image.h" | 26 #include "ui/gfx/image/image.h" |
| 26 #include "url/gurl.h" | 27 #include "url/gurl.h" |
| 27 | 28 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) | 29 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." | 30 #error "This file requires ARC support." |
| 30 #endif | 31 #endif |
| 31 | 32 |
| 32 namespace { | 33 namespace { |
| 33 | 34 |
| 34 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. | 35 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. |
| 35 typedef NS_ENUM(NSInteger, ItemType) { | 36 typedef NS_ENUM(NSInteger, ItemType) { |
| 36 ItemTypeText = kItemTypeEnumZero, | 37 ItemTypeText = kItemTypeEnumZero, |
| 37 ItemTypeArticle, | 38 ItemTypeArticle, |
| 38 ItemTypeExpand, | 39 ItemTypeExpand, |
| 39 ItemTypeStack, | 40 ItemTypeStack, |
| 40 ItemTypeFavicon, | 41 ItemTypeFavicon, |
| 41 ItemTypeFooter, | 42 ItemTypeFooter, |
| 43 ItemTypeHeader, | |
| 42 }; | 44 }; |
| 43 | 45 |
| 44 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 46 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 45 SectionIdentifierBookmarks = kSectionIdentifierEnumZero, | 47 SectionIdentifierBookmarks = kSectionIdentifierEnumZero, |
| 46 SectionIdentifierArticles, | 48 SectionIdentifierArticles, |
| 47 SectionIdentifierDefault, | 49 SectionIdentifierDefault, |
| 48 }; | 50 }; |
| 49 | 51 |
| 50 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { | 52 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { |
| 51 switch (type) { | 53 switch (type) { |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 159 return; | 161 return; |
| 160 | 162 |
| 161 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel | 163 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel |
| 162 indexPathForItem:correspondingItem | 164 indexPathForItem:correspondingItem |
| 163 inSectionWithIdentifier:sectionIdentifier]; | 165 inSectionWithIdentifier:sectionIdentifier]; |
| 164 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; | 166 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; |
| 165 } | 167 } |
| 166 | 168 |
| 167 - (void)reloadAllData { | 169 - (void)reloadAllData { |
| 168 [self resetModels]; | 170 [self resetModels]; |
| 169 [self.collectionViewController | 171 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; |
| 170 addSuggestions:[self.dataSource allSuggestions]]; | 172 [self addSectionsForSuggestionsToModel:suggestions]; |
| 173 [self addSuggestionsToModel:suggestions]; | |
| 174 [self.collectionViewController.collectionView reloadData]; | |
|
gambard
2017/03/16 15:49:41
This is a bug fix, appearing only when you add hea
lpromero
2017/03/16 16:43:21
Can you explain more on the issue in a comment in
gambard
2017/03/17 09:33:39
Done.
| |
| 171 } | 175 } |
| 172 | 176 |
| 173 - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo { | 177 - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo { |
| 174 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); | 178 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| 175 NSInteger section = [self.collectionViewController.collectionViewModel | 179 NSInteger section = [self.collectionViewController.collectionViewModel |
| 176 sectionIdentifierForSection:sectionIdentifier]; | 180 sectionIdentifierForSection:sectionIdentifier]; |
| 177 | 181 |
| 178 [self.collectionViewController dismissSection:section]; | 182 [self.collectionViewController dismissSection:section]; |
| 179 } | 183 } |
| 180 | 184 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 241 ContentSuggestionsSectionInformation* sectionInfo = | 245 ContentSuggestionsSectionInformation* sectionInfo = |
| 242 suggestion.suggestionIdentifier.sectionInfo; | 246 suggestion.suggestionIdentifier.sectionInfo; |
| 243 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); | 247 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| 244 | 248 |
| 245 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { | 249 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { |
| 246 [model addSectionWithIdentifier:sectionIdentifier]; | 250 [model addSectionWithIdentifier:sectionIdentifier]; |
| 247 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; | 251 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; |
| 248 [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; | 252 [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; |
| 249 | 253 |
| 250 [self addFooterIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; | 254 [self addFooterIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; |
| 255 [self addHeader:suggestion.suggestionIdentifier.sectionInfo]; | |
|
lpromero
2017/03/16 16:43:21
Optional nit: move one line up?
gambard
2017/03/17 09:33:39
Done.
| |
| 251 } | 256 } |
| 252 } | 257 } |
| 253 return indexSet; | 258 return indexSet; |
| 254 } | 259 } |
| 255 | 260 |
| 256 #pragma mark - ContentSuggestionsArticleItemDelegate | 261 #pragma mark - ContentSuggestionsArticleItemDelegate |
| 257 | 262 |
| 258 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { | 263 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
| 259 NSInteger sectionIdentifier = | 264 NSInteger sectionIdentifier = |
| 260 SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo); | 265 SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 300 block:^{ | 305 block:^{ |
| 301 [weakSelf runAdditionalActionForSection:sectionInfo]; | 306 [weakSelf runAdditionalActionForSection:sectionInfo]; |
| 302 }]; | 307 }]; |
| 303 | 308 |
| 304 [self.collectionViewController.collectionViewModel | 309 [self.collectionViewController.collectionViewModel |
| 305 setFooter:footer | 310 setFooter:footer |
| 306 forSectionWithIdentifier:sectionIdentifier]; | 311 forSectionWithIdentifier:sectionIdentifier]; |
| 307 } | 312 } |
| 308 } | 313 } |
| 309 | 314 |
| 315 // Adds the header corresponding to |sectionInfo| to the section. | |
| 316 - (void)addHeader:(ContentSuggestionsSectionInformation*)sectionInfo { | |
| 317 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); | |
| 318 | |
| 319 if (![self.collectionViewController.collectionViewModel | |
| 320 headerForSectionWithIdentifier:sectionIdentifier]) { | |
| 321 ContentSuggestionsHeaderItem* header = | |
| 322 [[ContentSuggestionsHeaderItem alloc] initWithType:ItemTypeHeader]; | |
| 323 header.text = sectionInfo.title; | |
| 324 [self.collectionViewController.collectionViewModel | |
| 325 setHeader:header | |
| 326 forSectionWithIdentifier:sectionIdentifier]; | |
| 327 } | |
| 328 } | |
| 329 | |
| 310 // Resets the models, removing the current CollectionViewItem and the | 330 // Resets the models, removing the current CollectionViewItem and the |
| 311 // SectionInfo. | 331 // SectionInfo. |
| 312 - (void)resetModels { | 332 - (void)resetModels { |
| 313 [self.collectionViewController loadModel]; | 333 [self.collectionViewController loadModel]; |
| 314 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; | 334 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; |
| 315 } | 335 } |
| 316 | 336 |
| 317 // Runs the additional action for the section identified by |sectionInfo|. | 337 // Runs the additional action for the section identified by |sectionInfo|. |
| 318 - (void)runAdditionalActionForSection: | 338 - (void)runAdditionalActionForSection: |
| 319 (ContentSuggestionsSectionInformation*)sectionInfo { | 339 (ContentSuggestionsSectionInformation*)sectionInfo { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 339 }]; | 359 }]; |
| 340 } | 360 } |
| 341 | 361 |
| 342 // Adds the |suggestions| to the collection view. All the suggestions must have | 362 // Adds the |suggestions| to the collection view. All the suggestions must have |
| 343 // the same sectionInfo. | 363 // the same sectionInfo. |
| 344 - (void)moreSuggestionsFetched:(NSArray<ContentSuggestion*>*)suggestions { | 364 - (void)moreSuggestionsFetched:(NSArray<ContentSuggestion*>*)suggestions { |
| 345 [self.collectionViewController addSuggestions:suggestions]; | 365 [self.collectionViewController addSuggestions:suggestions]; |
| 346 } | 366 } |
| 347 | 367 |
| 348 @end | 368 @end |
| OLD | NEW |