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_view_controller.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/cells/collection_view_item.h" | |
10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | |
11 #import "ios/chrome/browser/ui/suggestions/expandable_item.h" | |
12 #import "ios/chrome/browser/ui/suggestions/suggestions_collection_updater.h" | |
13 #import "ios/chrome/browser/ui/suggestions/suggestions_commands.h" | |
14 #import "ios/chrome/browser/ui/suggestions/suggestions_item_actions.h" | |
15 #import "ios/chrome/browser/ui/suggestions/suggestions_stack_item.h" | |
16 #import "ios/chrome/browser/ui/suggestions/suggestions_stack_item_actions.h" | |
17 | |
18 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
19 #error "This file requires ARC support." | |
20 #endif | |
21 | |
22 namespace { | |
23 const NSTimeInterval kAnimationDuration = 0.35; | |
24 } // namespace | |
25 | |
26 @interface SuggestionsViewController ()<SuggestionsItemActions, | |
27 SuggestionsStackItemActions> | |
28 | |
29 @property(nonatomic, strong) SuggestionsCollectionUpdater* collectionUpdater; | |
30 | |
31 // Expand or collapse the |cell|, if it is a SuggestionsExpandableCell, | |
32 // according to |expand|. | |
33 - (void)expand:(BOOL)expand cell:(UICollectionViewCell*)cell; | |
34 | |
35 @end | |
36 | |
37 @implementation SuggestionsViewController | |
38 | |
39 @synthesize suggestionCommandHandler = _suggestionCommandHandler; | |
40 @synthesize collectionUpdater = _collectionUpdater; | |
41 | |
42 #pragma mark - UIViewController | |
43 | |
44 - (void)viewDidLoad { | |
45 [super viewDidLoad]; | |
46 | |
47 _collectionUpdater = [[SuggestionsCollectionUpdater alloc] init]; | |
48 _collectionUpdater.collectionViewController = self; | |
49 | |
50 self.collectionView.delegate = self; | |
51 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | |
52 } | |
53 | |
54 #pragma mark - UICollectionViewDelegate | |
55 | |
56 - (void)collectionView:(UICollectionView*)collectionView | |
57 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
58 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | |
59 | |
60 CollectionViewItem* item = | |
61 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
62 if (item.type == ItemTypeStack) { | |
63 [self.suggestionCommandHandler openReadingList]; | |
64 } | |
65 } | |
66 | |
67 #pragma mark - SuggestionsExpandableCellDelegate | |
68 | |
69 - (void)collapseCell:(UICollectionViewCell*)cell { | |
70 [self expand:NO cell:cell]; | |
71 } | |
72 | |
73 - (void)expandCell:(UICollectionViewCell*)cell { | |
74 [self expand:YES cell:cell]; | |
75 } | |
76 | |
77 #pragma mark - SuggestionsFaviconCellDelegate | |
78 | |
79 - (void)openFaviconAtIndexPath:(NSIndexPath*)innerIndexPath { | |
80 [self.suggestionCommandHandler openFaviconAtIndex:innerIndexPath.item]; | |
81 } | |
82 | |
83 #pragma mark - SuggestionsItemActions | |
84 | |
85 - (void)addNewItem:(id)sender { | |
86 [self.suggestionCommandHandler addEmptyItem]; | |
87 } | |
88 | |
89 #pragma mark - SuggestionsCollectionUpdater forwarding | |
90 | |
91 - (void)addTextItem:(NSString*)title | |
92 subtitle:(NSString*)subtitle | |
93 toSection:(NSInteger)inputSection { | |
94 [self.collectionUpdater addTextItem:title | |
95 subtitle:subtitle | |
96 toSection:inputSection]; | |
97 } | |
98 | |
99 #pragma mark - SuggestionsStackItemActions | |
100 | |
101 - (void)openReadingListFirstItem:(id)sender { | |
102 [self.suggestionCommandHandler openFirstPageOfReadingList]; | |
103 } | |
104 | |
105 #pragma mark - MDCCollectionViewStylingDelegate | |
106 | |
107 - (UIColor*)collectionView:(nonnull UICollectionView*)collectionView | |
108 cellBackgroundColorAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
109 if ([self.collectionUpdater | |
110 shouldUseCustomStyleForSection:indexPath.section]) { | |
111 return [UIColor clearColor]; | |
112 } | |
113 return [UIColor whiteColor]; | |
114 } | |
115 | |
116 - (BOOL)collectionView:(nonnull UICollectionView*)collectionView | |
117 shouldHideItemBackgroundAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
118 if ([self.collectionUpdater | |
119 shouldUseCustomStyleForSection:indexPath.section]) { | |
120 return YES; | |
121 } | |
122 return NO; | |
123 } | |
124 | |
125 - (CGFloat)collectionView:(UICollectionView*)collectionView | |
126 cellHeightAtIndexPath:(NSIndexPath*)indexPath { | |
127 CollectionViewItem* item = | |
128 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
129 UIEdgeInsets inset = [self collectionView:collectionView | |
130 layout:collectionView.collectionViewLayout | |
131 insetForSectionAtIndex:indexPath.section]; | |
132 | |
133 return [MDCCollectionViewCell | |
134 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) - | |
135 inset.left - inset.right | |
136 forItem:item]; | |
137 } | |
138 | |
139 #pragma mark - Private | |
140 | |
141 - (void)expand:(BOOL)expand cell:(UICollectionViewCell*)cell { | |
142 NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell]; | |
143 CollectionViewItem* item = | |
144 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
145 if ([item conformsToProtocol:@protocol(SuggestionsExpandableArticle)]) { | |
146 id<SuggestionsExpandableArticle> expandableItem = | |
147 (id<SuggestionsExpandableArticle>)item; | |
148 | |
149 NSInteger sectionIdentifier = [self.collectionViewModel | |
150 sectionIdentifierForSection:indexPath.section]; | |
151 | |
152 expandableItem.expanded = expand; | |
153 [self reconfigureCellsForItems:@[ item ] | |
154 inSectionWithIdentifier:sectionIdentifier]; | |
155 | |
156 [UIView | |
157 animateWithDuration:kAnimationDuration | |
158 animations:^{ | |
159 [self.collectionView.collectionViewLayout invalidateLayout]; | |
160 }]; | |
161 } | |
162 } | |
163 | |
164 @end | |
OLD | NEW |