| 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 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" | 9 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_id.h" | 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_id.h" |
| 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" | 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" |
| 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" | 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" |
| 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour
ce.h" | 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour
ce.h" |
| 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl
e_item.h" | 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl
e_item.h" |
| 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" |
| 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet
cher.h" |
| 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h" | 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h" |
| 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i
nformation.h" | 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i
nformation.h" |
| 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite
m.h" | 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite
m.h" |
| 21 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont
roller.h" | 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont
roller.h" |
| 23 #include "ui/gfx/image/image.h" |
| 22 #include "url/gurl.h" | 24 #include "url/gurl.h" |
| 23 | 25 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) | 26 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." | 27 #error "This file requires ARC support." |
| 26 #endif | 28 #endif |
| 27 | 29 |
| 28 namespace { | 30 namespace { |
| 29 | 31 |
| 30 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. | 32 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. |
| 31 typedef NS_ENUM(NSInteger, ItemType) { | 33 typedef NS_ENUM(NSInteger, ItemType) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 case ContentSuggestionsSectionArticles: | 70 case ContentSuggestionsSectionArticles: |
| 69 return SectionIdentifierArticles; | 71 return SectionIdentifierArticles; |
| 70 | 72 |
| 71 case ContentSuggestionsSectionUnknown: | 73 case ContentSuggestionsSectionUnknown: |
| 72 return SectionIdentifierDefault; | 74 return SectionIdentifierDefault; |
| 73 } | 75 } |
| 74 } | 76 } |
| 75 | 77 |
| 76 } // namespace | 78 } // namespace |
| 77 | 79 |
| 78 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink> | 80 @interface ContentSuggestionsCollectionUpdater ()< |
| 81 ContentSuggestionsArticleItemDelegate, |
| 82 ContentSuggestionsDataSink> |
| 79 | 83 |
| 80 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; | 84 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; |
| 81 @property(nonatomic, strong) | 85 @property(nonatomic, strong) |
| 82 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* | 86 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* |
| 83 sectionInfoBySectionIdentifier; | 87 sectionInfoBySectionIdentifier; |
| 84 | 88 |
| 85 // Reloads all the data from the data source, deleting all the current items. | 89 // Reloads all the data from the data source, deleting all the current items. |
| 86 - (void)reloadData; | 90 - (void)reloadData; |
| 87 // Adds a new section if needed and returns the section identifier. | 91 // Adds a new section if needed and returns the section identifier. |
| 88 - (NSInteger)addSectionIfNeeded: | 92 - (NSInteger)addSectionIfNeeded: |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 ContentSuggestionsSectionInformation* sectionInformation = | 172 ContentSuggestionsSectionInformation* sectionInformation = |
| 169 self.sectionInfoBySectionIdentifier[identifier]; | 173 self.sectionInfoBySectionIdentifier[identifier]; |
| 170 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom; | 174 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom; |
| 171 } | 175 } |
| 172 | 176 |
| 173 - (ContentSuggestionType)contentSuggestionTypeForItem: | 177 - (ContentSuggestionType)contentSuggestionTypeForItem: |
| 174 (CollectionViewItem*)item { | 178 (CollectionViewItem*)item { |
| 175 return ContentSuggestionTypeForItemType(item.type); | 179 return ContentSuggestionTypeForItemType(item.type); |
| 176 } | 180 } |
| 177 | 181 |
| 182 #pragma mark - ContentSuggestionsArticleItemDelegate |
| 183 |
| 184 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
| 185 NSInteger sectionIdentifier = |
| 186 SectionIdentifierForInfo(articleItem.suggestionID.sectionInfo); |
| 187 |
| 188 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 189 __weak ContentSuggestionsArticleItem* weakArticle = articleItem; |
| 190 void (^imageFetchedCallback)(const gfx::Image&) = ^(const gfx::Image& image) { |
| 191 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; |
| 192 ContentSuggestionsArticleItem* strongArticle = weakArticle; |
| 193 if (!strongSelf || !strongArticle) { |
| 194 return; |
| 195 } |
| 196 |
| 197 strongArticle.imageBeingFetched = NO; |
| 198 strongArticle.image = image.CopyUIImage(); |
| 199 [strongSelf.collectionViewController |
| 200 reconfigureCellsForItems:@[ strongArticle ] |
| 201 inSectionWithIdentifier:sectionIdentifier]; |
| 202 }; |
| 203 |
| 204 [self.dataSource.imageFetcher fetchImageForSuggestion:articleItem.suggestionID |
| 205 callback:imageFetchedCallback]; |
| 206 } |
| 207 |
| 178 #pragma mark - Private methods | 208 #pragma mark - Private methods |
| 179 | 209 |
| 180 - (void)reloadData { | 210 - (void)reloadData { |
| 181 [self resetModels]; | 211 [self resetModels]; |
| 182 CollectionViewModel* model = | 212 CollectionViewModel* model = |
| 183 self.collectionViewController.collectionViewModel; | 213 self.collectionViewController.collectionViewModel; |
| 184 | 214 |
| 185 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; | 215 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; |
| 186 | 216 |
| 187 for (ContentSuggestion* suggestion in suggestions) { | 217 for (ContentSuggestion* suggestion in suggestions) { |
| 188 NSInteger sectionIdentifier = | 218 NSInteger sectionIdentifier = |
| 189 [self addSectionIfNeeded:suggestion.suggestionID.sectionInfo]; | 219 [self addSectionIfNeeded:suggestion.suggestionID.sectionInfo]; |
| 190 ContentSuggestionsArticleItem* articleItem = | 220 ContentSuggestionsArticleItem* articleItem = |
| 191 [[ContentSuggestionsArticleItem alloc] | 221 [[ContentSuggestionsArticleItem alloc] |
| 192 initWithType:ItemTypeForContentSuggestionType(suggestion.type) | 222 initWithType:ItemTypeForContentSuggestionType(suggestion.type) |
| 193 title:suggestion.title | 223 title:suggestion.title |
| 194 subtitle:suggestion.text | 224 subtitle:suggestion.text |
| 195 image:suggestion.image | 225 delegate:self |
| 196 url:suggestion.url]; | 226 url:suggestion.url]; |
| 197 articleItem.suggestionID = suggestion.suggestionID; | 227 articleItem.suggestionID = suggestion.suggestionID; |
| 198 | 228 |
| 199 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier]; | 229 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier]; |
| 200 } | 230 } |
| 201 | 231 |
| 202 if ([self.collectionViewController isViewLoaded]) { | 232 if ([self.collectionViewController isViewLoaded]) { |
| 203 [self.collectionViewController.collectionView reloadData]; | 233 [self.collectionViewController.collectionView reloadData]; |
| 204 } | 234 } |
| 205 } | 235 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 219 } | 249 } |
| 220 return sectionIdentifier; | 250 return sectionIdentifier; |
| 221 } | 251 } |
| 222 | 252 |
| 223 - (void)resetModels { | 253 - (void)resetModels { |
| 224 [self.collectionViewController loadModel]; | 254 [self.collectionViewController loadModel]; |
| 225 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; | 255 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; |
| 226 } | 256 } |
| 227 | 257 |
| 228 @end | 258 @end |
| OLD | NEW |