Chromium Code Reviews| 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/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/optional.h" | |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 8 #include "components/ntp_snippets/category.h" | 11 #include "components/ntp_snippets/category.h" |
| 9 #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" | |
| 10 #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" |
| 11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" | 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" |
| 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_upd ater.h" | |
| 18 #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 #include "ui/gfx/image/image.h" | |
| 13 | 21 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 22 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." | 23 #error "This file requires ARC support." |
| 16 #endif | 24 #endif |
| 17 | 25 |
| 26 namespace { | |
| 27 | |
| 28 // Returns the ItemType for this |category|. | |
| 29 ContentSuggestionsItemType TypeForCategory(ntp_snippets::Category category) { | |
| 30 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | |
| 31 return ContentSuggestionsItemTypeArticle; | |
| 32 return ContentSuggestionsItemTypeText; | |
| 33 } | |
| 34 | |
| 35 // Returns the section ID for this |category|. | |
| 36 ContentSuggestionsSectionID SectionIDForCategory( | |
| 37 ntp_snippets::Category category) { | |
| 38 if (category.IsKnownCategory(ntp_snippets::KnownCategories::BOOKMARKS)) | |
| 39 return ContentSuggestionsSectionBookmarks; | |
| 40 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | |
| 41 return ContentSuggestionsSectionArticles; | |
| 42 | |
| 43 return ContentSuggestionsSectionCount; | |
| 44 } | |
| 45 | |
| 46 // Returns the section layout corresponding to the category |layout|. | |
| 47 ContentSuggestionsSectionLayout SectionLayoutForLayout( | |
| 48 ntp_snippets::ContentSuggestionsCardLayout layout) { | |
| 49 // For now, only cards are relevant. | |
| 50 return ContentSuggestionsSectionLayoutCard; | |
| 51 } | |
| 52 | |
| 53 } // namespace | |
| 54 | |
| 18 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { | 55 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { |
| 19 // Bridge for this class to become an observer of a ContentSuggestionsService. | 56 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 20 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 57 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 21 } | 58 } |
| 22 | 59 |
| 23 @property(nonatomic, assign) | 60 @property(nonatomic, assign) |
| 24 ntp_snippets::ContentSuggestionsService* contentService; | 61 ntp_snippets::ContentSuggestionsService* contentService; |
| 62 @property(nonatomic, strong) NSMutableDictionary* sectionInformationByCategory; | |
|
stkhapugin
2017/02/14 16:34:18
Needs comment. What are the keys, objects and how
gambard
2017/02/15 10:17:56
Done.
| |
| 25 | 63 |
| 26 // Converts the data in |category| to ContentSuggestion and adds them to the | 64 // Converts the data in |category| to ContentSuggestion and adds them to the |
| 27 // |contentArray|. | 65 // |contentArray|. |
| 28 - (void)addContentInCategory:(ntp_snippets::Category&)category | 66 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 29 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 67 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 30 | 68 |
| 31 // Converts the |contentsuggestion| to a ContentSuggestion. | 69 // Converts the |contentsuggestion| to a ContentSuggestion. |
|
stkhapugin
2017/02/14 16:34:18
Update the arg name with same capitalization.
gambard
2017/02/15 10:17:56
Done.
| |
| 32 - (ContentSuggestion*)convertContentSuggestion: | 70 - (ContentSuggestion*)convertContentSuggestion: |
| 33 (const ntp_snippets::ContentSuggestion&)contentsuggestion; | 71 (const ntp_snippets::ContentSuggestion&)contentSuggestion; |
| 72 | |
| 73 // Adds the section information for |category| in | |
| 74 // self.sectionInformationByCategory. | |
| 75 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; | |
| 34 | 76 |
| 35 @end | 77 @end |
| 36 | 78 |
| 37 @implementation ContentSuggestionsMediator | 79 @implementation ContentSuggestionsMediator |
| 38 | 80 |
| 39 @synthesize contentService = _contentService; | 81 @synthesize contentService = _contentService; |
| 40 @synthesize dataSink = _dataSink; | 82 @synthesize dataSink = _dataSink; |
| 83 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | |
|
stkhapugin
2017/02/14 16:34:18
Is it necessary? I keep forgetting :(
gambard
2017/02/15 10:17:56
Seems so :)
marq (ping after 24h)
2017/02/16 13:14:35
We have not yet turned on property autosynthesis.
| |
| 41 | 84 |
| 42 - (instancetype)initWithContentService: | 85 - (instancetype)initWithContentService: |
| 43 (ntp_snippets::ContentSuggestionsService*)contentService { | 86 (ntp_snippets::ContentSuggestionsService*)contentService { |
| 44 self = [super init]; | 87 self = [super init]; |
| 45 if (self) { | 88 if (self) { |
| 46 _suggestionBridge = | 89 _suggestionBridge = |
| 47 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); | 90 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); |
| 48 _contentService = contentService; | 91 _contentService = contentService; |
| 92 _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; | |
| 49 } | 93 } |
| 50 return self; | 94 return self; |
| 51 } | 95 } |
| 52 | 96 |
| 53 #pragma mark - ContentSuggestionsDataSource | 97 #pragma mark - ContentSuggestionsDataSource |
| 54 | 98 |
| 55 - (NSArray<ContentSuggestion*>*)allSuggestions { | 99 - (NSArray<ContentSuggestion*>*)allSuggestions { |
| 56 std::vector<ntp_snippets::Category> categories = | 100 std::vector<ntp_snippets::Category> categories = |
| 57 self.contentService->GetCategories(); | 101 self.contentService->GetCategories(); |
| 58 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; | 102 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; |
| 59 for (auto& category : categories) { | 103 for (auto& category : categories) { |
| 104 if (self.contentService->GetCategoryStatus(category) != | |
| 105 ntp_snippets::CategoryStatus::AVAILABLE) { | |
| 106 continue; | |
| 107 } | |
| 108 if (![self.sectionInformationByCategory | |
|
stkhapugin
2017/02/14 16:34:18
Use modern syntax: if (!self.sectionInformationByC
gambard
2017/02/15 10:17:56
Done.
| |
| 109 objectForKey:[[ContentSuggestionsCategoryWrapper alloc] | |
| 110 initWithCategory:category]]) { | |
| 111 [self addSectionInformationForCategory:category]; | |
| 112 } | |
| 60 [self addContentInCategory:category toArray:dataHolders]; | 113 [self addContentInCategory:category toArray:dataHolders]; |
| 61 } | 114 } |
| 62 return dataHolders; | 115 return dataHolders; |
| 63 } | 116 } |
| 64 | 117 |
| 65 #pragma mark - ContentSuggestionsServiceObserver | 118 #pragma mark - ContentSuggestionsServiceObserver |
| 66 | 119 |
| 67 - (void)contentSuggestionsService: | 120 - (void)contentSuggestionsService: |
| 68 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 121 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 69 newSuggestionsInCategory:(ntp_snippets::Category)category { | 122 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 93 (ntp_snippets::ContentSuggestionsService*)suggestionsService { | 146 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 94 // Update dataSink. | 147 // Update dataSink. |
| 95 } | 148 } |
| 96 | 149 |
| 97 #pragma mark - Private | 150 #pragma mark - Private |
| 98 | 151 |
| 99 - (void)addContentInCategory:(ntp_snippets::Category&)category | 152 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 100 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { | 153 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| 101 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = | 154 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| 102 self.contentService->GetSuggestionsForCategory(category); | 155 self.contentService->GetSuggestionsForCategory(category); |
| 156 ContentSuggestionsCategoryWrapper* categoryWrapper = | |
| 157 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; | |
| 103 for (auto& contentSuggestion : suggestions) { | 158 for (auto& contentSuggestion : suggestions) { |
| 104 [contentArray addObject:[self convertContentSuggestion:contentSuggestion]]; | 159 ContentSuggestion* suggestion = |
| 160 [self convertContentSuggestion:contentSuggestion]; | |
| 161 suggestion.type = TypeForCategory(category); | |
| 162 suggestion.section = self.sectionInformationByCategory[categoryWrapper]; | |
| 163 [contentArray addObject:suggestion]; | |
| 105 } | 164 } |
| 106 } | 165 } |
| 107 | 166 |
| 108 - (ContentSuggestion*)convertContentSuggestion: | 167 - (ContentSuggestion*)convertContentSuggestion: |
|
stkhapugin
2017/02/14 16:34:18
This should be a method on ContentSuggestion (poss
gambard
2017/02/15 10:17:56
ContentSuggestion should not know about ntp_snippe
stkhapugin
2017/02/15 15:35:42
A common Objecitve-C pattern is:
@class ModelClas
gambard
2017/02/16 10:06:29
Acknowledged.
| |
| 109 (const ntp_snippets::ContentSuggestion&)contentsuggestion { | 168 (const ntp_snippets::ContentSuggestion&)contentSuggestion { |
| 110 return [[ContentSuggestion alloc] init]; | 169 ContentSuggestionsImageUpdater* imageUpdater = |
| 170 [[ContentSuggestionsImageUpdater alloc] init]; | |
| 171 | |
| 172 __weak ContentSuggestionsImageUpdater* weakImageUpdater = imageUpdater; | |
| 173 self.contentService->FetchSuggestionImage( | |
| 174 contentSuggestion.id(), base::BindBlockArc(^(const gfx::Image& image) { | |
| 175 [weakImageUpdater receivedImage:image.CopyUIImage()]; | |
| 176 })); | |
| 177 | |
| 178 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; | |
| 179 suggestion.title = base::SysUTF16ToNSString(contentSuggestion.title()); | |
| 180 suggestion.text = base::SysUTF16ToNSString(contentSuggestion.snippet_text()); | |
| 181 suggestion.url = contentSuggestion.url(); | |
| 182 suggestion.imageUpdater = imageUpdater; | |
| 183 | |
| 184 return suggestion; | |
| 185 } | |
| 186 | |
| 187 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { | |
| 188 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = | |
| 189 self.contentService->GetCategoryInfo(category); | |
| 190 ContentSuggestionsSectionInformation* sectionInfo = | |
| 191 [[ContentSuggestionsSectionInformation alloc] | |
| 192 initWithID:SectionIDForCategory(category)]; | |
| 193 if (categoryInfo) { | |
| 194 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | |
|
stkhapugin
2017/02/14 16:34:18
All this setup of ContentSuggestionsSectionInforma
gambard
2017/02/15 10:17:56
Same. UI code should not know about ntp_snippets::
| |
| 195 if (categoryInfo->show_if_empty()) { | |
| 196 NSString* title = | |
| 197 base::SysUTF16ToNSString(categoryInfo->no_suggestions_message()); | |
| 198 sectionInfo.emptyCell = [[ContentSuggestionsItem alloc] initWithType:0 | |
| 199 title:title | |
| 200 subtitle:nil]; | |
| 201 } | |
| 202 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | |
| 203 } | |
| 204 | |
| 205 [self.sectionInformationByCategory | |
| 206 setObject:sectionInfo | |
| 207 forKey:[[ContentSuggestionsCategoryWrapper alloc] | |
|
stkhapugin
2017/02/14 16:34:18
Use modern syntax:
self.sectionInformationByCateg
gambard
2017/02/15 10:17:56
Done.
| |
| 208 initWithCategory:category]]; | |
| 111 } | 209 } |
| 112 | 210 |
| 113 @end | 211 @end |
| OLD | NEW |