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