Chromium Code Reviews| Index: ios/chrome/browser/ui/suggestions/suggestions_data_source.mm |
| diff --git a/ios/chrome/browser/ui/suggestions/suggestions_data_source.mm b/ios/chrome/browser/ui/suggestions/suggestions_data_source.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..31df618a150192f1faae3d9b3c66e168f16b4413 |
| --- /dev/null |
| +++ b/ios/chrome/browser/ui/suggestions/suggestions_data_source.mm |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/chrome/browser/ui/suggestions/suggestions_data_source.h" |
| + |
| +#import "ios/chrome/browser/ui/collection_view/collection_view_controller.h" |
| +#import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| +#import "ios/chrome/browser/ui/suggestions/suggestions_article_item.h" |
| +#import "ios/chrome/browser/ui/suggestions/suggestions_item.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +namespace { |
| +typedef NS_ENUM(NSInteger, ItemType) { |
| + ItemTypeText = kItemTypeEnumZero, |
| + ItemTypeArticle, |
| + ItemTypeExpand, |
| +}; |
| + |
| +} // namespace |
| + |
| +@implementation SuggestionsDataSource { |
|
marq (ping after 24h)
2017/01/12 15:05:16
I'm confused; where's the @interface for this decl
gambard
2017/01/12 15:28:19
This is an error during rebase...
|
| + NSMutableArray<NSMutableArray<CollectionViewItem*>*>* _items; |
| +} |
| + |
| +@synthesize collectionViewController = _collectionViewController; |
| + |
| +- (instancetype)init { |
| + self = [super init]; |
| + if (self) { |
| + _items = [NSMutableArray arrayWithCapacity:1]; |
| + for (NSInteger i = 0; i < 3; i++) { |
| + NSMutableArray<CollectionViewItem*>* arrayToAdd = |
| + [NSMutableArray arrayWithCapacity:3]; |
| + [arrayToAdd |
| + addObject:[[SuggestionsArticleItem alloc] |
| + initWithType:ItemTypeArticle |
| + title:@"Title of an Article" |
| + subtitle:@"This is the subtitle of an article, can " |
| + @"spawn on multiple lines" |
| + image:[UIImage |
| + imageNamed:@"distillation_success"]]]; |
| + [arrayToAdd |
| + addObject:[[SuggestionsItem alloc] initWithType:ItemTypeText |
| + title:@"The title" |
| + subtitle:@"The subtitle"]]; |
| + [_items addObject:arrayToAdd]; |
| + } |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark - Properties |
| + |
| +- (void)setCollectionViewController: |
| + (CollectionViewController*)collectionViewController { |
| + _collectionViewController = collectionViewController; |
| + if (!collectionViewController) { |
| + return; |
| + } |
| + [collectionViewController loadModel]; |
| + CollectionViewModel* model = collectionViewController.collectionViewModel; |
| + NSInteger sectionIdentifier = kSectionIdentifierEnumZero; |
| + for (NSMutableArray<CollectionViewItem*>* arrayWithItems in _items) { |
| + [model addSectionWithIdentifier:sectionIdentifier]; |
| + for (CollectionViewItem* item in arrayWithItems) { |
| + [model addItem:item toSectionWithIdentifier:sectionIdentifier]; |
| + } |
| + sectionIdentifier++; |
| + } |
| +} |
| + |
| +#pragma mark - Public methods |
| + |
| +- (void)addTextItem:(NSString*)title |
| + subtitle:(NSString*)subtitle |
| + toSection:(NSUInteger)section { |
| + SuggestionsItem* item = [[SuggestionsItem alloc] initWithType:ItemTypeText |
| + title:title |
| + subtitle:subtitle]; |
| + NSInteger sectionIdentifier = kSectionIdentifierEnumZero + section; |
| + NSInteger sectionIndex = section; |
| + if (section >= [_items count]) { |
| + sectionIndex = [_items count]; |
| + sectionIdentifier = kSectionIdentifierEnumZero + section; |
| + [_items addObject:[NSMutableArray arrayWithObject:item]]; |
| + if (_collectionViewController) { |
| + [_collectionViewController.collectionView performBatchUpdates:^{ |
| + [_collectionViewController.collectionViewModel |
| + addSectionWithIdentifier:sectionIdentifier]; |
| + [_collectionViewController.collectionView |
| + insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; |
| + } |
| + completion:^(BOOL){ |
| + }]; |
| + } |
| + } |
| + [[_items objectAtIndex:section] addObject:item]; |
| + if (_collectionViewController) { |
| + [_collectionViewController.collectionViewModel addItem:item |
| + toSectionWithIdentifier:sectionIdentifier]; |
| + [_collectionViewController.collectionView performBatchUpdates:^{ |
| + [_collectionViewController.collectionView insertItemsAtIndexPaths:@[ |
| + [NSIndexPath indexPathForRow:[[_items objectAtIndex:section] count] - 1 |
| + inSection:section] |
| + ]]; |
| + } |
| + completion:^(BOOL){ |
| + }]; |
| + } |
| +} |
| + |
| +@end |