| 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_image_fet
cher.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_text_item
.h" | 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_text_item
.h" |
| 22 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont
roller.h" | 23 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont
roller.h" |
| 24 #include "ui/gfx/image/image.h" |
| 23 #include "url/gurl.h" | 25 #include "url/gurl.h" |
| 24 | 26 |
| 25 #if !defined(__has_feature) || !__has_feature(objc_arc) | 27 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 26 #error "This file requires ARC support." | 28 #error "This file requires ARC support." |
| 27 #endif | 29 #endif |
| 28 | 30 |
| 29 namespace { | 31 namespace { |
| 30 | 32 |
| 31 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. | 33 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. |
| 32 typedef NS_ENUM(NSInteger, ItemType) { | 34 typedef NS_ENUM(NSInteger, ItemType) { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 69 case ContentSuggestionsSectionArticles: | 71 case ContentSuggestionsSectionArticles: |
| 70 return SectionIdentifierArticles; | 72 return SectionIdentifierArticles; |
| 71 | 73 |
| 72 case ContentSuggestionsSectionUnknown: | 74 case ContentSuggestionsSectionUnknown: |
| 73 return SectionIdentifierDefault; | 75 return SectionIdentifierDefault; |
| 74 } | 76 } |
| 75 } | 77 } |
| 76 | 78 |
| 77 } // namespace | 79 } // namespace |
| 78 | 80 |
| 79 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink> | 81 @interface ContentSuggestionsCollectionUpdater ()< |
| 82 ContentSuggestionsArticleItemDelegate, |
| 83 ContentSuggestionsDataSink> |
| 80 | 84 |
| 81 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; | 85 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; |
| 82 @property(nonatomic, strong) | 86 @property(nonatomic, strong) |
| 83 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* | 87 NSMutableDictionary<NSNumber*, ContentSuggestionsSectionInformation*>* |
| 84 sectionInfoBySectionIdentifier; | 88 sectionInfoBySectionIdentifier; |
| 85 | 89 |
| 86 // Reloads all the data from the data source, deleting all the current items. | 90 // Reloads all the data from the data source, deleting all the current items. |
| 87 - (void)reloadData; | 91 - (void)reloadData; |
| 88 // Adds a new section if needed and returns the section identifier. | 92 // Adds a new section if needed and returns the section identifier. |
| 89 - (NSInteger)addSectionIfNeeded: | 93 - (NSInteger)addSectionIfNeeded: |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 ContentSuggestionsSectionInformation* sectionInformation = | 137 ContentSuggestionsSectionInformation* sectionInformation = |
| 134 self.sectionInfoBySectionIdentifier[identifier]; | 138 self.sectionInfoBySectionIdentifier[identifier]; |
| 135 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom; | 139 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom; |
| 136 } | 140 } |
| 137 | 141 |
| 138 - (ContentSuggestionType)contentSuggestionTypeForItem: | 142 - (ContentSuggestionType)contentSuggestionTypeForItem: |
| 139 (CollectionViewItem*)item { | 143 (CollectionViewItem*)item { |
| 140 return ContentSuggestionTypeForItemType(item.type); | 144 return ContentSuggestionTypeForItemType(item.type); |
| 141 } | 145 } |
| 142 | 146 |
| 147 #pragma mark - ContentSuggestionsArticleItemDelegate |
| 148 |
| 149 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
| 150 NSInteger sectionIdentifier = |
| 151 SectionIdentifierForInfo(articleItem.suggestionIdentifier.sectionInfo); |
| 152 |
| 153 __weak ContentSuggestionsCollectionUpdater* weakSelf = self; |
| 154 __weak ContentSuggestionsArticleItem* weakArticle = articleItem; |
| 155 void (^imageFetchedCallback)(const gfx::Image&) = ^(const gfx::Image& image) { |
| 156 ContentSuggestionsCollectionUpdater* strongSelf = weakSelf; |
| 157 ContentSuggestionsArticleItem* strongArticle = weakArticle; |
| 158 if (!strongSelf || !strongArticle) { |
| 159 return; |
| 160 } |
| 161 |
| 162 strongArticle.imageBeingFetched = NO; |
| 163 strongArticle.image = image.CopyUIImage(); |
| 164 [strongSelf.collectionViewController |
| 165 reconfigureCellsForItems:@[ strongArticle ] |
| 166 inSectionWithIdentifier:sectionIdentifier]; |
| 167 }; |
| 168 |
| 169 articleItem.imageBeingFetched = YES; |
| 170 |
| 171 [self.dataSource.imageFetcher |
| 172 fetchImageForSuggestion:articleItem.suggestionIdentifier |
| 173 callback:imageFetchedCallback]; |
| 174 } |
| 175 |
| 143 #pragma mark - Private methods | 176 #pragma mark - Private methods |
| 144 | 177 |
| 145 - (void)reloadData { | 178 - (void)reloadData { |
| 146 [self resetModels]; | 179 [self resetModels]; |
| 147 CollectionViewModel* model = | 180 CollectionViewModel* model = |
| 148 self.collectionViewController.collectionViewModel; | 181 self.collectionViewController.collectionViewModel; |
| 149 | 182 |
| 150 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; | 183 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; |
| 151 | 184 |
| 152 for (ContentSuggestion* suggestion in suggestions) { | 185 for (ContentSuggestion* suggestion in suggestions) { |
| 153 NSInteger sectionIdentifier = | 186 NSInteger sectionIdentifier = |
| 154 [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; | 187 [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; |
| 155 ContentSuggestionsArticleItem* articleItem = | 188 ContentSuggestionsArticleItem* articleItem = |
| 156 [[ContentSuggestionsArticleItem alloc] | 189 [[ContentSuggestionsArticleItem alloc] |
| 157 initWithType:ItemTypeForContentSuggestionType(suggestion.type) | 190 initWithType:ItemTypeForContentSuggestionType(suggestion.type) |
| 158 title:suggestion.title | 191 title:suggestion.title |
| 159 subtitle:suggestion.text | 192 subtitle:suggestion.text |
| 160 image:suggestion.image | 193 delegate:self |
| 161 url:suggestion.url]; | 194 url:suggestion.url]; |
| 162 | 195 |
| 163 articleItem.publisher = suggestion.publisher; | 196 articleItem.publisher = suggestion.publisher; |
| 164 articleItem.publishDate = suggestion.publishDate; | 197 articleItem.publishDate = suggestion.publishDate; |
| 165 | 198 |
| 166 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; | 199 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier; |
| 167 | 200 |
| 168 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier]; | 201 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier]; |
| 169 } | 202 } |
| 170 | 203 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 188 } | 221 } |
| 189 return sectionIdentifier; | 222 return sectionIdentifier; |
| 190 } | 223 } |
| 191 | 224 |
| 192 - (void)resetModels { | 225 - (void)resetModels { |
| 193 [self.collectionViewController loadModel]; | 226 [self.collectionViewController loadModel]; |
| 194 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; | 227 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; |
| 195 } | 228 } |
| 196 | 229 |
| 197 @end | 230 @end |
| OLD | NEW |