Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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_expandable_item.h" | |
| 6 | |
| 7 #import "ios/chrome/browser/ui/uikit_ui_util.h" | |
| 8 #include "ios/chrome/grit/ios_strings.h" | |
| 9 #include "ui/base/l10n/l10n_util_mac.h" | |
| 10 | |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 12 #error "This file requires ARC support." | |
| 13 #endif | |
| 14 | |
| 15 namespace { | |
| 16 const CGFloat kImageSize = 80; | |
| 17 } | |
| 18 | |
| 19 @interface SuggestionsExpandableItem () | |
| 20 | |
| 21 - (void)expand:(SuggestionsExpandableCell*)cell; | |
| 22 - (void)retract:(SuggestionsExpandableCell*)cell; | |
| 23 | |
| 24 @end | |
| 25 | |
| 26 @interface SuggestionsExpandableCell () | |
| 27 | |
| 28 - (void)expand; | |
| 29 - (void)retract; | |
| 30 | |
| 31 @end | |
| 32 | |
| 33 #pragma mark - SuggestionsExpandableItem | |
| 34 | |
| 35 @implementation SuggestionsExpandableItem { | |
| 36 NSString* _title; | |
| 37 NSString* _subtitle; | |
| 38 UIImage* _image; | |
| 39 NSString* _detail; | |
| 40 BOOL isExpanded; | |
|
lpromero
2017/01/12 16:44:39
s/isExpanded/_isExpanded.
gambard
2017/01/13 16:41:52
Done.
| |
| 41 } | |
| 42 | |
| 43 @synthesize collectionView = _collectionView; | |
| 44 | |
| 45 - (instancetype)initWithType:(NSInteger)type | |
| 46 title:(NSString*)title | |
| 47 subtitle:(NSString*)subtitle | |
| 48 image:(UIImage*)image | |
| 49 detailText:(NSString*)detail { | |
| 50 self = [super initWithType:type]; | |
| 51 if (self) { | |
| 52 self.cellClass = [SuggestionsExpandableCell class]; | |
| 53 _title = [title copy]; | |
| 54 _subtitle = [subtitle copy]; | |
| 55 _image = image; | |
| 56 _detail = [detail copy]; | |
| 57 } | |
| 58 return self; | |
| 59 } | |
| 60 | |
| 61 #pragma mark - CollectionViewItem | |
| 62 | |
| 63 - (void)configureCell:(SuggestionsExpandableCell*)cell { | |
| 64 [super configureCell:cell]; | |
| 65 cell.item = self; | |
| 66 cell.titleLabel.text = _title; | |
| 67 cell.subtitleLabel.text = _subtitle; | |
| 68 cell.imageView.image = _image; | |
| 69 cell.detailLabel.text = _detail; | |
| 70 if (isExpanded) | |
| 71 [cell expand]; | |
| 72 else | |
| 73 [cell retract]; | |
| 74 } | |
| 75 | |
| 76 #pragma mark - Private | |
| 77 | |
| 78 - (void)expand:(SuggestionsExpandableCell*)cell { | |
| 79 [cell expand]; | |
| 80 isExpanded = YES; | |
| 81 [UIView animateWithDuration:1 | |
| 82 animations:^{ | |
| 83 [_collectionView.collectionViewLayout invalidateLayout]; | |
| 84 }]; | |
| 85 } | |
| 86 | |
| 87 - (void)retract:(SuggestionsExpandableCell*)cell { | |
| 88 [cell retract]; | |
| 89 isExpanded = NO; | |
| 90 [UIView animateWithDuration:1 | |
| 91 animations:^{ | |
| 92 [_collectionView.collectionViewLayout invalidateLayout]; | |
| 93 }]; | |
| 94 } | |
| 95 | |
| 96 @end | |
| 97 | |
| 98 #pragma mark - SuggestionsExpandableCell | |
| 99 | |
| 100 @implementation SuggestionsExpandableCell { | |
| 101 UIView* _articleContainer; | |
| 102 UIButton* _interactionButton; | |
| 103 UIButton* _expandButton; | |
| 104 BOOL _isExpanded; | |
| 105 } | |
| 106 | |
| 107 @synthesize item = _item; | |
| 108 @synthesize titleLabel = _titleLabel; | |
| 109 @synthesize subtitleLabel = _subtitleLabel; | |
| 110 @synthesize detailLabel = _detailLabel; | |
| 111 @synthesize imageView = _imageView; | |
| 112 | |
| 113 - (instancetype)initWithFrame:(CGRect)frame { | |
| 114 self = [super initWithFrame:frame]; | |
| 115 if (self) { | |
| 116 _isExpanded = NO; | |
| 117 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
| 118 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
| 119 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
| 120 UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
| 121 _articleContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
| 122 _expandButton = [UIButton buttonWithType:UIButtonTypeSystem]; | |
| 123 _detailLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
| 124 _interactionButton = [UIButton buttonWithType:UIButtonTypeSystem]; | |
| 125 | |
| 126 _subtitleLabel.numberOfLines = 0; | |
| 127 [_expandButton setTitle:@"See more" forState:UIControlStateNormal]; | |
| 128 _detailLabel.numberOfLines = 0; | |
| 129 [_interactionButton setTitle:@"Less interaction" | |
| 130 forState:UIControlStateNormal]; | |
| 131 | |
| 132 imageContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
| 133 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
| 134 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
| 135 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
| 136 _articleContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
| 137 _expandButton.translatesAutoresizingMaskIntoConstraints = NO; | |
| 138 _detailLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
| 139 _interactionButton.translatesAutoresizingMaskIntoConstraints = NO; | |
| 140 | |
| 141 [_expandButton addTarget:self | |
| 142 action:@selector(expandPressed) | |
| 143 forControlEvents:UIControlEventTouchUpInside]; | |
| 144 [_interactionButton addTarget:self | |
| 145 action:@selector(retractPressed) | |
| 146 forControlEvents:UIControlEventTouchUpInside]; | |
| 147 | |
| 148 [imageContainer addSubview:_imageView]; | |
| 149 [_articleContainer addSubview:imageContainer]; | |
| 150 [_articleContainer addSubview:_titleLabel]; | |
| 151 [_articleContainer addSubview:_subtitleLabel]; | |
| 152 | |
| 153 [self.contentView addSubview:_articleContainer]; | |
| 154 [self.contentView addSubview:_expandButton]; | |
| 155 | |
| 156 [NSLayoutConstraint activateConstraints:@[ | |
| 157 [self.contentView.centerXAnchor | |
| 158 constraintEqualToAnchor:_expandButton.centerXAnchor], | |
| 159 [_expandButton.topAnchor | |
| 160 constraintEqualToAnchor:_articleContainer.bottomAnchor], | |
| 161 [_expandButton.bottomAnchor | |
| 162 constraintEqualToAnchor:self.contentView.bottomAnchor] | |
| 163 ]]; | |
| 164 | |
| 165 ApplyVisualConstraintsWithMetrics( | |
| 166 @[ | |
| 167 @"H:|[container]|", @"H:|-[title]-[imageContainer(imageSize)]-|", | |
| 168 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", | |
| 169 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", | |
| 170 @"V:|-[imageContainer(>=imageSize)]-|", @"V:|[container]" | |
| 171 ], | |
| 172 @{ | |
| 173 @"image" : _imageView, | |
| 174 @"imageContainer" : imageContainer, | |
| 175 @"title" : _titleLabel, | |
| 176 @"text" : _subtitleLabel, | |
| 177 @"container" : _articleContainer | |
| 178 }, | |
| 179 @{ @"imageSize" : @(kImageSize) }); | |
| 180 } | |
| 181 return self; | |
| 182 } | |
| 183 | |
| 184 #pragma mark - Private | |
| 185 | |
| 186 - (void)expandPressed { | |
| 187 [self.item expand:self]; | |
| 188 } | |
| 189 | |
| 190 - (void)retractPressed { | |
| 191 [self.item retract:self]; | |
| 192 } | |
| 193 | |
| 194 - (void)expand { | |
| 195 if (_isExpanded) | |
| 196 return; | |
| 197 _isExpanded = YES; | |
| 198 | |
| 199 [self.contentView addSubview:_detailLabel]; | |
| 200 [self.contentView addSubview:_interactionButton]; | |
| 201 [_expandButton removeFromSuperview]; | |
| 202 | |
| 203 [NSLayoutConstraint activateConstraints:@[ | |
| 204 [_detailLabel.topAnchor | |
| 205 constraintEqualToAnchor:_articleContainer.bottomAnchor], | |
| 206 [_detailLabel.bottomAnchor | |
| 207 constraintEqualToAnchor:_interactionButton.topAnchor], | |
| 208 [_interactionButton.bottomAnchor | |
| 209 constraintEqualToAnchor:self.contentView.bottomAnchor], | |
| 210 [_detailLabel.leadingAnchor | |
| 211 constraintEqualToAnchor:self.contentView.leadingAnchor], | |
| 212 [_detailLabel.trailingAnchor | |
| 213 constraintEqualToAnchor:self.contentView.trailingAnchor] | |
| 214 ]]; | |
| 215 } | |
| 216 | |
| 217 - (void)retract { | |
| 218 if (!_isExpanded) | |
| 219 return; | |
| 220 _isExpanded = NO; | |
| 221 | |
| 222 [_detailLabel removeFromSuperview]; | |
| 223 [_interactionButton removeFromSuperview]; | |
| 224 [self.contentView addSubview:_expandButton]; | |
| 225 | |
| 226 [NSLayoutConstraint activateConstraints:@[ | |
| 227 [self.contentView.centerXAnchor | |
| 228 constraintEqualToAnchor:_expandButton.centerXAnchor], | |
| 229 [_expandButton.topAnchor | |
| 230 constraintEqualToAnchor:_articleContainer.bottomAnchor], | |
| 231 [_expandButton.bottomAnchor | |
| 232 constraintEqualToAnchor:self.contentView.bottomAnchor] | |
| 233 ]]; | |
| 234 } | |
| 235 | |
| 236 #pragma mark - UIView | |
| 237 | |
| 238 // Implements -layoutSubviews as per instructions in documentation for | |
| 239 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
| 240 - (void)layoutSubviews { | |
| 241 [super layoutSubviews]; | |
| 242 | |
| 243 // Adjust the text label preferredMaxLayoutWidth when the parent's width | |
| 244 // changes, for instance on screen rotation. | |
| 245 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | |
| 246 _subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | |
| 247 _detailLabel.preferredMaxLayoutWidth = parentWidth; | |
| 248 | |
| 249 // Re-layout with the new preferred width to allow the label to adjust its | |
| 250 // height. | |
| 251 [super layoutSubviews]; | |
| 252 } | |
| 253 | |
| 254 @end | |
| OLD | NEW |