| 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" |
| 11 #include "components/ntp_snippets/category.h" | 11 #include "components/ntp_snippets/category.h" |
| 12 #include "components/ntp_snippets/content_suggestion.h" | 12 #include "components/ntp_snippets/content_suggestion.h" |
| 13 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra
pper.h" | 13 #import "ios/chrome/browser/content_suggestions/content_suggestions_category_wra
pper.h" |
| 14 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid
ge_observer.h" | 14 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid
ge_observer.h" |
| 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier
.h" | 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier
.h" |
| 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.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_section_i
nformation.h" | 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i
nformation.h" |
| 19 #include "ui/gfx/image/image.h" | 20 #include "ui/gfx/image/image.h" |
| 20 | 21 |
| 21 #if !defined(__has_feature) || !__has_feature(objc_arc) | 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 22 #error "This file requires ARC support." | 23 #error "This file requires ARC support." |
| 23 #endif | 24 #endif |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 // Returns the Type for this |category|. | 28 // Returns the Type for this |category|. |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 81 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | 82 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); |
| 82 if (categoryInfo->show_if_empty()) { | 83 if (categoryInfo->show_if_empty()) { |
| 83 // TODO(crbug.com/686728): Creates an item to display information when the | 84 // TODO(crbug.com/686728): Creates an item to display information when the |
| 84 // section is empty. | 85 // section is empty. |
| 85 } | 86 } |
| 86 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | 87 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); |
| 87 } | 88 } |
| 88 return sectionInfo; | 89 return sectionInfo; |
| 89 } | 90 } |
| 90 | 91 |
| 92 // Returns a ntp_snippets::ID based on a Objective-C Category and the ID in the |
| 93 // category. |
| 94 ntp_snippets::ContentSuggestion::ID SuggestionIDForSectionID( |
| 95 ContentSuggestionsCategoryWrapper* category, |
| 96 const std::string& id_in_category) { |
| 97 return ntp_snippets::ContentSuggestion::ID(category.category, id_in_category); |
| 98 } |
| 99 |
| 91 } // namespace | 100 } // namespace |
| 92 | 101 |
| 93 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { | 102 @interface ContentSuggestionsMediator ()<ContentSuggestionsImageFetcher, |
| 103 ContentSuggestionsServiceObserver> { |
| 94 // Bridge for this class to become an observer of a ContentSuggestionsService. | 104 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 95 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 105 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 96 } | 106 } |
| 97 | 107 |
| 98 @property(nonatomic, assign) | 108 @property(nonatomic, assign) |
| 99 ntp_snippets::ContentSuggestionsService* contentService; | 109 ntp_snippets::ContentSuggestionsService* contentService; |
| 100 @property(nonatomic, strong, nonnull) | 110 @property(nonatomic, strong, nonnull) |
| 101 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | 111 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| 102 ContentSuggestionsSectionInformation*>* | 112 ContentSuggestionsSectionInformation*>* |
| 103 sectionInformationByCategory; | 113 sectionInformationByCategory; |
| 104 | 114 |
| 105 // 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 |
| 106 // |contentArray|. | 116 // |contentArray|. |
| 107 - (void)addContentInCategory:(ntp_snippets::Category&)category | 117 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 108 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 118 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 109 | 119 |
| 110 // Adds the section information for |category| in | 120 // Adds the section information for |category| in |
| 111 // self.sectionInformationByCategory. | 121 // self.sectionInformationByCategory. |
| 112 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; | 122 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| 113 | 123 |
| 124 // Returns a CategoryWrapper acting as a key for this section info. |
| 125 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: |
| 126 (ContentSuggestionsSectionInformation*)sectionInfo; |
| 127 |
| 114 @end | 128 @end |
| 115 | 129 |
| 116 @implementation ContentSuggestionsMediator | 130 @implementation ContentSuggestionsMediator |
| 117 | 131 |
| 118 @synthesize contentService = _contentService; | 132 @synthesize contentService = _contentService; |
| 119 @synthesize dataSink = _dataSink; | 133 @synthesize dataSink = _dataSink; |
| 120 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | 134 @synthesize sectionInformationByCategory = _sectionInformationByCategory; |
| 121 | 135 |
| 122 - (instancetype)initWithContentService: | 136 - (instancetype)initWithContentService: |
| 123 (ntp_snippets::ContentSuggestionsService*)contentService { | 137 (ntp_snippets::ContentSuggestionsService*)contentService { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 144 } | 158 } |
| 145 if (!self.sectionInformationByCategory[ | 159 if (!self.sectionInformationByCategory[ |
| 146 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { | 160 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { |
| 147 [self addSectionInformationForCategory:category]; | 161 [self addSectionInformationForCategory:category]; |
| 148 } | 162 } |
| 149 [self addContentInCategory:category toArray:dataHolders]; | 163 [self addContentInCategory:category toArray:dataHolders]; |
| 150 } | 164 } |
| 151 return dataHolders; | 165 return dataHolders; |
| 152 } | 166 } |
| 153 | 167 |
| 168 - (id<ContentSuggestionsImageFetcher>)imageFetcher { |
| 169 return self; |
| 170 } |
| 171 |
| 154 #pragma mark - ContentSuggestionsServiceObserver | 172 #pragma mark - ContentSuggestionsServiceObserver |
| 155 | 173 |
| 156 - (void)contentSuggestionsService: | 174 - (void)contentSuggestionsService: |
| 157 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 175 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 158 newSuggestionsInCategory:(ntp_snippets::Category)category { | 176 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| 159 [self.dataSink dataAvailable]; | 177 [self.dataSink dataAvailable]; |
| 160 } | 178 } |
| 161 | 179 |
| 162 - (void)contentSuggestionsService: | 180 - (void)contentSuggestionsService: |
| 163 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 181 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| (...skipping 12 matching lines...) Expand all Loading... |
| 176 - (void)contentSuggestionsServiceFullRefreshRequired: | 194 - (void)contentSuggestionsServiceFullRefreshRequired: |
| 177 (ntp_snippets::ContentSuggestionsService*)suggestionsService { | 195 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 178 // Update dataSink. | 196 // Update dataSink. |
| 179 } | 197 } |
| 180 | 198 |
| 181 - (void)contentSuggestionsServiceShutdown: | 199 - (void)contentSuggestionsServiceShutdown: |
| 182 (ntp_snippets::ContentSuggestionsService*)suggestionsService { | 200 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 183 // Update dataSink. | 201 // Update dataSink. |
| 184 } | 202 } |
| 185 | 203 |
| 204 #pragma mark - ContentSuggestionsImageFetcher |
| 205 |
| 206 - (void)fetchImageForSuggestion: |
| 207 (ContentSuggestionIdentifier*)suggestionIdentifier |
| 208 callback:(void (^)(const gfx::Image&))callback { |
| 209 self.contentService->FetchSuggestionImage( |
| 210 SuggestionIDForSectionID( |
| 211 [self categoryWrapperForSectionInfo:suggestionIdentifier.sectionInfo], |
| 212 suggestionIdentifier.IDInSection), |
| 213 base::BindBlockArc(callback)); |
| 214 } |
| 215 |
| 186 #pragma mark - Private | 216 #pragma mark - Private |
| 187 | 217 |
| 188 - (void)addContentInCategory:(ntp_snippets::Category&)category | 218 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 189 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { | 219 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| 190 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = | 220 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| 191 self.contentService->GetSuggestionsForCategory(category); | 221 self.contentService->GetSuggestionsForCategory(category); |
| 192 ContentSuggestionsCategoryWrapper* categoryWrapper = | 222 ContentSuggestionsCategoryWrapper* categoryWrapper = |
| 193 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; | 223 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; |
| 194 for (auto& contentSuggestion : suggestions) { | 224 for (auto& contentSuggestion : suggestions) { |
| 195 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); | 225 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
| 196 suggestion.type = TypeForCategory(category); | 226 suggestion.type = TypeForCategory(category); |
| 197 suggestion.suggestionIdentifier.sectionInfo = | 227 suggestion.suggestionIdentifier.sectionInfo = |
| 198 self.sectionInformationByCategory[categoryWrapper]; | 228 self.sectionInformationByCategory[categoryWrapper]; |
| 199 | 229 |
| 200 // TODO(crbug.com/686728): fetch the image. | |
| 201 | |
| 202 [contentArray addObject:suggestion]; | 230 [contentArray addObject:suggestion]; |
| 203 } | 231 } |
| 204 } | 232 } |
| 205 | 233 |
| 206 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { | 234 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { |
| 207 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = | 235 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = |
| 208 self.contentService->GetCategoryInfo(category); | 236 self.contentService->GetCategoryInfo(category); |
| 209 | 237 |
| 210 ContentSuggestionsSectionInformation* sectionInfo = | 238 ContentSuggestionsSectionInformation* sectionInfo = |
| 211 SectionInformationFromCategoryInfo(categoryInfo, category); | 239 SectionInformationFromCategoryInfo(categoryInfo, category); |
| 212 | 240 |
| 213 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper | 241 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper |
| 214 wrapperWithCategory:category]] = sectionInfo; | 242 wrapperWithCategory:category]] = sectionInfo; |
| 215 } | 243 } |
| 216 | 244 |
| 245 - (ContentSuggestionsCategoryWrapper*)categoryWrapperForSectionInfo: |
| 246 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 247 return [[self.sectionInformationByCategory allKeysForObject:sectionInfo] |
| 248 firstObject]; |
| 249 } |
| 250 |
| 217 @end | 251 @end |
| OLD | NEW |