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