Chromium Code Reviews| 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 |
| index c87b1631ed1e5f2e9755a62e6b91d777c554fcac..a9c19085fdf8fa87d75ca82fce774641e2c351a7 100644 |
| --- a/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm |
| +++ b/ios/chrome/browser/content_suggestions/content_suggestions_mediator.mm |
| @@ -4,17 +4,79 @@ |
| #import "ios/chrome/browser/content_suggestions/content_suggestions_mediator.h" |
| +#include "base/mac/bind_objc_block.h" |
| #include "base/memory/ptr_util.h" |
| +#include "base/optional.h" |
| +#include "base/strings/sys_string_conversions.h" |
| #include "components/ntp_snippets/category.h" |
| #include "components/ntp_snippets/content_suggestion.h" |
| +#import "ios/chrome/browser/content_suggestions/content_suggestions_category_wrapper.h" |
| #import "ios/chrome/browser/content_suggestions/content_suggestions_service_bridge_observer.h" |
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
| #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink.h" |
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h" |
| +#import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_information.h" |
| +#include "ui/gfx/image/image.h" |
| #if !defined(__has_feature) || !__has_feature(objc_arc) |
| #error "This file requires ARC support." |
| #endif |
| +namespace { |
| + |
| +// Returns the ItemType for this |category|. |
| +ContentSuggestionType TypeForCategory(ntp_snippets::Category category) { |
| + return ContentSuggestionTypeArticle; |
| +} |
| + |
| +// Returns the section ID for this |category|. |
| +ContentSuggestionsSectionID SectionIDForCategory( |
| + ntp_snippets::Category category) { |
| + if (category.IsKnownCategory(ntp_snippets::KnownCategories::BOOKMARKS)) |
| + return ContentSuggestionsSectionBookmarks; |
| + if (category.IsKnownCategory(ntp_snippets::KnownCategories::ARTICLES)) |
| + return ContentSuggestionsSectionArticles; |
| + |
| + return ContentSuggestionsSectionUnknown; |
| +} |
| + |
| +// Returns the section layout corresponding to the category |layout|. |
| +ContentSuggestionsSectionLayout SectionLayoutForLayout( |
| + ntp_snippets::ContentSuggestionsCardLayout layout) { |
| + // For now, only cards are relevant. |
| + return ContentSuggestionsSectionLayoutCard; |
| +} |
| + |
| +// Converts the |contentSuggestion| to a ContentSuggestion. |
| +ContentSuggestion* ConvertContentSuggestion( |
| + const ntp_snippets::ContentSuggestion& contentSuggestion) { |
| + ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; |
| + suggestion.title = base::SysUTF16ToNSString(contentSuggestion.title()); |
| + suggestion.text = base::SysUTF16ToNSString(contentSuggestion.snippet_text()); |
| + suggestion.url = contentSuggestion.url(); |
| + |
| + return suggestion; |
| +} |
| + |
| +ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( |
|
marq (ping after 24h)
2017/02/16 13:14:35
Comment for this function.
gambard
2017/02/16 13:45:53
Done.
|
| + const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, |
| + const ntp_snippets::Category& category) { |
| + ContentSuggestionsSectionInformation* sectionInfo = |
| + [[ContentSuggestionsSectionInformation alloc] |
| + initWithID:SectionIDForCategory(category)]; |
| + if (categoryInfo) { |
| + sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); |
| + if (categoryInfo->show_if_empty()) { |
| + // TODO(crbug.com/686728): Creates an item to display information when the |
| + // section is empty. |
| + } |
| + sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); |
| + } |
| + return sectionInfo; |
|
stkhapugin
2017/02/16 10:53:27
Does this make sense to return an empty section in
gambard
2017/02/16 13:45:53
Presenting an empty section make sense. For now it
|
| +} |
| + |
| +} // namespace |
| + |
| @interface ContentSuggestionsMediator ()<ContentSuggestionsServiceObserver> { |
| // Bridge for this class to become an observer of a ContentSuggestionsService. |
| std::unique_ptr<ContentSuggestionsServiceBridge> _suggestionBridge; |
| @@ -22,15 +84,19 @@ |
| @property(nonatomic, assign) |
| ntp_snippets::ContentSuggestionsService* contentService; |
| +@property(nonatomic, strong, nonnull) |
| + NSMutableDictionary<ContentSuggestionsCategoryWrapper*, |
| + ContentSuggestionsSectionInformation*>* |
| + sectionInformationByCategory; |
| // Converts the data in |category| to ContentSuggestion and adds them to the |
| // |contentArray|. |
| - (void)addContentInCategory:(ntp_snippets::Category&)category |
| toArray:(NSMutableArray<ContentSuggestion*>*)contentArray; |
| -// Converts the |contentsuggestion| to a ContentSuggestion. |
| -- (ContentSuggestion*)convertContentSuggestion: |
| - (const ntp_snippets::ContentSuggestion&)contentsuggestion; |
| +// Adds the section information for |category| in |
| +// self.sectionInformationByCategory. |
| +- (void)addSectionInformationForCategory:(ntp_snippets::Category)category; |
| @end |
| @@ -38,6 +104,7 @@ |
| @synthesize contentService = _contentService; |
| @synthesize dataSink = _dataSink; |
| +@synthesize sectionInformationByCategory = _sectionInformationByCategory; |
| - (instancetype)initWithContentService: |
| (ntp_snippets::ContentSuggestionsService*)contentService { |
| @@ -46,6 +113,7 @@ |
| _suggestionBridge = |
| base::MakeUnique<ContentSuggestionsServiceBridge>(self, contentService); |
| _contentService = contentService; |
| + _sectionInformationByCategory = [[NSMutableDictionary alloc] init]; |
| } |
| return self; |
| } |
| @@ -57,6 +125,14 @@ |
| self.contentService->GetCategories(); |
| NSMutableArray<ContentSuggestion*>* dataHolders = [NSMutableArray array]; |
| for (auto& category : categories) { |
| + if (self.contentService->GetCategoryStatus(category) != |
| + ntp_snippets::CategoryStatus::AVAILABLE) { |
| + continue; |
| + } |
| + if (!self.sectionInformationByCategory[ |
| + [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]]) { |
| + [self addSectionInformationForCategory:category]; |
| + } |
| [self addContentInCategory:category toArray:dataHolders]; |
| } |
| return dataHolders; |
| @@ -100,14 +176,28 @@ |
| toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
| const std::vector<ntp_snippets::ContentSuggestion>& suggestions = |
| self.contentService->GetSuggestionsForCategory(category); |
| + ContentSuggestionsCategoryWrapper* categoryWrapper = |
| + [[ContentSuggestionsCategoryWrapper alloc] initWithCategory:category]; |
| for (auto& contentSuggestion : suggestions) { |
| - [contentArray addObject:[self convertContentSuggestion:contentSuggestion]]; |
| + ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
| + suggestion.type = TypeForCategory(category); |
| + suggestion.section = self.sectionInformationByCategory[categoryWrapper]; |
| + |
| + // TODO(crbug.com/686728): fetch the image. |
| + |
| + [contentArray addObject:suggestion]; |
| } |
| } |
| -- (ContentSuggestion*)convertContentSuggestion: |
| - (const ntp_snippets::ContentSuggestion&)contentsuggestion { |
| - return [[ContentSuggestion alloc] init]; |
| +- (void)addSectionInformationForCategory:(ntp_snippets::Category)category { |
| + base::Optional<ntp_snippets::CategoryInfo> categoryInfo = |
| + self.contentService->GetCategoryInfo(category); |
| + |
| + ContentSuggestionsSectionInformation* sectionInfo = |
| + SectionInformationFromCategoryInfo(categoryInfo, category); |
| + |
| + self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper |
| + wrapperWithCategory:category]] = sectionInfo; |
| } |
| @end |