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