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