| OLD | NEW |
| (Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/content_suggestions/content_suggestions_mediator.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "components/ntp_snippets/category.h" |
| 9 #include "components/ntp_snippets/content_suggestion.h" |
| 10 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid
ge_observer.h" |
| 11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" |
| 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 18 @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { |
| 19 // Bridge for this class to become an observer of a ContentSuggestionsService. |
| 20 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| 21 } |
| 22 |
| 23 @property(nonatomic, assign) |
| 24 ntp_snippets::ContentSuggestionsService* contentService; |
| 25 |
| 26 // Converts the data in |category| to ContentSuggestion and adds them to the |
| 27 // |contentArray|. |
| 28 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 29 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| 30 |
| 31 // Converts the |contentsuggestion| to a ContentSuggestion. |
| 32 - (ContentSuggestion*)convertContentSuggestion: |
| 33 (const ntp_snippets::ContentSuggestion&)contentsuggestion; |
| 34 |
| 35 @end |
| 36 |
| 37 @implementation ContentSuggestionsMediator |
| 38 |
| 39 @synthesize contentService = _contentService; |
| 40 @synthesize dataSink = _dataSink; |
| 41 |
| 42 - (instancetype)initWithContentService: |
| 43 (ntp_snippets::ContentSuggestionsService*)contentService { |
| 44 self = [super init]; |
| 45 if (self) { |
| 46 _suggestionBridge = |
| 47 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); |
| 48 _contentService = contentService; |
| 49 } |
| 50 return self; |
| 51 } |
| 52 |
| 53 #pragma mark - ContentSuggestionsDataSource |
| 54 |
| 55 - (NSArray<ContentSuggestion*>*)allSuggestions { |
| 56 std::vector<ntp_snippets::Category> categories = |
| 57 self.contentService->GetCategories(); |
| 58 NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; |
| 59 for (auto& category : categories) { |
| 60 [self addContentInCategory:category toArray:dataHolders]; |
| 61 } |
| 62 return dataHolders; |
| 63 } |
| 64 |
| 65 #pragma mark - ContentSuggestionsServiceObserver |
| 66 |
| 67 - (void)contentSuggestionsService: |
| 68 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 69 newSuggestionsInCategory:(ntp_snippets::Category)category { |
| 70 [self.dataSink dataAvailable]; |
| 71 } |
| 72 |
| 73 - (void)contentSuggestionsService: |
| 74 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 75 category:(ntp_snippets::Category)category |
| 76 statusChangedTo:(ntp_snippets::CategoryStatus)status { |
| 77 // Update dataSink. |
| 78 } |
| 79 |
| 80 - (void)contentSuggestionsService: |
| 81 (ntp_snippets::ContentSuggestionsService*)suggestionsService |
| 82 SuggestionInvalidated: |
| 83 (const ntp_snippets::ContentSuggestion::ID&)suggestion_id { |
| 84 // Update dataSink. |
| 85 } |
| 86 |
| 87 - (void)contentSuggestionsServiceFullRefreshRequired: |
| 88 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 89 // Update dataSink. |
| 90 } |
| 91 |
| 92 - (void)contentSuggestionsServiceShutdown: |
| 93 (ntp_snippets::ContentSuggestionsService*)suggestionsService { |
| 94 // Update dataSink. |
| 95 } |
| 96 |
| 97 #pragma mark - Private |
| 98 |
| 99 - (void)addContentInCategory:(ntp_snippets::Category&)category |
| 100 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| 101 const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| 102 self.contentService->GetSuggestionsForCategory(category); |
| 103 for (auto& contentSuggestion : suggestions) { |
| 104 [contentArray addObject:[self convertContentSuggestion:contentSuggestion]]; |
| 105 } |
| 106 } |
| 107 |
| 108 - (ContentSuggestion*)convertContentSuggestion: |
| 109 (const ntp_snippets::ContentSuggestion&)contentsuggestion { |
| 110 return [[ContentSuggestion alloc] init]; |
| 111 } |
| 112 |
| 113 @end |
| OLD | NEW |