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