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

Side by Side Diff: ios/chrome/browser/ui/content_suggestions/content_suggestions_collection_updater.mm

Issue 2697843003: Display articles in Content Suggestions (Closed)
Patch Set: Created 3 years, 10 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
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/ui/content_suggestions/content_suggestions_collectio n_updater.h" 5 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_collectio n_updater.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/mac/foundation_util.h" 8 #include "base/mac/foundation_util.h"
9 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 9 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h"
11 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i tem.h" 12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i tem.h"
12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h" 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink .h"
13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h" 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour ce.h"
14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl e_item.h" 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl e_item.h"
15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i tem.h" 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i tem.h"
16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h" 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_item.h"
18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_section_i nformation.h"
17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite m.h" 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_stack_ite m.h"
18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h" 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_view_cont roller.h"
19 #include "url/gurl.h" 21 #include "url/gurl.h"
20 22
21 #if !defined(__has_feature) || !__has_feature(objc_arc) 23 #if !defined(__has_feature) || !__has_feature(objc_arc)
22 #error "This file requires ARC support." 24 #error "This file requires ARC support."
23 #endif 25 #endif
24 26
27 namespace {
28
29 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater.
30 typedef NS_ENUM(NSInteger, ItemType) {
31 ItemTypeText = kItemTypeEnumZero,
32 ItemTypeArticle,
33 ItemTypeExpand,
34 ItemTypeStack,
35 ItemTypeFavicon,
36 };
37
38 typedef NS_ENUM(NSInteger, SectionIdentifier) {
39 SectionIdentifierBookmarks = kSectionIdentifierEnumZero,
40 SectionIdentifierArticles,
41 SectionIdentifierDefault,
42 };
43
44 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) {
45 switch (type) {
46 case ContentSuggestionTypeArticle:
47 return ItemTypeArticle;
48 }
49 }
50
51 ContentSuggestionType ContentSuggestionTypeForItemType(NSInteger type) {
52 if (type == ItemTypeArticle)
53 return ContentSuggestionTypeArticle;
54 // Add new type here
55
56 // Default type.
57 return ContentSuggestionTypeArticle;
58 }
59
60 // Returns the section identifier corresponding to the section |info|.
61 SectionIdentifier SectionIdentifierForInfo(
62 ContentSuggestionsSectionInformation* info) {
63 switch (info.ID) {
64 case ContentSuggestionsSectionBookmarks:
65 return SectionIdentifierBookmarks;
66
67 case ContentSuggestionsSectionArticles:
68 return SectionIdentifierArticles;
69
70 case ContentSuggestionsSectionUnknown:
71 return SectionIdentifierDefault;
72 }
73 }
74
75 } // namespace
76
25 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink> 77 @interface ContentSuggestionsCollectionUpdater ()<ContentSuggestionsDataSink>
26 78
27 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource; 79 @property(nonatomic, weak) id<ContentSuggestionsDataSource> dataSource;
80 @property(nonatomic, strong)
81 NSMutableDictionary* sectionInfoBySectionIdentifier;
stkhapugin 2017/02/15 15:42:36 Please add types
gambard 2017/02/16 10:11:06 Done.
82
83 // Reloads all the data from the data source, deleting all the current items.
84 - (void)reloadData;
85 // Adds a new section if needed and returns the section identifier.
86 - (NSInteger)addSectionIfNeeded:
87 (ContentSuggestionsSectionInformation*)sectionInformation;
88 // Resets the models and cells.
89 - (void)resetModels;
28 90
29 @end 91 @end
30 92
31 @implementation ContentSuggestionsCollectionUpdater 93 @implementation ContentSuggestionsCollectionUpdater
32 94
33 @synthesize collectionViewController = _collectionViewController; 95 @synthesize collectionViewController = _collectionViewController;
34 @synthesize dataSource = _dataSource; 96 @synthesize dataSource = _dataSource;
97 @synthesize sectionInfoBySectionIdentifier = _sectionInfoBySectionIdentifier;
35 98
36 - (instancetype)initWithDataSource: 99 - (instancetype)initWithDataSource:
37 (id<ContentSuggestionsDataSource>)dataSource { 100 (id<ContentSuggestionsDataSource>)dataSource {
38 self = [super init]; 101 self = [super init];
39 if (self) { 102 if (self) {
40 _dataSource = dataSource; 103 _dataSource = dataSource;
41 _dataSource.dataSink = self; 104 _dataSource.dataSink = self;
42 } 105 }
43 return self; 106 return self;
44 } 107 }
45 108
46 #pragma mark - Properties 109 #pragma mark - Properties
47 110
48 - (void)setCollectionViewController: 111 - (void)setCollectionViewController:
49 (ContentSuggestionsViewController*)collectionViewController { 112 (ContentSuggestionsViewController*)collectionViewController {
50 _collectionViewController = collectionViewController; 113 _collectionViewController = collectionViewController;
51 [collectionViewController loadModel];
52 CollectionViewModel* model = collectionViewController.collectionViewModel;
53 114
54 // TODO(crbug.com/686728): Load the data with the dataSource instead of hard 115 [self reloadData];
55 // coded value.
56
57 NSInteger sectionIdentifier = kSectionIdentifierEnumZero;
58
59 // Stack Item.
60 [model addSectionWithIdentifier:sectionIdentifier];
61 [model addItem:[[ContentSuggestionsStackItem alloc]
62 initWithType:ItemTypeStack
63 title:@"The title"
64 subtitle:@"The subtitle"]
65 toSectionWithIdentifier:sectionIdentifier++];
66
67 // Favicon Item.
68 [model addSectionWithIdentifier:sectionIdentifier];
69 ContentSuggestionsFaviconItem* faviconItem =
70 [[ContentSuggestionsFaviconItem alloc] initWithType:ItemTypeFavicon];
71 for (NSInteger i = 0; i < 6; i++) {
72 [faviconItem addFavicon:[UIImage imageNamed:@"bookmark_gray_star_large"]
73 withTitle:@"Super website! Incredible!"];
74 }
75 faviconItem.delegate = _collectionViewController;
76 [model addItem:faviconItem toSectionWithIdentifier:sectionIdentifier++];
77
78 for (NSInteger i = 0; i < 3; i++) {
79 [model addSectionWithIdentifier:sectionIdentifier];
80
81 // Standard Item.
82 [model addItem:[[ContentSuggestionsItem alloc] initWithType:ItemTypeText
83 title:@"The title"
84 subtitle:@"The subtitle"]
85 toSectionWithIdentifier:sectionIdentifier];
86
87 // Article Item.
88 [model addItem:[[ContentSuggestionsArticleItem alloc]
89 initWithType:ItemTypeArticle
90 title:@"Title of an Article"
91 subtitle:@"This is the subtitle of an article, can "
92 @"spawn on multiple lines"
93 image:[UIImage imageNamed:@"distillation_success"]
94 url:GURL()]
95 toSectionWithIdentifier:sectionIdentifier];
96
97 // Expandable Item.
98 SuggestionsExpandableItem* expandableItem =
99 [[SuggestionsExpandableItem alloc]
100 initWithType:ItemTypeExpand
101 title:@"Title of an Expandable Article"
102 subtitle:@"This Article can be expanded to display "
103 @"additional information or interaction "
104 @"options"
105 image:[UIImage imageNamed:@"distillation_fail"]
106 detailText:@"Details shown only when the article is "
107 @"expanded. It can be displayed on "
108 @"multiple lines."];
109 expandableItem.delegate = _collectionViewController;
110 [model addItem:expandableItem toSectionWithIdentifier:sectionIdentifier];
111 sectionIdentifier++;
112 }
113 } 116 }
114 117
115 #pragma mark - ContentSuggestionsDataSink 118 #pragma mark - ContentSuggestionsDataSink
116 119
117 - (void)dataAvailable { 120 - (void)dataAvailable {
118 // TODO(crbug.com/686728): Get the new data from the DataSource. 121 [self reloadData];
119 } 122 }
120 123
121 #pragma mark - Public methods 124 #pragma mark - Public methods
122 125
123 - (void)addTextItem:(NSString*)title 126 - (void)addTextItem:(NSString*)title
124 subtitle:(NSString*)subtitle 127 subtitle:(NSString*)subtitle
125 toSection:(NSInteger)inputSection { 128 toSection:(NSInteger)inputSection {
126 DCHECK(_collectionViewController); 129 DCHECK(self.collectionViewController);
127 ContentSuggestionsItem* item = 130 ContentSuggestionsItem* item =
128 [[ContentSuggestionsItem alloc] initWithType:ItemTypeText 131 [[ContentSuggestionsItem alloc] initWithType:ItemTypeText
129 title:title 132 title:title
130 subtitle:subtitle]; 133 subtitle:subtitle];
131 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection; 134 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection;
132 NSInteger sectionIndex = inputSection; 135 NSInteger sectionIndex = inputSection;
133 CollectionViewModel* model = _collectionViewController.collectionViewModel; 136 CollectionViewModel* model =
137 self.collectionViewController.collectionViewModel;
134 if ([model numberOfSections] <= inputSection) { 138 if ([model numberOfSections] <= inputSection) {
135 sectionIndex = [model numberOfSections]; 139 sectionIndex = [model numberOfSections];
136 sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex; 140 sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex;
137 [_collectionViewController.collectionView performBatchUpdates:^{ 141 [self.collectionViewController.collectionView performBatchUpdates:^{
138 [_collectionViewController.collectionViewModel 142 [self.collectionViewController.collectionViewModel
139 addSectionWithIdentifier:sectionIdentifier]; 143 addSectionWithIdentifier:sectionIdentifier];
140 [_collectionViewController.collectionView 144 [self.collectionViewController.collectionView
141 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; 145 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
142 } 146 }
143 completion:nil]; 147 completion:nil];
144 } 148 }
145 NSInteger numberOfItemsInSection = 149 NSInteger numberOfItemsInSection =
146 [model numberOfItemsInSection:sectionIndex]; 150 [model numberOfItemsInSection:sectionIndex];
147 [_collectionViewController.collectionViewModel addItem:item 151 [self.collectionViewController.collectionViewModel addItem:item
148 toSectionWithIdentifier:sectionIdentifier]; 152 toSectionWithIdentifier:sectionIdentifier];
149 [_collectionViewController.collectionView performBatchUpdates:^{ 153 [self.collectionViewController.collectionView performBatchUpdates:^{
150 [_collectionViewController.collectionView 154 [self.collectionViewController.collectionView
151 insertItemsAtIndexPaths:@[ [NSIndexPath 155 insertItemsAtIndexPaths:@[ [NSIndexPath
152 indexPathForRow:numberOfItemsInSection 156 indexPathForRow:numberOfItemsInSection
153 inSection:sectionIndex] ]]; 157 inSection:sectionIndex] ]];
154 } 158 }
155 completion:nil]; 159 completion:nil];
156 } 160 }
157 161
158 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section { 162 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section {
159 return section == 0 || section == 1; 163 NSInteger identifier = [self.collectionViewController.collectionViewModel
stkhapugin 2017/02/15 15:42:36 You can create an NSNumber here directly to avoid
gambard 2017/02/16 10:11:06 Done.
164 sectionIdentifierForSection:section];
165 ContentSuggestionsSectionInformation* sectionInformation =
166 [self.sectionInfoBySectionIdentifier objectForKey:@(identifier)];
stkhapugin 2017/02/15 15:42:36 self.sectionInfoBySectionIdentifier[@(identifier)]
gambard 2017/02/16 10:11:06 Done.
167 return sectionInformation.layout == ContentSuggestionsSectionLayoutCustom;
168 }
169
170 - (ContentSuggestionType)contentSuggestionTypeForItem:
171 (CollectionViewItem*)item {
172 return ContentSuggestionTypeForItemType(item.type);
173 }
174
175 #pragma mark - Private methods
176
177 - (void)reloadData {
178 [self resetModels];
179 CollectionViewModel* model =
180 self.collectionViewController.collectionViewModel;
181
182 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions];
183
184 for (ContentSuggestion* suggestion in suggestions) {
185 NSInteger sectionIdentifier = [self addSectionIfNeeded:suggestion.section];
186 ContentSuggestionsArticleItem* articleItem =
187 [[ContentSuggestionsArticleItem alloc]
188 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
189 title:suggestion.title
190 subtitle:suggestion.text
191 image:suggestion.image
192 url:suggestion.url];
193
194 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
195 }
196
197 if ([self.collectionViewController isViewLoaded]) {
198 [self.collectionViewController.collectionView reloadData];
199 }
200 }
201
202 - (NSInteger)addSectionIfNeeded:
203 (ContentSuggestionsSectionInformation*)sectionInformation {
204 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation);
205
206 CollectionViewModel* model =
207 self.collectionViewController.collectionViewModel;
208 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) {
209 [model addSectionWithIdentifier:sectionIdentifier];
210 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] =
211 sectionInformation;
212 [self.sectionInfoBySectionIdentifier setObject:sectionInformation
213 forKey:@(sectionIdentifier)];
214 }
215 return sectionIdentifier;
216 }
217
218 - (void)resetModels {
219 [self.collectionViewController loadModel];
220 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init];
160 } 221 }
161 222
162 @end 223 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698