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_item.h" | |
| 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h" | |
| 19 #include "ui/gfx/image/image.h" | |
| 13 | 20 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 21 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." | 22 #error "This file requires ARC support." |
| 16 #endif | 23 #endif |
| 17 | 24 |
| 25 namespace { | |
| 26 | |
| 27 // Returns the ItemType for this |category|. | |
| 28 ContentSuggestionType TypeForCategory(ntp_snippets::Category category) { | |
| 29 return ContentSuggestionTypeArticle; | |
| 30 } | |
| 31 | |
| 32 // Returns the section ID for this |category|. | |
| 33 ContentSuggestionsSectionID SectionIDForCategory( | |
| 34 ntp_snippets::Category category) { | |
| 35 if (category.IsKnownCategory(ntp_snippets::KnownCategories::BOOKMARKS)) | |
| 36 return ContentSuggestionsSectionBookmarks; | |
| 37 if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) | |
| 38 return ContentSuggestionsSectionArticles; | |
| 39 | |
| 40 return ContentSuggestionsSectionUnknown; | |
| 41 } | |
| 42 | |
| 43 // Returns the section layout corresponding to the category |layout|. | |
| 44 ContentSuggestionsSectionLayout SectionLayoutForLayout( | |
| 45 ntp_snippets::ContentSuggestionsCardLayout layout) { | |
| 46 // For now, only cards are relevant. | |
| 47 return ContentSuggestionsSectionLayoutCard; | |
| 48 } | |
| 49 | |
| 50 // Converts the |contentSuggestion| to a ContentSuggestion. | |
| 51 ContentSuggestion* ConvertContentSuggestion( | |
| 52 const ntp_snippets::ContentSuggestion& contentSuggestion) { | |
| 53 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; | |
| 54 suggestion.title = base::SysUTF16ToNSString(contentSuggestion.title()); | |
| 55 suggestion.text = base::SysUTF16ToNSString(contentSuggestion.snippet_text()); | |
| 56 suggestion.url = contentSuggestion.url(); | |
| 57 | |
| 58 return suggestion; | |
| 59 } | |
| 60 | |
| 61 ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( | |
|
marq (ping after 24h)
2017/02/16 13:14:35
Comment for this function.
gambard
2017/02/16 13:45:53
Done.
| |
| 62 const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, | |
| 63 const ntp_snippets::Category& category) { | |
| 64 ContentSuggestionsSectionInformation* sectionInfo = | |
| 65 [[ContentSuggestionsSectionInformation alloc] | |
| 66 initWithID:SectionIDForCategory(category)]; | |
| 67 if (categoryInfo) { | |
| 68 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | |
| 69 if (categoryInfo->show_if_empty()) { | |
| 70 // TODO(crbug.com/686728): Creates an item to display information when the | |
| 71 // section is empty. | |
| 72 } | |
| 73 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | |
| 74 } | |
| 75 return sectionInfo; | |
|
stkhapugin
2017/02/16 10:53:27
Does this make sense to return an empty section in
gambard
2017/02/16 13:45:53
Presenting an empty section make sense. For now it
| |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 18 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { | 80 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { |
| 19 // Bridge for this class to become an observer of a ContentSuggestionsService. | 81 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 20 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; | 82 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 21 } | 83 } |
| 22 | 84 |
| 23 @property(nonatomic, assign) | 85 @property(nonatomic, assign) |
| 24 ntp_snippets::ContentSuggestionsService* contentService; | 86 ntp_snippets::ContentSuggestionsService* contentService; |
| 87 @property(nonatomic, strong, nonnull) | |
| 88 NSMutableDictionary<ContentSuggestionsCategoryWrapper*, | |
| 89 ContentSuggestionsSectionInformation*>* | |
| 90 sectionInformationByCategory; | |
| 25 | 91 |
| 26 // Converts the data in |category| to ContentSuggestion and adds them to the | 92 // Converts the data in |category| to ContentSuggestion and adds them to the |
| 27 // |contentArray|. | 93 // |contentArray|. |
| 28 - (void)addContentInCategory:(ntp_snippets::Category&)category | 94 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 29 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; | 95 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 30 | 96 |
| 31 // Converts the |contentsuggestion| to a ContentSuggestion. | 97 // Adds the section information for |category| in |
| 32 - (ContentSuggestion*)convertContentSuggestion: | 98 // self.sectionInformationByCategory. |
| 33 (const ntp_snippets::ContentSuggestion&)contentsuggestion; | 99 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| 34 | 100 |
| 35 @end | 101 @end |
| 36 | 102 |
| 37 @implementation ContentSuggestionsMediator | 103 @implementation ContentSuggestionsMediator |
| 38 | 104 |
| 39 @synthesize contentService = _contentService; | 105 @synthesize contentService = _contentService; |
| 40 @synthesize dataSink = _dataSink; | 106 @synthesize dataSink = _dataSink; |
| 107 @synthesize sectionInformationByCategory = _sectionInformationByCategory; | |
| 41 | 108 |
| 42 - (instancetype)initWithContentService: | 109 - (instancetype)initWithContentService: |
| 43 (ntp_snippets::ContentSuggestionsService*)contentService { | 110 (ntp_snippets::ContentSuggestionsService*)contentService { |
| 44 self = [super init]; | 111 self = [super init]; |
| 45 if (self) { | 112 if (self) { |
| 46 _suggestionBridge = | 113 _suggestionBridge = |
| 47 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); | 114 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); |
| 48 _contentService = contentService; | 115 _contentService = contentService; |
| 116 _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; | |
| 49 } | 117 } |
| 50 return self; | 118 return self; |
| 51 } | 119 } |
| 52 | 120 |
| 53 #pragma mark - ContentSuggestionsDataSource | 121 #pragma mark - ContentSuggestionsDataSource |
| 54 | 122 |
| 55 - (NSArray<ContentSuggestion*>*)allSuggestions { | 123 - (NSArray<ContentSuggestion*>*)allSuggestions { |
| 56 std::vector<ntp_snippets::Category> categories = | 124 std::vector<ntp_snippets::Category> categories = |
| 57 self.contentService->GetCategories(); | 125 self.contentService->GetCategories(); |
| 58 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; | 126 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; |
| 59 for (auto& category : categories) { | 127 for (auto& category : categories) { |
| 128 if (self.contentService->GetCategoryStatus(category) != | |
| 129 ntp_snippets::CategoryStatus::AVAILABLE) { | |
| 130 continue; | |
| 131 } | |
| 132 if (!self.sectionInformationByCategory[ | |
| 133 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { | |
| 134 [self addSectionInformationForCategory:category]; | |
| 135 } | |
| 60 [self addContentInCategory:category toArray:dataHolders]; | 136 [self addContentInCategory:category toArray:dataHolders]; |
| 61 } | 137 } |
| 62 return dataHolders; | 138 return dataHolders; |
| 63 } | 139 } |
| 64 | 140 |
| 65 #pragma mark - ContentSuggestionsServiceObserver | 141 #pragma mark - ContentSuggestionsServiceObserver |
| 66 | 142 |
| 67 - (void)contentSuggestionsService: | 143 - (void)contentSuggestionsService: |
| 68 (ntp_snippets::ContentSuggestionsService*)suggestionsService | 144 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 69 newSuggestionsInCategory:(ntp_snippets::Category)category { | 145 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 93 (ntp_snippets::ContentSuggestionsService*)suggestionsService { | 169 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 94 // Update dataSink. | 170 // Update dataSink. |
| 95 } | 171 } |
| 96 | 172 |
| 97 #pragma mark - Private | 173 #pragma mark - Private |
| 98 | 174 |
| 99 - (void)addContentInCategory:(ntp_snippets::Category&)category | 175 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 100 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { | 176 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| 101 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = | 177 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| 102 self.contentService->GetSuggestionsForCategory(category); | 178 self.contentService->GetSuggestionsForCategory(category); |
| 179 ContentSuggestionsCategoryWrapper* categoryWrapper = | |
| 180 [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; | |
| 103 for (auto& contentSuggestion : suggestions) { | 181 for (auto& contentSuggestion : suggestions) { |
| 104 [contentArray addObject:[self convertContentSuggestion:contentSuggestion]]; | 182 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
| 183 suggestion.type = TypeForCategory(category); | |
| 184 suggestion.section = self.sectionInformationByCategory[categoryWrapper]; | |
| 185 | |
| 186 // TODO(crbug.com/686728): fetch the image. | |
| 187 | |
| 188 [contentArray addObject:suggestion]; | |
| 105 } | 189 } |
| 106 } | 190 } |
| 107 | 191 |
| 108 - (ContentSuggestion*)convertContentSuggestion: | 192 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { |
| 109 (const ntp_snippets::ContentSuggestion&)contentsuggestion { | 193 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = |
| 110 return [[ContentSuggestion alloc] init]; | 194 self.contentService->GetCategoryInfo(category); |
| 195 | |
| 196 ContentSuggestionsSectionInformation* sectionInfo = | |
| 197 SectionInformationFromCategoryInfo(categoryInfo, category); | |
| 198 | |
| 199 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper | |
| 200 wrapperWithCategory:category]] = sectionInfo; | |
| 111 } | 201 } |
| 112 | 202 |
| 113 @end | 203 @end |
| OLD | NEW |