OLD | NEW |
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/cells/collection_view_text_item.h
" |
10 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" | 11 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
11 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 12 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
12 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" | 13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion.h" |
13 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier
.h" | 14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestion_identifier
.h" |
14 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" | 15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_article_i
tem.h" |
15 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" | 16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sink
.h" |
16 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour
ce.h" | 17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_data_sour
ce.h" |
17 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl
e_item.h" | 18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_expandabl
e_item.h" |
18 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" | 19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_favicon_i
tem.h" |
19 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_footer_it
em.h" | 20 #import "ios/chrome/browser/ui/content_suggestions/content_suggestions_footer_it
em.h" |
(...skipping 12 matching lines...) Expand all Loading... |
32 namespace { | 33 namespace { |
33 | 34 |
34 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. | 35 // Enum defining the ItemType of this ContentSuggestionsCollectionUpdater. |
35 typedef NS_ENUM(NSInteger, ItemType) { | 36 typedef NS_ENUM(NSInteger, ItemType) { |
36 ItemTypeText = kItemTypeEnumZero, | 37 ItemTypeText = kItemTypeEnumZero, |
37 ItemTypeArticle, | 38 ItemTypeArticle, |
38 ItemTypeExpand, | 39 ItemTypeExpand, |
39 ItemTypeStack, | 40 ItemTypeStack, |
40 ItemTypeFavicon, | 41 ItemTypeFavicon, |
41 ItemTypeFooter, | 42 ItemTypeFooter, |
| 43 ItemTypeHeader, |
42 }; | 44 }; |
43 | 45 |
44 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 46 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
45 SectionIdentifierBookmarks = kSectionIdentifierEnumZero, | 47 SectionIdentifierBookmarks = kSectionIdentifierEnumZero, |
46 SectionIdentifierArticles, | 48 SectionIdentifierArticles, |
47 SectionIdentifierDefault, | 49 SectionIdentifierDefault, |
48 }; | 50 }; |
49 | 51 |
50 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { | 52 ItemType ItemTypeForContentSuggestionType(ContentSuggestionType type) { |
51 switch (type) { | 53 switch (type) { |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
159 return; | 161 return; |
160 | 162 |
161 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel | 163 NSIndexPath* indexPath = [self.collectionViewController.collectionViewModel |
162 indexPathForItem:correspondingItem | 164 indexPathForItem:correspondingItem |
163 inSectionWithIdentifier:sectionIdentifier]; | 165 inSectionWithIdentifier:sectionIdentifier]; |
164 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; | 166 [self.collectionViewController dismissEntryAtIndexPath:indexPath]; |
165 } | 167 } |
166 | 168 |
167 - (void)reloadAllData { | 169 - (void)reloadAllData { |
168 [self resetModels]; | 170 [self resetModels]; |
169 [self.collectionViewController | 171 |
170 addSuggestions:[self.dataSource allSuggestions]]; | 172 // The data is reset, add the new data directly in the model then reload the |
| 173 // collection. |
| 174 NSArray<ContentSuggestion*>* suggestions = [self.dataSource allSuggestions]; |
| 175 [self addSectionsForSuggestionsToModel:suggestions]; |
| 176 [self addSuggestionsToModel:suggestions]; |
| 177 [self.collectionViewController.collectionView reloadData]; |
171 } | 178 } |
172 | 179 |
173 - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo { | 180 - (void)clearSection:(ContentSuggestionsSectionInformation*)sectionInfo { |
174 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); | 181 SectionIdentifier sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
175 NSInteger section = [self.collectionViewController.collectionViewModel | 182 NSInteger section = [self.collectionViewController.collectionViewModel |
176 sectionIdentifierForSection:sectionIdentifier]; | 183 sectionIdentifierForSection:sectionIdentifier]; |
177 | 184 |
178 [self.collectionViewController dismissSection:section]; | 185 [self.collectionViewController dismissSection:section]; |
179 } | 186 } |
180 | 187 |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
240 for (ContentSuggestion* suggestion in suggestions) { | 247 for (ContentSuggestion* suggestion in suggestions) { |
241 ContentSuggestionsSectionInformation* sectionInfo = | 248 ContentSuggestionsSectionInformation* sectionInfo = |
242 suggestion.suggestionIdentifier.sectionInfo; | 249 suggestion.suggestionIdentifier.sectionInfo; |
243 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); | 250 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
244 | 251 |
245 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { | 252 if (![model hasSectionForSectionIdentifier:sectionIdentifier]) { |
246 [model addSectionWithIdentifier:sectionIdentifier]; | 253 [model addSectionWithIdentifier:sectionIdentifier]; |
247 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; | 254 self.sectionInfoBySectionIdentifier[@(sectionIdentifier)] = sectionInfo; |
248 [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; | 255 [indexSet addIndex:[model sectionForSectionIdentifier:sectionIdentifier]]; |
249 | 256 |
| 257 [self addHeader:suggestion.suggestionIdentifier.sectionInfo]; |
250 [self addFooterIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; | 258 [self addFooterIfNeeded:suggestion.suggestionIdentifier.sectionInfo]; |
251 } | 259 } |
252 } | 260 } |
253 return indexSet; | 261 return indexSet; |
254 } | 262 } |
255 | 263 |
256 #pragma mark - ContentSuggestionsArticleItemDelegate | 264 #pragma mark - ContentSuggestionsArticleItemDelegate |
257 | 265 |
258 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { | 266 - (void)loadImageForArticleItem:(ContentSuggestionsArticleItem*)articleItem { |
259 NSInteger sectionIdentifier = | 267 NSInteger sectionIdentifier = |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
300 block:^{ | 308 block:^{ |
301 [weakSelf runAdditionalActionForSection:sectionInfo]; | 309 [weakSelf runAdditionalActionForSection:sectionInfo]; |
302 }]; | 310 }]; |
303 | 311 |
304 [self.collectionViewController.collectionViewModel | 312 [self.collectionViewController.collectionViewModel |
305 setFooter:footer | 313 setFooter:footer |
306 forSectionWithIdentifier:sectionIdentifier]; | 314 forSectionWithIdentifier:sectionIdentifier]; |
307 } | 315 } |
308 } | 316 } |
309 | 317 |
| 318 // Adds the header corresponding to |sectionInfo| to the section. |
| 319 - (void)addHeader:(ContentSuggestionsSectionInformation*)sectionInfo { |
| 320 NSInteger sectionIdentifier = SectionIdentifierForInfo(sectionInfo); |
| 321 |
| 322 if (![self.collectionViewController.collectionViewModel |
| 323 headerForSectionWithIdentifier:sectionIdentifier]) { |
| 324 CollectionViewTextItem* header = |
| 325 [[CollectionViewTextItem alloc] initWithType:ItemTypeHeader]; |
| 326 header.text = sectionInfo.title; |
| 327 [self.collectionViewController.collectionViewModel |
| 328 setHeader:header |
| 329 forSectionWithIdentifier:sectionIdentifier]; |
| 330 } |
| 331 } |
| 332 |
310 // Resets the models, removing the current CollectionViewItem and the | 333 // Resets the models, removing the current CollectionViewItem and the |
311 // SectionInfo. | 334 // SectionInfo. |
312 - (void)resetModels { | 335 - (void)resetModels { |
313 [self.collectionViewController loadModel]; | 336 [self.collectionViewController loadModel]; |
314 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; | 337 self.sectionInfoBySectionIdentifier = [[NSMutableDictionary alloc] init]; |
315 } | 338 } |
316 | 339 |
317 // Runs the additional action for the section identified by |sectionInfo|. | 340 // Runs the additional action for the section identified by |sectionInfo|. |
318 - (void)runAdditionalActionForSection: | 341 - (void)runAdditionalActionForSection: |
319 (ContentSuggestionsSectionInformation*)sectionInfo { | 342 (ContentSuggestionsSectionInformation*)sectionInfo { |
(...skipping 19 matching lines...) Expand all Loading... |
339 }]; | 362 }]; |
340 } | 363 } |
341 | 364 |
342 // Adds the |suggestions| to the collection view. All the suggestions must have | 365 // Adds the |suggestions| to the collection view. All the suggestions must have |
343 // the same sectionInfo. | 366 // the same sectionInfo. |
344 - (void)moreSuggestionsFetched:(NSArray<ContentSuggestion*>*)suggestions { | 367 - (void)moreSuggestionsFetched:(NSArray<ContentSuggestion*>*)suggestions { |
345 [self.collectionViewController addSuggestions:suggestions]; | 368 [self.collectionViewController addSuggestions:suggestions]; |
346 } | 369 } |
347 | 370 |
348 @end | 371 @end |
OLD | NEW |