| 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_data_source.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_article_item.h" |
| 10 #import "ios/chrome/browser/ui/suggestions/suggestions_item.h" |
| 11 |
| 12 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 13 #error "This file requires ARC support." |
| 14 #endif |
| 15 |
| 16 namespace { |
| 17 typedef NS_ENUM(NSInteger, ItemType) { |
| 18 ItemTypeText = kItemTypeEnumZero, |
| 19 ItemTypeArticle, |
| 20 ItemTypeExpand, |
| 21 }; |
| 22 |
| 23 } // namespace |
| 24 |
| 25 @implementation SuggestionsDataSource { |
| 26 NSMutableArray<NSMutableArray<CollectionViewItem*>*>* _items; |
| 27 } |
| 28 |
| 29 @synthesize collectionViewController = _collectionViewController; |
| 30 |
| 31 - (instancetype)init { |
| 32 self = [super init]; |
| 33 if (self) { |
| 34 _items = [NSMutableArray arrayWithCapacity:1]; |
| 35 for (NSInteger i = 0; i < 3; i++) { |
| 36 NSMutableArray<CollectionViewItem*>* arrayToAdd = |
| 37 [NSMutableArray arrayWithCapacity:3]; |
| 38 [arrayToAdd |
| 39 addObject:[[SuggestionsArticleItem alloc] |
| 40 initWithType:ItemTypeArticle |
| 41 title:@"Title of an Article" |
| 42 subtitle:@"This is the subtitle of an article, can " |
| 43 @"spawn on multiple lines" |
| 44 image:[UIImage |
| 45 imageNamed:@"distillation_success"]]]; |
| 46 [arrayToAdd |
| 47 addObject:[[SuggestionsItem alloc] initWithType:ItemTypeText |
| 48 title:@"The title" |
| 49 subtitle:@"The subtitle"]]; |
| 50 [_items addObject:arrayToAdd]; |
| 51 } |
| 52 } |
| 53 return self; |
| 54 } |
| 55 |
| 56 #pragma mark - Properties |
| 57 |
| 58 - (void)setCollectionViewController: |
| 59 (CollectionViewController*)collectionViewController { |
| 60 _collectionViewController = collectionViewController; |
| 61 if (!collectionViewController) { |
| 62 return; |
| 63 } |
| 64 [collectionViewController loadModel]; |
| 65 CollectionViewModel* model = collectionViewController.collectionViewModel; |
| 66 NSInteger sectionIdentifier = kSectionIdentifierEnumZero; |
| 67 for (NSMutableArray<CollectionViewItem*>* arrayWithItems in _items) { |
| 68 [model addSectionWithIdentifier:sectionIdentifier]; |
| 69 for (CollectionViewItem* item in arrayWithItems) { |
| 70 [model addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| 71 } |
| 72 sectionIdentifier++; |
| 73 } |
| 74 } |
| 75 |
| 76 #pragma mark - Public methods |
| 77 |
| 78 - (void)addTextItem:(NSString*)title |
| 79 subtitle:(NSString*)subtitle |
| 80 toSection:(NSUInteger)section { |
| 81 SuggestionsItem* item = [[SuggestionsItem alloc] initWithType:ItemTypeText |
| 82 title:title |
| 83 subtitle:subtitle]; |
| 84 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + section; |
| 85 NSInteger sectionIndex = section; |
| 86 if (section >= [_items count]) { |
| 87 sectionIndex = [_items count]; |
| 88 sectionIdentifier = kSectionIdentifierEnumZero + section; |
| 89 [_items addObject:[NSMutableArray arrayWithObject:item]]; |
| 90 if (_collectionViewController) { |
| 91 [_collectionViewController.collectionView performBatchUpdates:^{ |
| 92 [_collectionViewController.collectionViewModel |
| 93 addSectionWithIdentifier:sectionIdentifier]; |
| 94 [_collectionViewController.collectionView |
| 95 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; |
| 96 } |
| 97 completion:^(BOOL){ |
| 98 }]; |
| 99 } |
| 100 } |
| 101 [[_items objectAtIndex:section] addObject:item]; |
| 102 if (_collectionViewController) { |
| 103 [_collectionViewController.collectionViewModel addItem:item |
| 104 toSectionWithIdentifier:sectionIdentifier]; |
| 105 [_collectionViewController.collectionView performBatchUpdates:^{ |
| 106 [_collectionViewController.collectionView insertItemsAtIndexPaths:@[ |
| 107 [NSIndexPath indexPathForRow:[[_items objectAtIndex:section] count] - 1 |
| 108 inSection:section] |
| 109 ]]; |
| 110 } |
| 111 completion:^(BOOL){ |
| 112 }]; |
| 113 } |
| 114 } |
| 115 |
| 116 @end |
| OLD | NEW |