| Index: ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm
|
| diff --git a/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm b/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..401520f0912a3b72431bd6ef51e81da118d2c55c
|
| --- /dev/null
|
| +++ b/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm
|
| @@ -0,0 +1,113 @@
|
| +// Copyright 2017 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#import "ios/chrome/browser/content_suggestions/content_suggestions_mediator.h"
|
| +
|
| +#include "base/memory/ptr_util.h"
|
| +#include "components/ntp_snippets/category.h"
|
| +#include "components/ntp_snippets/content_suggestion.h"
|
| +#import "ios/chrome/browser/content_suggestions/content_suggestions_data_holder.h"
|
| +#import "ios/chrome/browser/content_suggestions/content_suggestions_service_bridge_observer.h"
|
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink.h"
|
| +
|
| +#if !defined(__has_feature) || !__has_feature(objc_arc)
|
| +#error "This file requires ARC support."
|
| +#endif
|
| +
|
| +@interface ContentSuggestionsMediator ()<
|
| + ContentSuggestionsServiceBridgeObserver> {
|
| + // Bridge for this class to become an observer of a ContentSuggestionsService.
|
| + std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge;
|
| +}
|
| +
|
| +@property(nonatomic, assign)
|
| + ntp_snippets::ContentSuggestionsService* contentService;
|
| +
|
| +// Converts the data in |category| to ContentSuggestionsDataHolder and adds them
|
| +// to the |contentArray|.
|
| +- (void)addContentInCategory:(ntp_snippets::Category&)category
|
| + toArray:(NSMutableArray<ContentSuggestionsDataHolder*>*)
|
| + contentArray;
|
| +
|
| +// Converts the |contentsuggestion| to a ContentSuggestionsDataHolder.
|
| +- (ContentSuggestionsDataHolder*)convertContentSuggestion:
|
| + (const ntp_snippets::ContentSuggestion&)contentsuggestion;
|
| +
|
| +@end
|
| +
|
| +@implementation ContentSuggestionsMediator
|
| +
|
| +@synthesize contentService = _contentService;
|
| +@synthesize dataSink = _dataSink;
|
| +
|
| +- (instancetype)initWithContentService:
|
| + (ntp_snippets::ContentSuggestionsService*)contentService {
|
| + self = [super init];
|
| + if (self) {
|
| + _suggestionBridge =
|
| + base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService);
|
| + _contentService = contentService;
|
| + }
|
| + return self;
|
| +}
|
| +
|
| +#pragma mark - ContentSuggestionsDataSource
|
| +
|
| +- (NSArray<ContentSuggestionsDataHolder*>*)getData {
|
| + std::vector<ntp_snippets::Category> categories =
|
| + self.contentService->GetCategories();
|
| + NSMutableArray<ContentSuggestionsDataHolder*>* dataHolders =
|
| + [NSMutableArray array];
|
| + for (auto& category : categories) {
|
| + [self addContentInCategory:category toArray:dataHolders];
|
| + }
|
| + return dataHolders;
|
| +}
|
| +
|
| +- (NSArray<ContentSuggestionsDataHolder*>*)getMoreData {
|
| + return nil;
|
| +}
|
| +
|
| +#pragma mark - ContentSuggestionsServiceBridgeObserver
|
| +
|
| +- (void)onNewSuggestions:(ntp_snippets::Category)category {
|
| + [self.dataSink dataAvailable];
|
| +}
|
| +
|
| +- (void)onCategory:(ntp_snippets::Category)category
|
| + statusChanged:(ntp_snippets::CategoryStatus)status {
|
| + // Update dataSink.
|
| +}
|
| +
|
| +- (void)onSuggestionInvalidated:
|
| + (const ntp_snippets::ContentSuggestion::ID&)suggestion_id {
|
| + // Update dataSink.
|
| +}
|
| +
|
| +- (void)onFullRefreshRequired {
|
| + // Update dataSink.
|
| +}
|
| +
|
| +- (void)contentSuggestionsServiceShutdown {
|
| + // Update dataSink.
|
| +}
|
| +
|
| +#pragma mark - Private
|
| +
|
| +- (void)addContentInCategory:(ntp_snippets::Category&)category
|
| + toArray:(NSMutableArray<ContentSuggestionsDataHolder*>*)
|
| + contentArray {
|
| + const std::vector<ntp_snippets::ContentSuggestion>& suggestions =
|
| + self.contentService->GetSuggestionsForCategory(category);
|
| + for (auto& contentSuggestion : suggestions) {
|
| + [contentArray addObject:[self convertContentSuggestion:contentSuggestion]];
|
| + }
|
| +}
|
| +
|
| +- (ContentSuggestionsDataHolder*)convertContentSuggestion:
|
| + (const ntp_snippets::ContentSuggestion&)contentsuggestion {
|
| + return [[ContentSuggestionsDataHolder alloc] init];
|
| +}
|
| +
|
| +@end
|
|
|