OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/suggestions/suggestions_collection_updater.h" |
| 6 |
| 7 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| 8 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 9 #import "ios/chrome/browser/ui/suggestions/suggestions_item.h" |
| 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." |
| 13 #endif |
| 14 |
| 15 namespace { |
| 16 typedef NS_ENUM(NSInteger, ItemType) { |
| 17 ItemTypeText = kItemTypeEnumZero, |
| 18 ItemTypeArticle, |
| 19 ItemTypeExpand, |
| 20 }; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 @implementation SuggestionsCollectionUpdater { |
| 25 CollectionViewController* _collectionViewController; |
| 26 } |
| 27 |
| 28 - (instancetype)initWithCollectionViewController: |
| 29 (CollectionViewController*)collectionViewController { |
| 30 self = [super init]; |
| 31 if (self) { |
| 32 _collectionViewController = collectionViewController; |
| 33 [collectionViewController loadModel]; |
| 34 CollectionViewModel* model = collectionViewController.collectionViewModel; |
| 35 NSInteger sectionIdentifier = kSectionIdentifierEnumZero; |
| 36 for (NSInteger i = 0; i < 3; i++) { |
| 37 [model addSectionWithIdentifier:sectionIdentifier]; |
| 38 [model addItem:[[SuggestionsItem alloc] initWithType:ItemTypeText |
| 39 title:@"The title" |
| 40 subtitle:@"The subtitle"] |
| 41 toSectionWithIdentifier:sectionIdentifier]; |
| 42 sectionIdentifier++; |
| 43 } |
| 44 } |
| 45 return self; |
| 46 } |
| 47 |
| 48 #pragma mark - Public methods |
| 49 |
| 50 - (void)addTextItem:(NSString*)title |
| 51 subtitle:(NSString*)subtitle |
| 52 toSection:(NSInteger)inputSection { |
| 53 SuggestionsItem* item = [[SuggestionsItem alloc] initWithType:ItemTypeText |
| 54 title:title |
| 55 subtitle:subtitle]; |
| 56 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection; |
| 57 NSInteger sectionIndex = inputSection; |
| 58 CollectionViewModel* model = _collectionViewController.collectionViewModel; |
| 59 if ([model numberOfSections] <= inputSection) { |
| 60 sectionIndex = [model numberOfSections]; |
| 61 sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex; |
| 62 [_collectionViewController.collectionView performBatchUpdates:^{ |
| 63 [_collectionViewController.collectionViewModel |
| 64 addSectionWithIdentifier:sectionIdentifier]; |
| 65 [_collectionViewController.collectionView |
| 66 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; |
| 67 } |
| 68 completion:nil]; |
| 69 } |
| 70 NSInteger numberOfItemsInSection = |
| 71 [model numberOfItemsInSection:sectionIndex]; |
| 72 [_collectionViewController.collectionViewModel addItem:item |
| 73 toSectionWithIdentifier:sectionIdentifier]; |
| 74 [_collectionViewController.collectionView performBatchUpdates:^{ |
| 75 [_collectionViewController.collectionView |
| 76 insertItemsAtIndexPaths:@[ [NSIndexPath |
| 77 indexPathForRow:numberOfItemsInSection |
| 78 inSection:sectionIndex] ]]; |
| 79 } |
| 80 completion:nil]; |
| 81 } |
| 82 |
| 83 @end |
OLD | NEW |