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