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 - SuggestionsItemActions | |
78 | |
79 - (void)addNewItem:(id)sender { | |
80 [self.suggestionCommandHandler addEmptyItem]; | |
81 } | |
82 | |
83 #pragma mark - SuggestionsCollectionUpdater forwarding | |
84 | |
85 - (void)addTextItem:(NSString*)title | |
86 subtitle:(NSString*)subtitle | |
87 toSection:(NSInteger)inputSection { | |
88 [self.collectionUpdater addTextItem:title | |
89 subtitle:subtitle | |
90 toSection:inputSection]; | |
91 } | |
92 | |
93 #pragma mark - SuggestionsStackItemActions | |
94 | |
95 - (void)openReadingListFirstItem:(id)sender { | |
96 [self.suggestionCommandHandler openFirstPageOfReadingList]; | |
97 } | |
98 | |
99 #pragma mark - MDCCollectionViewStylingDelegate | |
100 | |
101 - (UIColor*)collectionView:(nonnull UICollectionView*)collectionView | |
102 cellBackgroundColorAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
103 if ([self.collectionUpdater | |
104 shouldUseCustomStyleForSection:indexPath.section]) { | |
105 return [UIColor clearColor]; | |
106 } | |
107 return [UIColor whiteColor]; | |
108 } | |
109 | |
110 - (BOOL)collectionView:(nonnull UICollectionView*)collectionView | |
111 shouldHideItemBackgroundAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
112 if ([self.collectionUpdater | |
113 shouldUseCustomStyleForSection:indexPath.section]) { | |
114 return YES; | |
115 } | |
116 return NO; | |
117 } | |
118 | |
119 - (CGFloat)collectionView:(UICollectionView*)collectionView | |
120 cellHeightAtIndexPath:(NSIndexPath*)indexPath { | |
121 CollectionViewItem* item = | |
122 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
123 UIEdgeInsets inset = [self collectionView:collectionView | |
124 layout:collectionView.collectionViewLayout | |
125 insetForSectionAtIndex:indexPath.section]; | |
126 | |
127 return [MDCCollectionViewCell | |
128 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) - | |
129 inset.left - inset.right | |
130 forItem:item]; | |
131 } | |
132 | |
133 #pragma mark - Private | |
134 | |
135 - (void)expand:(BOOL)expand cell:(UICollectionViewCell*)cell { | |
136 NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell]; | |
137 CollectionViewItem* item = | |
138 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
139 if ([item conformsToProtocol:@protocol(SuggestionsExpandableArticle)]) { | |
140 id<SuggestionsExpandableArticle> expandableItem = | |
141 (id<SuggestionsExpandableArticle>)item; | |
142 | |
143 NSInteger sectionIdentifier = [self.collectionViewModel | |
144 sectionIdentifierForSection:indexPath.section]; | |
145 | |
146 expandableItem.expanded = expand; | |
147 [self reconfigureCellsForItems:@[ item ] | |
148 inSectionWithIdentifier:sectionIdentifier]; | |
149 | |
150 [UIView | |
151 animateWithDuration:kAnimationDuration | |
152 animations:^{ | |
153 [self.collectionView.collectionViewLayout invalidateLayout]; | |
154 }]; | |
155 } | |
156 } | |
157 | |
158 @end | |
OLD | NEW |