Chromium Code Reviews| 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 typedef void (^ExpandableCellInteraction)(SuggestionsExpandableItem*, | |
| 20 SuggestionsExpandableCell*); | |
| 21 | |
| 18 @interface SuggestionsViewController ()<SuggestionsItemActions> | 22 @interface SuggestionsViewController ()<SuggestionsItemActions> |
| 19 | 23 |
| 20 @property(nonatomic, strong) SuggestionsCollectionUpdater* collectionUpdater; | 24 @property(nonatomic, strong) SuggestionsCollectionUpdater* collectionUpdater; |
| 21 | 25 |
| 26 // Applies |interaction| to the item corresponding to |cell| if it is a | |
| 27 // SuggestionsExpandableCell. | |
| 28 - (void)interact:(ExpandableCellInteraction)interaction | |
| 29 withCell:(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 - SuggestionsExpandableCellAudience | |
| 67 | |
| 68 - (void)retractCell:(UICollectionViewCell*)cell { | |
| 69 [self interact:^(SuggestionsExpandableItem* item, | |
|
lpromero
2017/01/13 17:35:48
See streamlined code in my comment above.
gambard
2017/01/16 09:50:39
Done.
| |
| 70 SuggestionsExpandableCell* expandableCell) { | |
| 71 [item retract:expandableCell]; | |
| 72 } | |
| 73 withCell:cell]; | |
| 74 } | |
| 75 | |
| 76 - (void)expandCell:(UICollectionViewCell*)cell { | |
| 77 [self interact:^(SuggestionsExpandableItem* item, | |
| 78 SuggestionsExpandableCell* expandableCell) { | |
| 79 [item expand:expandableCell]; | |
| 80 } | |
| 81 withCell:cell]; | |
| 82 } | |
| 83 | |
| 57 #pragma mark - SuggestionsItemActions | 84 #pragma mark - SuggestionsItemActions |
| 58 | 85 |
| 59 - (void)addNewItem:(id)sender { | 86 - (void)addNewItem:(id)sender { |
| 60 [self.suggestionCommandHandler addEmptyItem]; | 87 [self.suggestionCommandHandler addEmptyItem]; |
| 61 } | 88 } |
| 62 | 89 |
| 63 #pragma mark - SuggestionsCollectionUpdater forwarding | 90 #pragma mark - SuggestionsCollectionUpdater forwarding |
| 64 | 91 |
| 65 - (void)addTextItem:(NSString*)title | 92 - (void)addTextItem:(NSString*)title |
| 66 subtitle:(NSString*)subtitle | 93 subtitle:(NSString*)subtitle |
| 67 toSection:(NSInteger)inputSection { | 94 toSection:(NSInteger)inputSection { |
| 68 [self.collectionUpdater addTextItem:title | 95 [self.collectionUpdater addTextItem:title |
| 69 subtitle:subtitle | 96 subtitle:subtitle |
| 70 toSection:inputSection]; | 97 toSection:inputSection]; |
| 71 } | 98 } |
| 72 | 99 |
| 100 #pragma mark - Private | |
| 101 | |
| 102 - (void)interact:(ExpandableCellInteraction)interaction | |
| 103 withCell:(UICollectionViewCell*)cell { | |
| 104 if (!interaction) { | |
| 105 return; | |
| 106 } | |
| 107 NSIndexPath* indexPath = [self.collectionView indexPathForCell:cell]; | |
| 108 CollectionViewItem* item = | |
| 109 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
| 110 if ([item isKindOfClass:[SuggestionsExpandableItem class]]) { | |
| 111 SuggestionsExpandableItem* expandableItem = | |
| 112 base::mac::ObjCCast<SuggestionsExpandableItem>(item); | |
| 113 SuggestionsExpandableCell* expandableCell = | |
| 114 base::mac::ObjCCast<SuggestionsExpandableCell>(cell); | |
| 115 | |
| 116 interaction(expandableItem, expandableCell); | |
| 117 | |
| 118 [UIView | |
| 119 animateWithDuration:1 | |
| 120 animations:^{ | |
| 121 [self.collectionView.collectionViewLayout invalidateLayout]; | |
| 122 }]; | |
| 123 } | |
| 124 } | |
| 125 | |
| 73 @end | 126 @end |
| OLD | NEW |