Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(96)

Side by Side Diff: ios/chrome/browser/ui/suggestions/suggestions_data_source.mm

Issue 2618343002: Creates the Suggestions UI (Closed)
Patch Set: Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 #include "base/mac/foundation_util.h"
8 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h"
9 #import "ios/chrome/browser/ui/collection_view/collection_view_controller.h"
10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
11 #import "ios/chrome/browser/ui/suggestions/suggestions_article_item.h"
12 #import "ios/chrome/browser/ui/suggestions/suggestions_expandable_item.h"
13 #import "ios/chrome/browser/ui/suggestions/suggestions_item.h"
14
15 #if !defined(__has_feature) || !__has_feature(objc_arc)
16 #error "This file requires ARC support."
17 #endif
18
19 namespace {
20 typedef NS_ENUM(NSInteger, ItemType) {
21 ItemTypeText = kItemTypeEnumZero,
22 ItemTypeArticle,
23 ItemTypeExpand,
24 };
25
26 } // namespace
27
28 @implementation SuggestionsDataSource {
29 NSMutableArray<NSMutableArray<CollectionViewItem*>*>* _items;
30 }
31
32 @synthesize collectionViewController = _collectionViewController;
33
34 - (instancetype)init {
35 self = [super init];
36 if (self) {
37 _items = [NSMutableArray arrayWithCapacity:1];
38 for (NSInteger i = 0; i < 3; i++) {
39 NSMutableArray<CollectionViewItem*>* arrayToAdd =
40 [NSMutableArray arrayWithCapacity:3];
41 [arrayToAdd
42 addObject:[[SuggestionsArticleItem alloc]
43 initWithType:ItemTypeArticle
44 title:@"Title of an Article"
45 subtitle:@"This is the subtitle of an article, can "
46 @"spawn on multiple lines"
47 image:[UIImage
48 imageNamed:@"distillation_success"]]];
49 [arrayToAdd
50 addObject:[[SuggestionsItem alloc] initWithType:ItemTypeText
51 title:@"The title"
52 subtitle:@"The subtitle"]];
53 [arrayToAdd
54 addObject:[[SuggestionsExpandableItem alloc]
55 initWithType:ItemTypeExpand
56 title:@"Title of an Expandable Article"
57 subtitle:@"This Article can be expanded to display "
58 @"addition information or interaction "
59 @"options"
60 image:[UIImage imageNamed:@"distillation_fail"]
61 detailText:@"Details shown only when the article is "
62 @"expanded. It can be displayed on "
63 @"multiple lines."]];
64 [_items addObject:arrayToAdd];
65 }
66 }
67 return self;
68 }
69
70 #pragma mark - Properties
71
72 - (void)setCollectionViewController:
73 (CollectionViewController*)collectionViewController {
74 _collectionViewController = collectionViewController;
75 if (!collectionViewController) {
76 return;
77 }
78 [collectionViewController loadModel];
79 CollectionViewModel* model = collectionViewController.collectionViewModel;
80 NSInteger sectionIdentifier = kSectionIdentifierEnumZero;
81 for (NSMutableArray<CollectionViewItem*>* arrayWithItems in _items) {
82 [model addSectionWithIdentifier:sectionIdentifier];
83 for (CollectionViewItem* item in arrayWithItems) {
84 if ([item isKindOfClass:[SuggestionsExpandableItem class]]) {
85 SuggestionsExpandableItem* expandableItem =
86 base::mac::ObjCCastStrict<SuggestionsExpandableItem>(item);
87 expandableItem.collectionView = collectionViewController.collectionView;
88 }
89 [model addItem:item toSectionWithIdentifier:sectionIdentifier];
90 }
91 sectionIdentifier++;
92 }
93 }
94
95 #pragma mark - Public methods
96
97 - (void)addTextItem:(NSString*)title
98 subtitle:(NSString*)subtitle
99 toSection:(NSUInteger)section {
100 SuggestionsItem* item = [[SuggestionsItem alloc] initWithType:ItemTypeText
101 title:title
102 subtitle:subtitle];
103 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + section;
104 NSInteger sectionIndex = section;
105 if (section >= [_items count]) {
106 sectionIndex = [_items count];
107 sectionIdentifier = kSectionIdentifierEnumZero + section;
108 [_items addObject:[NSMutableArray arrayWithObject:item]];
109 if (_collectionViewController) {
110 [_collectionViewController.collectionView performBatchUpdates:^{
111 [_collectionViewController.collectionViewModel
112 addSectionWithIdentifier:sectionIdentifier];
113 [_collectionViewController.collectionView
114 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
115 }
116 completion:^(BOOL){
117 }];
118 }
119 }
120 [[_items objectAtIndex:section] addObject:item];
121 if (_collectionViewController) {
122 [_collectionViewController.collectionViewModel addItem:item
123 toSectionWithIdentifier:sectionIdentifier];
124 [_collectionViewController.collectionView performBatchUpdates:^{
125 [_collectionViewController.collectionView insertItemsAtIndexPaths:@[
126 [NSIndexPath indexPathForRow:[[_items objectAtIndex:section] count] - 1
127 inSection:section]
128 ]];
129 }
130 completion:^(BOOL){
131 }];
132 }
133 }
134
135 #pragma mark - SuggestionsDataSource
136
137 - (CGFloat)collectionView:(UICollectionView*)collectionView
138 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
139 CollectionViewItem* item =
140 [_collectionViewController.collectionViewModel itemAtIndexPath:indexPath];
141 UIEdgeInsets inset = [_collectionViewController
142 collectionView:collectionView
143 layout:collectionView.collectionViewLayout
144 insetForSectionAtIndex:indexPath.section];
145
146 return [MDCCollectionViewCell
147 cr_preferredHeightForWidth:CGRectGetWidth(
148 _collectionViewController.view.bounds) -
149 inset.left - inset.right
150 forItem:item];
151 }
152
153 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698