| 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..9d6cdf35bda6a5cf095d45d633520628374ea92a
|
| --- /dev/null
|
| +++ b/ios/chrome/browser/ui/suggestions/suggestions_data_source.mm
|
| @@ -0,0 +1,153 @@
|
| +// 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"
|
| +
|
| +#include "base/mac/foundation_util.h"
|
| +#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.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_expandable_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 {
|
| + 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"]];
|
| + [arrayToAdd
|
| + addObject:[[SuggestionsExpandableItem alloc]
|
| + initWithType:ItemTypeExpand
|
| + title:@"Title of an Expandable Article"
|
| + subtitle:@"This Article can be expanded to display "
|
| + @"addition information or interaction "
|
| + @"options"
|
| + image:[UIImage imageNamed:@"distillation_fail"]
|
| + detailText:@"Details shown only when the article is "
|
| + @"expanded. It can be displayed on "
|
| + @"multiple lines."]];
|
| + [_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) {
|
| + if ([item isKindOfClass:[SuggestionsExpandableItem class]]) {
|
| + SuggestionsExpandableItem* expandableItem =
|
| + base::mac::ObjCCastStrict<SuggestionsExpandableItem>(item);
|
| + expandableItem.collectionView = collectionViewController.collectionView;
|
| + }
|
| + [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){
|
| + }];
|
| + }
|
| +}
|
| +
|
| +#pragma mark - SuggestionsDataSource
|
| +
|
| +- (CGFloat)collectionView:(UICollectionView*)collectionView
|
| + cellHeightAtIndexPath:(NSIndexPath*)indexPath {
|
| + CollectionViewItem* item =
|
| + [_collectionViewController.collectionViewModel itemAtIndexPath:indexPath];
|
| + UIEdgeInsets inset = [_collectionViewController
|
| + collectionView:collectionView
|
| + layout:collectionView.collectionViewLayout
|
| + insetForSectionAtIndex:indexPath.section];
|
| +
|
| + return [MDCCollectionViewCell
|
| + cr_preferredHeightForWidth:CGRectGetWidth(
|
| + _collectionViewController.view.bounds) -
|
| + inset.left - inset.right
|
| + forItem:item];
|
| +}
|
| +
|
| +@end
|
|
|