| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/content_suggestions/content_suggestions_mediator.h" | 5 #import "ios/chrome/browser/content_suggestions/content_suggestions_mediator.h" |
| 6 | 6 |
| 7 #include "base/mac/bind_objc_block.h" | 7 #include "base/mac/bind_objc_block.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/optional.h" | 9 #include "base/optional.h" |
| 10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 106 } | 106 } |
| 107 | 107 |
| 108 @property(nonatomic, assign) | 108 @property(nonatomic, assign) |
| 109 ntp_snippets::ContentSuggestionsService* contentService; | 109 ntp_snippets::ContentSuggestionsService* contentService; |
| 110 @property(nonatomic, strong, nonnull) | 110 @property(nonatomic, strong, nonnull) |
| 111 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 111 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| 112 ContentSuggestionsSectionInformation*>* | 112 ContentSuggestionsSectionInformation*>* |
| 113 sectionInformationByCategory; | 113 sectionInformationByCategory; |
| 114 | 114 |
| 115 // Converts the data in |category| to ContentSuggestion and adds them to the | 115 // Converts the data in |category| to ContentSuggestion and adds them to the |
| 116 // |contentArray|. | 116 // |contentArray| if the category is available. |
| 117 - (void)addContentInCategory:(ntp_snippets::Category&)category | 117 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 118 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 118 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 119 | 119 |
| 120 // Adds the section information for |category| in | 120 // Adds the section information for |category| in |
| 121 // self.sectionInformationByCategory. | 121 // self.sectionInformationByCategory. |
| 122 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; | 122 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| 123 | 123 |
| 124 // Returns a CategoryWrapper acting as a key for this section info. | 124 // Returns a CategoryWrapper acting as a key for this section info. |
| 125 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: | 125 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: |
| 126 (ContentSuggestionsSectionInformation*)sectionInfo; | 126 (ContentSuggestionsSectionInformation*)sectionInfo; |
| (...skipping 30 matching lines...) Expand all Loading... |
| 157 self.contentService->DismissSuggestion(suggestion_id); | 157 self.contentService->DismissSuggestion(suggestion_id); |
| 158 } | 158 } |
| 159 | 159 |
| 160 #pragma mark - ContentSuggestionsDataSource | 160 #pragma mark - ContentSuggestionsDataSource |
| 161 | 161 |
| 162 - (NSArray<ContentSuggestion*>*)allSuggestions { | 162 - (NSArray<ContentSuggestion*>*)allSuggestions { |
| 163 std::vector<ntp_snippets::Category> categories = | 163 std::vector<ntp_snippets::Category> categories = |
| 164 self.contentService->GetCategories(); | 164 self.contentService->GetCategories(); |
| 165 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; | 165 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; |
| 166 for (auto& category : categories) { | 166 for (auto& category : categories) { |
| 167 if (self.contentService->GetCategoryStatus(category) != | |
| 168 ntp_snippets::CategoryStatus::AVAILABLE) { | |
| 169 continue; | |
| 170 } | |
| 171 if (!self.sectionInformationByCategory[ | |
| 172 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { | |
| 173 [self addSectionInformationForCategory:category]; | |
| 174 } | |
| 175 [self addContentInCategory:category toArray:dataHolders]; | 167 [self addContentInCategory:category toArray:dataHolders]; |
| 176 } | 168 } |
| 177 return dataHolders; | 169 return dataHolders; |
| 178 } | 170 } |
| 179 | 171 |
| 172 - (NSArray<ContentSuggestion*>*)suggestionsForSection: |
| 173 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 174 ntp_snippets::Category category = |
| 175 [[self categoryWrapperForSectionInfo:sectionInfo] category]; |
| 176 |
| 177 NSMutableArray* suggestions = [NSMutableArray array]; |
| 178 [self addContentInCategory:category toArray:suggestions]; |
| 179 return suggestions; |
| 180 } |
| 181 |
| 180 - (id<ContentSuggestionsImageFetcher>)imageFetcher { | 182 - (id<ContentSuggestionsImageFetcher>)imageFetcher { |
| 181 return self; | 183 return self; |
| 182 } | 184 } |
| 183 | 185 |
| 184 #pragma mark - ContentSuggestionsServiceObserver | 186 #pragma mark - ContentSuggestionsServiceObserver |
| 185 | 187 |
| 186 - (void)contentSuggestionsService: | 188 - (void)contentSuggestionsService: |
| 187 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 189 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 188 newSuggestionsInCategory:(ntp_snippets::Category)category { | 190 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| 189 [self.dataSink dataAvailable]; | 191 ContentSuggestionsCategoryWrapper* wrapper = |
| 192 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; |
| 193 if (!self.sectionInformationByCategory[wrapper]) { |
| 194 [self addSectionInformationForCategory:category]; |
| 195 } |
| 196 [self.dataSink |
| 197 dataAvailableForSection:self.sectionInformationByCategory[wrapper]]; |
| 190 } | 198 } |
| 191 | 199 |
| 192 - (void)contentSuggestionsService: | 200 - (void)contentSuggestionsService: |
| 193 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 201 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 194 category:(ntp_snippets::Category)category | 202 category:(ntp_snippets::Category)category |
| 195 statusChangedTo:(ntp_snippets::CategoryStatus)status { | 203 statusChangedTo:(ntp_snippets::CategoryStatus)status { |
| 196 if (!ntp_snippets::IsCategoryStatusInitOrAvailable(status)) { | 204 if (!ntp_snippets::IsCategoryStatusInitOrAvailable(status)) { |
| 197 // Remove the category from the UI if it is not available. | 205 // Remove the category from the UI if it is not available. |
| 198 ContentSuggestionsCategoryWrapper* wrapper = | 206 ContentSuggestionsCategoryWrapper* wrapper = |
| 199 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; | 207 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 239 SuggestionIDForSectionID( | 247 SuggestionIDForSectionID( |
| 240 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo], | 248 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo], |
| 241 suggestionIdentifier.IDInSection), | 249 suggestionIdentifier.IDInSection), |
| 242 base::BindBlockArc(callback)); | 250 base::BindBlockArc(callback)); |
| 243 } | 251 } |
| 244 | 252 |
| 245 #pragma mark - Private | 253 #pragma mark - Private |
| 246 | 254 |
| 247 - (void)addContentInCategory:(ntp_snippets::Category&)category | 255 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 248 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { | 256 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| 257 if (self.contentService->GetCategoryStatus(category) != |
| 258 ntp_snippets::CategoryStatus::AVAILABLE) { |
| 259 return; |
| 260 } |
| 261 ContentSuggestionsCategoryWrapper* categoryWrapper = |
| 262 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; |
| 263 if (!self.sectionInformationByCategory[categoryWrapper]) { |
| 264 [self addSectionInformationForCategory:category]; |
| 265 } |
| 266 |
| 249 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = | 267 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| 250 self.contentService->GetSuggestionsForCategory(category); | 268 self.contentService->GetSuggestionsForCategory(category); |
| 251 ContentSuggestionsCategoryWrapper* categoryWrapper = | 269 |
| 252 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; | |
| 253 for (auto& contentSuggestion : suggestions) { | 270 for (auto& contentSuggestion : suggestions) { |
| 254 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); | 271 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
| 272 |
| 255 suggestion.type = TypeForCategory(category); | 273 suggestion.type = TypeForCategory(category); |
| 256 suggestion.suggestionIdentifier.sectionInfo = | 274 suggestion.suggestionIdentifier.sectionInfo = |
| 257 self.sectionInformationByCategory[categoryWrapper]; | 275 self.sectionInformationByCategory[categoryWrapper]; |
| 258 | 276 |
| 259 [contentArray addObject:suggestion]; | 277 [contentArray addObject:suggestion]; |
| 260 } | 278 } |
| 261 } | 279 } |
| 262 | 280 |
| 263 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { | 281 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { |
| 264 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = | 282 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = |
| 265 self.contentService->GetCategoryInfo(category); | 283 self.contentService->GetCategoryInfo(category); |
| 266 | 284 |
| 267 ContentSuggestionsSectionInformation* sectionInfo = | 285 ContentSuggestionsSectionInformation* sectionInfo = |
| 268 SectionInformationFromCategoryInfo(categoryInfo, category); | 286 SectionInformationFromCategoryInfo(categoryInfo, category); |
| 269 | 287 |
| 270 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper | 288 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper |
| 271 wrapperWithCategory:category]] = sectionInfo; | 289 wrapperWithCategory:category]] = sectionInfo; |
| 272 } | 290 } |
| 273 | 291 |
| 274 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: | 292 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: |
| 275 (ContentSuggestionsSectionInformation*)sectionInfo { | 293 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 276 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo] | 294 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo] |
| 277 firstObject]; | 295 firstObject]; |
| 278 } | 296 } |
| 279 | 297 |
| 280 @end | 298 @end |
| OLD | NEW |