| 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/showcase/content_suggestions/sc_content_suggestions_data_source.h" |
| 6 |
| 7 #include "components/strings/grit/components_strings.h" |
| 8 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" |
| 9 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_image_fet
cher.h" |
| 10 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion
_identifier.h" |
| 11 #import "ios/chrome/browser/ui/content_suggestions/identifier/content_suggestion
s_section_information.h" |
| 12 #import "ios/chrome/browser/ui/favicon/favicon_attributes.h" |
| 13 #include "ios/chrome/grit/ios_strings.h" |
| 14 #import "ios/showcase/content_suggestions/sc_content_suggestions_item.h" |
| 15 #import "ios/showcase/content_suggestions/sc_content_suggestions_most_visited_it
em.h" |
| 16 #include "ui/base/l10n/l10n_util_mac.h" |
| 17 |
| 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 19 #error "This file requires ARC support." |
| 20 #endif |
| 21 |
| 22 namespace { |
| 23 using CSCollectionViewItem = CollectionViewItem<SuggestedContent>; |
| 24 } |
| 25 |
| 26 @interface SCContentSuggestionsDataSource ()<ContentSuggestionsImageFetcher> |
| 27 |
| 28 // Section Info of type MostVisited. Created lazily. |
| 29 @property(nonatomic, strong) |
| 30 ContentSuggestionsSectionInformation* mostVisitedSection; |
| 31 // Section Info of type Reading List. Created lazily. |
| 32 @property(nonatomic, strong) |
| 33 ContentSuggestionsSectionInformation* readingListSection; |
| 34 // Section Info of type Article. Created lazily. |
| 35 @property(nonatomic, strong) |
| 36 ContentSuggestionsSectionInformation* articleSection; |
| 37 |
| 38 @end |
| 39 |
| 40 @implementation SCContentSuggestionsDataSource |
| 41 |
| 42 @synthesize dataSink = _dataSink; |
| 43 @synthesize mostVisitedSection = _mostVisitedSection; |
| 44 @synthesize readingListSection = _readingListSection; |
| 45 @synthesize articleSection = _articleSection; |
| 46 |
| 47 #pragma mark - ContentSuggestionsDataSource |
| 48 |
| 49 - (NSArray<ContentSuggestionsSectionInformation*>*)sectionsInfo { |
| 50 return @[ |
| 51 self.mostVisitedSection, self.readingListSection, self.articleSection |
| 52 ]; |
| 53 } |
| 54 |
| 55 - (NSArray<CSCollectionViewItem*>*)itemsForSectionInfo: |
| 56 (ContentSuggestionsSectionInformation*)sectionInfo { |
| 57 switch (sectionInfo.sectionID) { |
| 58 case ContentSuggestionsSectionMostVisited: { |
| 59 NSMutableArray<CSCollectionViewItem*>* items = [NSMutableArray array]; |
| 60 for (int i = 0; i < 8; i++) { |
| 61 SCContentSuggestionsMostVisitedItem* suggestion = |
| 62 [[SCContentSuggestionsMostVisitedItem alloc] initWithType:0]; |
| 63 suggestion.title = |
| 64 [NSString stringWithFormat:@"Test Tile number %i", i]; |
| 65 suggestion.attributes = [self randomColorFaviconAttributes]; |
| 66 [items addObject:suggestion]; |
| 67 } |
| 68 return items; |
| 69 } |
| 70 case ContentSuggestionsSectionReadingList: { |
| 71 SCContentSuggestionsItem* suggestion = [self article]; |
| 72 suggestion.title = @"Reading List item: No subtitle, no image"; |
| 73 suggestion.subtitle = nil; |
| 74 suggestion.hasImage = NO; |
| 75 suggestion.suggestionIdentifier.sectionInfo = self.readingListSection; |
| 76 return @[ suggestion ]; |
| 77 } |
| 78 case ContentSuggestionsSectionArticles: { |
| 79 NSMutableArray<CSCollectionViewItem*>* items = [NSMutableArray array]; |
| 80 SCContentSuggestionsItem* suggestion = [self article]; |
| 81 suggestion.title = @"Title of the first suggestions"; |
| 82 suggestion.suggestionIdentifier.IDInSection = std::to_string(1); |
| 83 [items addObject:suggestion]; |
| 84 for (int i = 2; i < 6; i++) { |
| 85 suggestion = suggestion = [self article]; |
| 86 suggestion.title = [NSString |
| 87 stringWithFormat:@"Title of the suggestions number #%i", i]; |
| 88 suggestion.suggestionIdentifier.IDInSection = std::to_string(i); |
| 89 [items addObject:suggestion]; |
| 90 } |
| 91 return items; |
| 92 } |
| 93 case ContentSuggestionsSectionUnknown: |
| 94 return @[]; |
| 95 } |
| 96 } |
| 97 |
| 98 - (id<ContentSuggestionsImageFetcher>)imageFetcher { |
| 99 return self; |
| 100 } |
| 101 |
| 102 - (void)fetchMoreSuggestionsKnowing: |
| 103 (NSArray<ContentSuggestionIdentifier*>*)knownSuggestions |
| 104 fromSectionInfo: |
| 105 (ContentSuggestionsSectionInformation*)sectionInfo |
| 106 callback:(MoreSuggestionsFetched)callback { |
| 107 } |
| 108 |
| 109 - (void)fetchFaviconAttributesForItem:(CSCollectionViewItem*)item |
| 110 completion:(void (^)(FaviconAttributes*))completion { |
| 111 } |
| 112 |
| 113 - (void)fetchFaviconImageForItem:(CSCollectionViewItem*)item |
| 114 completion:(void (^)(UIImage*))completion { |
| 115 } |
| 116 |
| 117 #pragma mark - ContentSuggestionsImageFetcher |
| 118 |
| 119 - (void)fetchImageForSuggestion: |
| 120 (ContentSuggestionIdentifier*)suggestionIdentifier |
| 121 callback:(void (^)(UIImage*))callback { |
| 122 if (callback) { |
| 123 callback([UIImage imageNamed:@"reading_list_empty_state"]); |
| 124 } |
| 125 } |
| 126 |
| 127 #pragma mark - Property |
| 128 |
| 129 - (ContentSuggestionsSectionInformation*)mostVisitedSection { |
| 130 if (!_mostVisitedSection) { |
| 131 _mostVisitedSection = [[ContentSuggestionsSectionInformation alloc] |
| 132 initWithSectionID:ContentSuggestionsSectionMostVisited]; |
| 133 _mostVisitedSection.layout = ContentSuggestionsSectionLayoutCustom; |
| 134 } |
| 135 return _mostVisitedSection; |
| 136 } |
| 137 |
| 138 - (ContentSuggestionsSectionInformation*)readingListSection { |
| 139 if (!_readingListSection) { |
| 140 _readingListSection = [[ContentSuggestionsSectionInformation alloc] |
| 141 initWithSectionID:ContentSuggestionsSectionReadingList]; |
| 142 _readingListSection.title = |
| 143 l10n_util::GetNSString(IDS_NTP_READING_LIST_SUGGESTIONS_SECTION_HEADER); |
| 144 _readingListSection.footerTitle = |
| 145 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); |
| 146 } |
| 147 return _readingListSection; |
| 148 } |
| 149 |
| 150 - (ContentSuggestionsSectionInformation*)articleSection { |
| 151 if (!_articleSection) { |
| 152 _articleSection = [[ContentSuggestionsSectionInformation alloc] |
| 153 initWithSectionID:ContentSuggestionsSectionArticles]; |
| 154 _articleSection.title = |
| 155 l10n_util::GetNSString(IDS_NTP_ARTICLE_SUGGESTIONS_SECTION_HEADER); |
| 156 _articleSection.footerTitle = |
| 157 l10n_util::GetNSString(IDS_IOS_CONTENT_SUGGESTIONS_FOOTER_TITLE); |
| 158 } |
| 159 return _articleSection; |
| 160 } |
| 161 |
| 162 #pragma mark - Private |
| 163 |
| 164 // Returns favicon attributes with a random background color and a random |
| 165 // monogram. |
| 166 - (FaviconAttributes*)randomColorFaviconAttributes { |
| 167 UIColor* backgroundColor = [[UIColor alloc] initWithHue:drand48() |
| 168 saturation:drand48() / 2 + 0.5 |
| 169 brightness:drand48() / 2 + 0.5 |
| 170 alpha:1.0]; |
| 171 |
| 172 NSString* monogram = |
| 173 [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'A']; |
| 174 UIColor* textColor = |
| 175 drand48() > 0.5 ? [UIColor whiteColor] : [UIColor blackColor]; |
| 176 |
| 177 return [FaviconAttributes attributesWithMonogram:monogram |
| 178 textColor:textColor |
| 179 backgroundColor:backgroundColor]; |
| 180 } |
| 181 |
| 182 // Returns a random date between now and three days before now. |
| 183 - (NSDate*)randomDate { |
| 184 int offset = arc4random_uniform(259200); |
| 185 return [NSDate dateWithTimeIntervalSinceNow:-offset]; |
| 186 } |
| 187 |
| 188 // Returns an article with the fields set except the IDInSection. |
| 189 - (SCContentSuggestionsItem*)article { |
| 190 SCContentSuggestionsItem* suggestion = |
| 191 [[SCContentSuggestionsItem alloc] initWithType:0]; |
| 192 suggestion.title = @"Title"; |
| 193 suggestion.subtitle = @"Subtitle for this greeeeeaaaaaat suggestion!"; |
| 194 suggestion.publisher = @"Publisher of the new"; |
| 195 suggestion.hasImage = YES; |
| 196 suggestion.publishDate = [self randomDate]; |
| 197 suggestion.attributes = [self randomColorFaviconAttributes]; |
| 198 suggestion.suggestionIdentifier = [[ContentSuggestionIdentifier alloc] init]; |
| 199 suggestion.suggestionIdentifier.sectionInfo = self.articleSection; |
| 200 return suggestion; |
| 201 } |
| 202 |
| 203 @end |
| OLD | NEW |