Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm

Issue 2638413006: Add ContentSuggestionsMediator (Closed)
Patch Set: Fix Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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_data_holder. h"
11 #import "ios/chrome/browser/content_suggestions/content_suggestions_service_brid ge_observer.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 ()<
19 ContentSuggestionsServiceBridgeObserver> {
20 // Bridge for this class to become an observer of a ContentSuggestionsService.
21 std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge;
22 }
23
24 @property(nonatomic, assign)
25 ntp_snippets::ContentSuggestionsService* contentService;
26
27 // Converts the data in |category| to ContentSuggestionsDataHolder and adds them
28 // to the |contentArray|.
29 - (void)addContentInCategory:(ntp_snippets::Category&)category
30 toArray:(NSMutableArray<ContentSuggestionsDataHolder*>*)
31 contentArray;
32
33 // Converts the |contentsuggestion| to a ContentSuggestionsDataHolder.
34 - (ContentSuggestionsDataHolder*)convertContentSuggestion:
35 (const ntp_snippets::ContentSuggestion&)contentsuggestion;
36
37 @end
38
39 @implementation ContentSuggestionsMediator
40
41 @synthesize contentService = _contentService;
42 @synthesize dataSink = _dataSink;
43
44 - (instancetype)initWithContentService:
45 (ntp_snippets::ContentSuggestionsService*)contentService {
46 self = [super init];
47 if (self) {
48 _suggestionBridge =
49 base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService);
50 _contentService = contentService;
51 }
52 return self;
53 }
54
55 #pragma mark - ContentSuggestionsDataSource
56
57 - (NSArray<ContentSuggestionsDataHolder*>*)getData {
58 std::vector<ntp_snippets::Category> categories =
59 self.contentService->GetCategories();
60 NSMutableArray<ContentSuggestionsDataHolder*>* dataHolders =
61 [NSMutableArray array];
62 for (auto& category : categories) {
63 [self addContentInCategory:category toArray:dataHolders];
64 }
65 return dataHolders;
66 }
67
68 - (NSArray<ContentSuggestionsDataHolder*>*)getMoreData {
69 return nil;
70 }
71
72 #pragma mark - ContentSuggestionsServiceBridgeObserver
73
74 - (void)onNewSuggestions:(ntp_snippets::Category)category {
75 [self.dataSink dataAvailable];
76 }
77
78 - (void)onCategory:(ntp_snippets::Category)category
79 statusChanged:(ntp_snippets::CategoryStatus)status {
80 // Update dataSink.
81 }
82
83 - (void)onSuggestionInvalidated:
84 (const ntp_snippets::ContentSuggestion::ID&)suggestion_id {
85 // Update dataSink.
86 }
87
88 - (void)onFullRefreshRequired {
89 // Update dataSink.
90 }
91
92 - (void)contentSuggestionsServiceShutdown {
93 // Update dataSink.
94 }
95
96 #pragma mark - Private
97
98 - (void)addContentInCategory:(ntp_snippets::Category&)category
99 toArray:(NSMutableArray<ContentSuggestionsDataHolder*>*)
100 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 - (ContentSuggestionsDataHolder*)convertContentSuggestion:
109 (const ntp_snippets::ContentSuggestion&)contentsuggestion {
110 return [[ContentSuggestionsDataHolder alloc] init];
111 }
112
113 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698