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 #pragma mark - SuggestionsExpandableItem | |
20 | |
21 @implementation SuggestionsExpandableItem { | |
22 NSString* _title; | |
23 NSString* _subtitle; | |
24 UIImage* _image; | |
25 NSString* _detail; | |
26 } | |
27 | |
28 @synthesize delegate = _delegate; | |
29 @synthesize expanded = _expanded; | |
30 | |
31 - (instancetype)initWithType:(NSInteger)type | |
32 title:(NSString*)title | |
33 subtitle:(NSString*)subtitle | |
34 image:(UIImage*)image | |
35 detailText:(NSString*)detail { | |
36 self = [super initWithType:type]; | |
37 if (self) { | |
38 self.cellClass = [SuggestionsExpandableCell class]; | |
39 _title = [title copy]; | |
40 _subtitle = [subtitle copy]; | |
41 _image = image; | |
42 _detail = [detail copy]; | |
43 } | |
44 return self; | |
45 } | |
46 | |
47 #pragma mark - CollectionViewItem | |
48 | |
49 - (void)configureCell:(SuggestionsExpandableCell*)cell { | |
50 [super configureCell:cell]; | |
51 cell.delegate = self.delegate; | |
52 cell.titleLabel.text = _title; | |
53 cell.subtitleLabel.text = _subtitle; | |
54 cell.imageView.image = _image; | |
55 cell.detailLabel.text = _detail; | |
56 if (self.expanded) | |
57 [cell expand]; | |
58 else | |
59 [cell retract]; | |
60 } | |
61 | |
62 @end | |
63 | |
64 #pragma mark - SuggestionsExpandableCell | |
65 | |
66 @implementation SuggestionsExpandableCell { | |
67 UIView* _articleContainer; | |
68 UIButton* _interactionButton; | |
69 UIButton* _expandButton; | |
70 BOOL _isExpanded; | |
marq (ping after 24h)
2017/01/16 17:16:41
Boolean vars shouldn't use 'is'; boolean getters s
gambard
2017/01/17 09:59:00
Done.
| |
71 } | |
72 | |
73 @synthesize titleLabel = _titleLabel; | |
74 @synthesize subtitleLabel = _subtitleLabel; | |
75 @synthesize detailLabel = _detailLabel; | |
76 @synthesize imageView = _imageView; | |
77 @synthesize delegate = _delegate; | |
78 | |
79 - (instancetype)initWithFrame:(CGRect)frame { | |
80 self = [super initWithFrame:frame]; | |
81 if (self) { | |
82 _isExpanded = NO; | |
83 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
84 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
85 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero]; | |
86 UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
87 _articleContainer = [[UIView alloc] initWithFrame:CGRectZero]; | |
88 _expandButton = [UIButton buttonWithType:UIButtonTypeSystem]; | |
89 _detailLabel = [[UILabel alloc] initWithFrame:CGRectZero]; | |
90 _interactionButton = [UIButton buttonWithType:UIButtonTypeSystem]; | |
91 | |
92 _subtitleLabel.numberOfLines = 0; | |
93 [_expandButton setTitle:@"See more" forState:UIControlStateNormal]; | |
94 _detailLabel.numberOfLines = 0; | |
95 [_interactionButton setTitle:@"Less interaction" | |
96 forState:UIControlStateNormal]; | |
97 | |
98 imageContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
99 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
100 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
101 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
102 _articleContainer.translatesAutoresizingMaskIntoConstraints = NO; | |
103 _expandButton.translatesAutoresizingMaskIntoConstraints = NO; | |
104 _detailLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
105 _interactionButton.translatesAutoresizingMaskIntoConstraints = NO; | |
106 | |
107 [_expandButton addTarget:self | |
marq (ping after 24h)
2017/01/16 17:16:41
Why not target:nil?
gambard
2017/01/17 09:59:00
Because it does not work.
| |
108 action:@selector(expandPressed) | |
marq (ping after 24h)
2017/01/16 17:16:41
I prefer action methods to not depend on a particu
gambard
2017/01/17 09:59:00
Done.
| |
109 forControlEvents:UIControlEventTouchUpInside]; | |
110 [_interactionButton addTarget:self | |
111 action:@selector(retractPressed) | |
112 forControlEvents:UIControlEventTouchUpInside]; | |
113 | |
114 [imageContainer addSubview:_imageView]; | |
marq (ping after 24h)
2017/01/16 17:16:41
Can you explain in comments why the imageContainer
gambard
2017/01/17 09:59:00
Done.
| |
115 [_articleContainer addSubview:imageContainer]; | |
116 [_articleContainer addSubview:_titleLabel]; | |
117 [_articleContainer addSubview:_subtitleLabel]; | |
118 | |
119 [self.contentView addSubview:_articleContainer]; | |
120 [self.contentView addSubview:_expandButton]; | |
121 | |
122 [NSLayoutConstraint activateConstraints:@[ | |
123 [self.contentView.centerXAnchor | |
124 constraintEqualToAnchor:_expandButton.centerXAnchor], | |
125 [_expandButton.topAnchor | |
126 constraintEqualToAnchor:_articleContainer.bottomAnchor], | |
127 [_expandButton.bottomAnchor | |
128 constraintEqualToAnchor:self.contentView.bottomAnchor] | |
129 ]]; | |
130 | |
131 ApplyVisualConstraintsWithMetrics( | |
132 @[ | |
133 @"H:|[container]|", @"H:|-[title]-[imageContainer(imageSize)]-|", | |
134 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]", | |
135 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|", | |
136 @"V:|-[imageContainer(>=imageSize)]-|", @"V:|[container]" | |
137 ], | |
138 @{ | |
139 @"image" : _imageView, | |
140 @"imageContainer" : imageContainer, | |
141 @"title" : _titleLabel, | |
142 @"text" : _subtitleLabel, | |
143 @"container" : _articleContainer | |
144 }, | |
145 @{ @"imageSize" : @(kImageSize) }); | |
146 } | |
147 return self; | |
148 } | |
149 | |
150 #pragma mark - Private | |
marq (ping after 24h)
2017/01/16 17:16:41
Comments on all private methods.
gambard
2017/01/17 09:59:00
Done.
| |
151 | |
152 - (void)expandPressed { | |
153 [self.delegate expandCell:self]; | |
marq (ping after 24h)
2017/01/16 17:16:41
Here's something to consider: If this instead went
gambard
2017/01/17 11:55:29
I don't like having to inspect all the cells to de
marq (ping after 24h)
2017/01/17 13:42:16
That's fine, although you won't have to inspect al
gambard
2017/01/17 14:46:56
Acknowledged.
| |
154 } | |
155 | |
156 - (void)retractPressed { | |
157 [self.delegate retractCell:self]; | |
158 } | |
159 | |
160 - (void)expand { | |
161 if (_isExpanded) | |
162 return; | |
163 _isExpanded = YES; | |
164 | |
165 [self.contentView addSubview:_detailLabel]; | |
166 [self.contentView addSubview:_interactionButton]; | |
167 [_expandButton removeFromSuperview]; | |
168 | |
169 [NSLayoutConstraint activateConstraints:@[ | |
marq (ping after 24h)
2017/01/16 17:16:41
This only works because _detailLabel and _interact
gambard
2017/01/17 09:59:00
Done.
I might have missread https://developer.appl
marq (ping after 24h)
2017/01/17 13:42:16
Looks like the guidelines have changed a bit, alth
gambard
2017/01/17 14:46:56
Acknowledged. :)
| |
170 [_detailLabel.topAnchor | |
171 constraintEqualToAnchor:_articleContainer.bottomAnchor], | |
172 [_detailLabel.bottomAnchor | |
173 constraintEqualToAnchor:_interactionButton.topAnchor], | |
174 [_interactionButton.bottomAnchor | |
175 constraintEqualToAnchor:self.contentView.bottomAnchor], | |
176 [_detailLabel.leadingAnchor | |
177 constraintEqualToAnchor:self.contentView.leadingAnchor], | |
178 [_detailLabel.trailingAnchor | |
179 constraintEqualToAnchor:self.contentView.trailingAnchor] | |
180 ]]; | |
181 } | |
182 | |
183 - (void)retract { | |
184 if (!_isExpanded) | |
185 return; | |
186 _isExpanded = NO; | |
187 | |
188 [_detailLabel removeFromSuperview]; | |
189 [_interactionButton removeFromSuperview]; | |
190 [self.contentView addSubview:_expandButton]; | |
191 | |
192 [NSLayoutConstraint activateConstraints:@[ | |
193 [self.contentView.centerXAnchor | |
194 constraintEqualToAnchor:_expandButton.centerXAnchor], | |
195 [_expandButton.topAnchor | |
196 constraintEqualToAnchor:_articleContainer.bottomAnchor], | |
197 [_expandButton.bottomAnchor | |
198 constraintEqualToAnchor:self.contentView.bottomAnchor] | |
199 ]]; | |
200 } | |
201 | |
202 #pragma mark - UIView | |
203 | |
204 // Implements -layoutSubviews as per instructions in documentation for | |
205 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
206 - (void)layoutSubviews { | |
207 [super layoutSubviews]; | |
208 | |
209 // Adjust the text label preferredMaxLayoutWidth when the parent's width | |
210 // changes, for instance on screen rotation. | |
211 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | |
212 _subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8; | |
213 _detailLabel.preferredMaxLayoutWidth = parentWidth; | |
214 | |
215 // Re-layout with the new preferred width to allow the label to adjust its | |
216 // height. | |
217 [super layoutSubviews]; | |
218 } | |
219 | |
220 @end | |
OLD | NEW |