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

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

Issue 2751713002: Fetch Suggestions by category (Closed)
Patch Set: git cl web Created 3 years, 9 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 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" 10 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
116 116
117 - (void)setCollectionViewController: 117 - (void)setCollectionViewController:
118 (ContentSuggestionsViewController*)collectionViewController { 118 (ContentSuggestionsViewController*)collectionViewController {
119 _collectionViewController = collectionViewController; 119 _collectionViewController = collectionViewController;
120 120
121 [self reloadAllData]; 121 [self reloadAllData];
122 } 122 }
123 123
124 #pragma mark - ContentSuggestionsDataSink 124 #pragma mark - ContentSuggestionsDataSink
125 125
126 - (void)dataAvailable { 126 - (void)dataAvailableForSection:
127 [self reloadAllData]; 127 (ContentSuggestionsSectionInformation*)sectionInfo {
128 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo);
129
130 CollectionViewModel* model =
131 self.collectionViewController.collectionViewModel;
132 if ([model hasSectionForSectionIdentifier:sectionIdentifier] &&
133 [model itemsInSectionWithIdentifier:sectionIdentifier].count > 0) {
134 // Do not dismiss the presented items.
135 return;
136 }
137
138 [self addSuggestions:[self.dataSource suggestionsForSection:sectionInfo]];
128 } 139 }
129 140
130 - (void)clearSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier { 141 - (void)clearSuggestion:(ContentSuggestionIdentifier*)suggestionIdentifier {
131 SectionIdentifier sectionIdentifier = 142 SectionIdentifier sectionIdentifier =
132 SectionIdentifierForInfo(suggestionIdentifier.sectionInfo); 143 SectionIdentifierForInfo(suggestionIdentifier.sectionInfo);
133 if (![self.collectionViewController.collectionViewModel 144 if (![self.collectionViewController.collectionViewModel
134 hasSectionForSectionIdentifier:sectionIdentifier]) { 145 hasSectionForSectionIdentifier:sectionIdentifier]) {
135 return; 146 return;
136 } 147 }
137 148
(...skipping 14 matching lines...) Expand all
152 return; 163 return;
153 164
154 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel 165 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel
155 indexPathForItem:correspondingItem 166 indexPathForItem:correspondingItem
156 inSectionWithIdentifier:sectionIdentifier]; 167 inSectionWithIdentifier:sectionIdentifier];
157 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; 168 [self.collectionViewController dismissEntryAtIndexPath:indexPath];
158 } 169 }
159 170
160 - (void)reloadAllData { 171 - (void)reloadAllData {
161 [self resetModels]; 172 [self resetModels];
162 CollectionViewModel* model = 173 [self addSuggestions:[self.dataSource allSuggestions]];
163 self.collectionViewController.collectionViewModel;
164
165 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions];
166
167 for (ContentSuggestion* suggestion in suggestions) {
168 NSInteger sectionIdentifier =
169 [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo];
170 ContentSuggestionsArticleItem* articleItem =
171 [[ContentSuggestionsArticleItem alloc]
172 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
173 title:suggestion.title
174 subtitle:suggestion.text
175 delegate:self
176 url:suggestion.url];
177
178 articleItem.publisher = suggestion.publisher;
179 articleItem.publishDate = suggestion.publishDate;
180
181 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
182
183 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
184 }
185
186 if ([self.collectionViewController isViewLoaded]) {
187 [self.collectionViewController.collectionView reloadData];
188 }
189 } 174 }
190 175
191 - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo { 176 - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo {
192 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); 177 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo);
193 NSInteger section = [self.collectionViewController.collectionViewModel 178 NSInteger section = [self.collectionViewController.collectionViewModel
194 sectionIdentifierForSection:sectionIdentifier]; 179 sectionIdentifierForSection:sectionIdentifier];
195 180
196 [self.collectionViewController dismissSection:section]; 181 [self.collectionViewController dismissSection:section];
197 } 182 }
198 183
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 inSectionWithIdentifier:sectionIdentifier]; 221 inSectionWithIdentifier:sectionIdentifier];
237 }; 222 };
238 223
239 [self.dataSource.imageFetcher 224 [self.dataSource.imageFetcher
240 fetchImageForSuggestion:articleItem.suggestionIdentifier 225 fetchImageForSuggestion:articleItem.suggestionIdentifier
241 callback:imageFetchedCallback]; 226 callback:imageFetchedCallback];
242 } 227 }
243 228
244 #pragma mark - Private methods 229 #pragma mark - Private methods
245 230
231 // Add the |suggestions| to the model and reload the data.
232 - (void)addSuggestions:(NSArray<ContentSuggestion*>*)suggestions {
233 if (suggestions.count == 0) {
234 return;
235 }
236
237 CollectionViewModel* model =
238 self.collectionViewController.collectionViewModel;
239
240 for (ContentSuggestion* suggestion in suggestions) {
241 NSInteger sectionIdentifier =
242 [self addSectionIfNeeded:suggestion.suggestionIdentifier.sectionInfo];
243 ContentSuggestionsArticleItem* articleItem =
244 [[ContentSuggestionsArticleItem alloc]
245 initWithType:ItemTypeForContentSuggestionType(suggestion.type)
246 title:suggestion.title
247 subtitle:suggestion.text
248 delegate:self
249 url:suggestion.url];
250
251 articleItem.publisher = suggestion.publisher;
252 articleItem.publishDate = suggestion.publishDate;
253
254 articleItem.suggestionIdentifier = suggestion.suggestionIdentifier;
255
256 [model addItem:articleItem toSectionWithIdentifier:sectionIdentifier];
257 }
258
259 if ([self.collectionViewController isViewLoaded]) {
260 [self.collectionViewController.collectionView reloadData];
261 }
262 }
263
246 - (NSInteger)addSectionIfNeeded: 264 - (NSInteger)addSectionIfNeeded:
247 (ContentSuggestionsSectionInformation*)sectionInformation { 265 (ContentSuggestionsSectionInformation*)sectionInformation {
248 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation); 266 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInformation);
249 267
250 CollectionViewModel* model = 268 CollectionViewModel* model =
251 self.collectionViewController.collectionViewModel; 269 self.collectionViewController.collectionViewModel;
252 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { 270 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) {
253 [model addSectionWithIdentifier:sectionIdentifier]; 271 [model addSectionWithIdentifier:sectionIdentifier];
254 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = 272 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] =
255 sectionInformation; 273 sectionInformation;
256 [self.sectionInfoBySectionIdentifier setObject:sectionInformation 274 [self.sectionInfoBySectionIdentifier setObject:sectionInformation
257 forKey:@(sectionIdentifier)]; 275 forKey:@(sectionIdentifier)];
258 } 276 }
259 return sectionIdentifier; 277 return sectionIdentifier;
260 } 278 }
261 279
262 - (void)resetModels { 280 - (void)resetModels {
263 [self.collectionViewController loadModel]; 281 [self.collectionViewController loadModel];
264 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; 282 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init];
265 } 283 }
266 284
267 @end 285 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698