OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/ui/suggestions/suggestions_view_controller.h" | 5 #import "ios/chrome/browser/ui/suggestions/suggestions_view_controller.h" |
6 | 6 |
| 7 #include "base/mac/foundation_util.h" |
7 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" | 8 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
8 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 9 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
9 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 10 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 11 #import "ios/chrome/browser/ui/suggestions/expandable_item.h" |
10 #import "ios/chrome/browser/ui/suggestions/suggestions_collection_updater.h" | 12 #import "ios/chrome/browser/ui/suggestions/suggestions_collection_updater.h" |
11 #import "ios/chrome/browser/ui/suggestions/suggestions_commands.h" | 13 #import "ios/chrome/browser/ui/suggestions/suggestions_commands.h" |
12 #import "ios/chrome/browser/ui/suggestions/suggestions_item_actions.h" | 14 #import "ios/chrome/browser/ui/suggestions/suggestions_item_actions.h" |
13 | 15 |
14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 16 #if !defined(__has_feature) || !__has_feature(objc_arc) |
15 #error "This file requires ARC support." | 17 #error "This file requires ARC support." |
16 #endif | 18 #endif |
17 | 19 |
| 20 namespace { |
| 21 const NSTimeInterval kAnimationDuration = 0.35; |
| 22 } // namespace |
| 23 |
18 @interface SuggestionsViewController ()<SuggestionsItemActions> | 24 @interface SuggestionsViewController ()<SuggestionsItemActions> |
19 | 25 |
20 @property(nonatomic, strong) SuggestionsCollectionUpdater* collectionUpdater; | 26 @property(nonatomic, strong) SuggestionsCollectionUpdater* collectionUpdater; |
21 | 27 |
| 28 // Expand or collapse the |cell|, if it is a SuggestionsExpandableCell, |
| 29 // according to |expand|. |
| 30 - (void)expand:(BOOL)expand cell:(UICollectionViewCell*)cell; |
| 31 |
22 @end | 32 @end |
23 | 33 |
24 @implementation SuggestionsViewController | 34 @implementation SuggestionsViewController |
25 | 35 |
26 @synthesize suggestionCommandHandler = _suggestionCommandHandler; | 36 @synthesize suggestionCommandHandler = _suggestionCommandHandler; |
27 @synthesize collectionUpdater = _collectionUpdater; | 37 @synthesize collectionUpdater = _collectionUpdater; |
28 | 38 |
29 #pragma mark - UIViewController | 39 #pragma mark - UIViewController |
30 | 40 |
31 - (void)viewDidLoad { | 41 - (void)viewDidLoad { |
(...skipping 15 matching lines...) Expand all Loading... |
47 UIEdgeInsets inset = [self collectionView:collectionView | 57 UIEdgeInsets inset = [self collectionView:collectionView |
48 layout:collectionView.collectionViewLayout | 58 layout:collectionView.collectionViewLayout |
49 insetForSectionAtIndex:indexPath.section]; | 59 insetForSectionAtIndex:indexPath.section]; |
50 | 60 |
51 return [MDCCollectionViewCell | 61 return [MDCCollectionViewCell |
52 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) - | 62 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) - |
53 inset.left - inset.right | 63 inset.left - inset.right |
54 forItem:item]; | 64 forItem:item]; |
55 } | 65 } |
56 | 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 |
57 #pragma mark - SuggestionsItemActions | 77 #pragma mark - SuggestionsItemActions |
58 | 78 |
59 - (void)addNewItem:(id)sender { | 79 - (void)addNewItem:(id)sender { |
60 [self.suggestionCommandHandler addEmptyItem]; | 80 [self.suggestionCommandHandler addEmptyItem]; |
61 } | 81 } |
62 | 82 |
63 #pragma mark - SuggestionsCollectionUpdater forwarding | 83 #pragma mark - SuggestionsCollectionUpdater forwarding |
64 | 84 |
65 - (void)addTextItem:(NSString*)title | 85 - (void)addTextItem:(NSString*)title |
66 subtitle:(NSString*)subtitle | 86 subtitle:(NSString*)subtitle |
67 toSection:(NSInteger)inputSection { | 87 toSection:(NSInteger)inputSection { |
68 [self.collectionUpdater addTextItem:title | 88 [self.collectionUpdater addTextItem:title |
69 subtitle:subtitle | 89 subtitle:subtitle |
70 toSection:inputSection]; | 90 toSection:inputSection]; |
71 } | 91 } |
72 | 92 |
| 93 #pragma mark - Private |
| 94 |
| 95 - (void)expand:(BOOL)expand cell:(UICollectionViewCell*)cell { |
| 96 NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell]; |
| 97 CollectionViewItem* item = |
| 98 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 99 if ([item conformsToProtocol:@protocol(SuggestionsExpandableArticle)]) { |
| 100 id<SuggestionsExpandableArticle> expandableItem = |
| 101 (id<SuggestionsExpandableArticle>)item; |
| 102 |
| 103 NSInteger sectionIdentifier = [self.collectionViewModel |
| 104 sectionIdentifierForSection:indexPath.section]; |
| 105 |
| 106 expandableItem.expanded = expand; |
| 107 [self reconfigureCellsForItems:@[ item ] |
| 108 inSectionWithIdentifier:sectionIdentifier]; |
| 109 |
| 110 [UIView |
| 111 animateWithDuration:kAnimationDuration |
| 112 animations:^{ |
| 113 [self.collectionView.collectionViewLayout invalidateLayout]; |
| 114 }]; |
| 115 } |
| 116 } |
| 117 |
73 @end | 118 @end |
OLD | NEW |