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/mac/bind_objc_block.h" |
8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
9 #include "base/optional.h" | 9 #include "base/optional.h" |
10 #include "base/strings/sys_string_conversions.h" | 10 #include "base/strings/sys_string_conversions.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 // Returns a SectionInformation for a |category|, filled with the | 90 // Returns a SectionInformation for a |category|, filled with the |
91 // |categoryInfo|. | 91 // |categoryInfo|. |
92 ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( | 92 ContentSuggestionsSectionInformation* SectionInformationFromCategoryInfo( |
93 const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, | 93 const base::Optional<ntp_snippets::CategoryInfo>& categoryInfo, |
94 const ntp_snippets::Category& category) { | 94 const ntp_snippets::Category& category) { |
95 ContentSuggestionsSectionInformation* sectionInfo = | 95 ContentSuggestionsSectionInformation* sectionInfo = |
96 [[ContentSuggestionsSectionInformation alloc] | 96 [[ContentSuggestionsSectionInformation alloc] |
97 initWithSectionID:SectionIDForCategory(category)]; | 97 initWithSectionID:SectionIDForCategory(category)]; |
98 if (categoryInfo) { | 98 if (categoryInfo) { |
99 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); | 99 sectionInfo.layout = SectionLayoutForLayout(categoryInfo->card_layout()); |
100 if (categoryInfo->show_if_empty()) { | 100 sectionInfo.showIfEmpty = categoryInfo->show_if_empty(); |
101 // TODO(crbug.com/686728): Creates an item to display information when the | 101 sectionInfo.emptyText = |
102 // section is empty. | 102 base::SysUTF16ToNSString(categoryInfo->no_suggestions_message()); |
103 } | |
104 if (categoryInfo->additional_action() != | 103 if (categoryInfo->additional_action() != |
105 ntp_snippets::ContentSuggestionsAdditionalAction::NONE) { | 104 ntp_snippets::ContentSuggestionsAdditionalAction::NONE) { |
106 sectionInfo.footerTitle = | 105 sectionInfo.footerTitle = |
107 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); | 106 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); |
108 } | 107 } |
109 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); | 108 sectionInfo.title = base::SysUTF16ToNSString(categoryInfo->title()); |
110 } | 109 } |
111 return sectionInfo; | 110 return sectionInfo; |
112 } | 111 } |
113 | 112 |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 suggestionIdentifier.IDInSection), | 308 suggestionIdentifier.IDInSection), |
310 base::BindBlockArc(callback)); | 309 base::BindBlockArc(callback)); |
311 } | 310 } |
312 | 311 |
313 #pragma mark - Private | 312 #pragma mark - Private |
314 | 313 |
315 - (void)addSuggestions: | 314 - (void)addSuggestions: |
316 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions | 315 (const std::vector<ntp_snippets::ContentSuggestion>&)suggestions |
317 fromCategory:(ntp_snippets::Category&)category | 316 fromCategory:(ntp_snippets::Category&)category |
318 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { | 317 toArray:(NSMutableArray<ContentSuggestion*>*)contentArray { |
319 if (self.contentService->GetCategoryStatus(category) != | 318 if (!ntp_snippets::IsCategoryStatusAvailable( |
320 ntp_snippets::CategoryStatus::AVAILABLE) { | 319 self.contentService->GetCategoryStatus(category))) { |
321 return; | 320 return; |
322 } | 321 } |
323 | 322 |
324 ContentSuggestionsCategoryWrapper* categoryWrapper = | 323 ContentSuggestionsCategoryWrapper* categoryWrapper = |
325 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; | 324 [ContentSuggestionsCategoryWrapper wrapperWithCategory:category]; |
326 if (!self.sectionInformationByCategory[categoryWrapper]) { | 325 if (!self.sectionInformationByCategory[categoryWrapper]) { |
327 [self addSectionInformationForCategory:category]; | 326 [self addSectionInformationForCategory:category]; |
328 } | 327 } |
329 | 328 |
330 for (auto& contentSuggestion : suggestions) { | 329 for (auto& contentSuggestion : suggestions) { |
331 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); | 330 ContentSuggestion* suggestion = ConvertContentSuggestion(contentSuggestion); |
332 | 331 |
333 suggestion.type = TypeForCategory(category); | 332 suggestion.type = TypeForCategory(category); |
334 | 333 |
335 suggestion.suggestionIdentifier.sectionInfo = | 334 suggestion.suggestionIdentifier.sectionInfo = |
336 self.sectionInformationByCategory[categoryWrapper]; | 335 self.sectionInformationByCategory[categoryWrapper]; |
337 | 336 |
338 [contentArray addObject:suggestion]; | 337 [contentArray addObject:suggestion]; |
339 } | 338 } |
| 339 |
| 340 if (suggestions.size() == 0) { |
| 341 ContentSuggestion* suggestion = [[ContentSuggestion alloc] init]; |
| 342 suggestion.type = ContentSuggestionTypeEmpty; |
| 343 suggestion.suggestionIdentifier = |
| 344 [[ContentSuggestionIdentifier alloc] init]; |
| 345 suggestion.suggestionIdentifier.sectionInfo = |
| 346 self.sectionInformationByCategory[categoryWrapper]; |
| 347 |
| 348 [contentArray addObject:suggestion]; |
| 349 } |
340 } | 350 } |
341 | 351 |
342 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { | 352 - (void)addSectionInformationForCategory:(ntp_snippets::Category)category { |
343 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = | 353 base::Optional<ntp_snippets::CategoryInfo> categoryInfo = |
344 self.contentService->GetCategoryInfo(category); | 354 self.contentService->GetCategoryInfo(category); |
345 | 355 |
346 ContentSuggestionsSectionInformation* sectionInfo = | 356 ContentSuggestionsSectionInformation* sectionInfo = |
347 SectionInformationFromCategoryInfo(categoryInfo, category); | 357 SectionInformationFromCategoryInfo(categoryInfo, category); |
348 | 358 |
349 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper | 359 self.sectionInformationByCategory[[ContentSuggestionsCategoryWrapper |
(...skipping 17 matching lines...) Expand all Loading... |
367 [NSMutableArray array]; | 377 [NSMutableArray array]; |
368 ntp_snippets::Category category = suggestions[0].id().category(); | 378 ntp_snippets::Category category = suggestions[0].id().category(); |
369 [self addSuggestions:suggestions | 379 [self addSuggestions:suggestions |
370 fromCategory:category | 380 fromCategory:category |
371 toArray:contentSuggestions]; | 381 toArray:contentSuggestions]; |
372 callback(contentSuggestions); | 382 callback(contentSuggestions); |
373 } | 383 } |
374 } | 384 } |
375 | 385 |
376 @end | 386 @end |
OLD | NEW |