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 #include "base/logging.h" | |
8 #include "base/mac/foundation_util.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_favicon_item.h" | |
14 #import "ios/chrome/browser/ui/suggestions/suggestions_item.h" | |
15 #import "ios/chrome/browser/ui/suggestions/suggestions_stack_item.h" | |
16 #import "ios/chrome/browser/ui/suggestions/suggestions_view_controller.h" | |
17 | |
18 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
19 #error "This file requires ARC support." | |
20 #endif | |
21 | |
22 @implementation SuggestionsCollectionUpdater | |
23 | |
24 @synthesize collectionViewController = _collectionViewController; | |
25 | |
26 #pragma mark - Properties | |
27 | |
28 - (void)setCollectionViewController: | |
29 (SuggestionsViewController*)collectionViewController { | |
30 _collectionViewController = collectionViewController; | |
31 [collectionViewController loadModel]; | |
32 CollectionViewModel* model = collectionViewController.collectionViewModel; | |
33 NSInteger sectionIdentifier = kSectionIdentifierEnumZero; | |
34 | |
35 // Stack Item. | |
36 [model addSectionWithIdentifier:sectionIdentifier]; | |
37 [model addItem:[[SuggestionsStackItem alloc] initWithType:ItemTypeStack | |
38 title:@"The title" | |
39 subtitle:@"The subtitle"] | |
40 toSectionWithIdentifier:sectionIdentifier++]; | |
41 | |
42 // Favicon Item. | |
43 [model addSectionWithIdentifier:sectionIdentifier]; | |
44 SuggestionsFaviconItem* faviconItem = | |
45 [[SuggestionsFaviconItem alloc] initWithType:ItemTypeFavicon]; | |
46 for (NSInteger i = 0; i < 6; i++) { | |
47 [faviconItem addFavicon:[UIImage imageNamed:@"bookmark_gray_star"] | |
48 withTitle:@"Super website! Incredible!"]; | |
49 } | |
50 faviconItem.delegate = _collectionViewController; | |
51 [model addItem:faviconItem toSectionWithIdentifier:sectionIdentifier++]; | |
52 | |
53 for (NSInteger i = 0; i < 3; i++) { | |
54 [model addSectionWithIdentifier:sectionIdentifier]; | |
55 | |
56 // Standard Item. | |
57 [model addItem:[[SuggestionsItem alloc] initWithType:ItemTypeText | |
58 title:@"The title" | |
59 subtitle:@"The subtitle"] | |
60 toSectionWithIdentifier:sectionIdentifier]; | |
61 | |
62 // Article Item. | |
63 [model addItem:[[SuggestionsArticleItem alloc] | |
64 initWithType:ItemTypeArticle | |
65 title:@"Title of an Article" | |
66 subtitle:@"This is the subtitle of an article, can " | |
67 @"spawn on multiple lines" | |
68 image:[UIImage | |
69 imageNamed:@"distillation_success"]] | |
70 toSectionWithIdentifier:sectionIdentifier]; | |
71 | |
72 // Expandable Item. | |
73 SuggestionsExpandableItem* expandableItem = | |
74 [[SuggestionsExpandableItem alloc] | |
75 initWithType:ItemTypeExpand | |
76 title:@"Title of an Expandable Article" | |
77 subtitle:@"This Article can be expanded to display " | |
78 @"additional information or interaction " | |
79 @"options" | |
80 image:[UIImage imageNamed:@"distillation_fail"] | |
81 detailText:@"Details shown only when the article is " | |
82 @"expanded. It can be displayed on " | |
83 @"multiple lines."]; | |
84 expandableItem.delegate = _collectionViewController; | |
85 [model addItem:expandableItem toSectionWithIdentifier:sectionIdentifier]; | |
86 sectionIdentifier++; | |
87 } | |
88 } | |
89 | |
90 #pragma mark - Public methods | |
91 | |
92 - (void)addTextItem:(NSString*)title | |
93 subtitle:(NSString*)subtitle | |
94 toSection:(NSInteger)inputSection { | |
95 DCHECK(_collectionViewController); | |
96 SuggestionsItem* item = [[SuggestionsItem alloc] initWithType:ItemTypeText | |
97 title:title | |
98 subtitle:subtitle]; | |
99 NSInteger sectionIdentifier = kSectionIdentifierEnumZero + inputSection; | |
100 NSInteger sectionIndex = inputSection; | |
101 CollectionViewModel* model = _collectionViewController.collectionViewModel; | |
102 if ([model numberOfSections] <= inputSection) { | |
103 sectionIndex = [model numberOfSections]; | |
104 sectionIdentifier = kSectionIdentifierEnumZero + sectionIndex; | |
105 [_collectionViewController.collectionView performBatchUpdates:^{ | |
106 [_collectionViewController.collectionViewModel | |
107 addSectionWithIdentifier:sectionIdentifier]; | |
108 [_collectionViewController.collectionView | |
109 insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]]; | |
110 } | |
111 completion:nil]; | |
112 } | |
113 NSInteger numberOfItemsInSection = | |
114 [model numberOfItemsInSection:sectionIndex]; | |
115 [_collectionViewController.collectionViewModel addItem:item | |
116 toSectionWithIdentifier:sectionIdentifier]; | |
117 [_collectionViewController.collectionView performBatchUpdates:^{ | |
118 [_collectionViewController.collectionView | |
119 insertItemsAtIndexPaths:@[ [NSIndexPath | |
120 indexPathForRow:numberOfItemsInSection | |
121 inSection:sectionIndex] ]]; | |
122 } | |
123 completion:nil]; | |
124 } | |
125 | |
126 - (BOOL)shouldUseCustomStyleForSection:(NSInteger)section { | |
127 return section == 0 || section == 1; | |
128 } | |
129 | |
130 @end | |
OLD | NEW |