Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(788)

Side by Side Diff: ios/chrome/browser/ui/content_suggestions/content_suggestions_expandable_item.mm

Issue 2761753002: Cleanup ContentSuggestions cells (Closed)
Patch Set: Fix tests Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/content_suggestions/content_suggestions_expandabl e_item.h"
6
7 #import "ios/chrome/browser/ui/uikit_ui_util.h"
8 #include "ui/base/l10n/l10n_util_mac.h"
9
10 #if !defined(__has_feature) || !__has_feature(objc_arc)
11 #error "This file requires ARC support."
12 #endif
13
14 namespace {
15 const CGFloat kImageSize = 80;
16 const CGFloat kStandardSpacing = 8;
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 = [ContentSuggestionsExpandableCell 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:(ContentSuggestionsExpandableCell*)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 collapse];
60 }
61
62 @end
63
64 #pragma mark - ContentSuggestionsExpandableCell
65
66 @interface ContentSuggestionsExpandableCell () {
67 UIView* _articleContainer;
68 UIButton* _interactionButton;
69 UIButton* _expandButton;
70 BOOL _expanded;
71 NSArray<NSLayoutConstraint*>* _expandedConstraints;
72 NSArray<NSLayoutConstraint*>* _collapsedConstraints;
73 }
74
75 // Callback for the UI action showing the details.
76 - (void)expand:(id)sender;
77 // Callback for the UI action hiding the details.
78 - (void)collapse:(id)sender;
79
80 @end
81
82 @implementation ContentSuggestionsExpandableCell
83
84 @synthesize titleLabel = _titleLabel;
85 @synthesize subtitleLabel = _subtitleLabel;
86 @synthesize detailLabel = _detailLabel;
87 @synthesize imageView = _imageView;
88 @synthesize delegate = _delegate;
89
90 - (instancetype)initWithFrame:(CGRect)frame {
91 self = [super initWithFrame:frame];
92 if (self) {
93 _expanded = NO;
94 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
95 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
96 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
97 _articleContainer = [[UIView alloc] initWithFrame:CGRectZero];
98 _expandButton = [UIButton buttonWithType:UIButtonTypeSystem];
99 _detailLabel = [[UILabel alloc] initWithFrame:CGRectZero];
100 _interactionButton = [UIButton buttonWithType:UIButtonTypeSystem];
101
102 _subtitleLabel.numberOfLines = 0;
103 [_expandButton setTitle:@"See more" forState:UIControlStateNormal];
104 _detailLabel.numberOfLines = 0;
105 [_interactionButton setTitle:@"Less interaction"
106 forState:UIControlStateNormal];
107
108 _imageView.translatesAutoresizingMaskIntoConstraints = NO;
109 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
110 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
111 _articleContainer.translatesAutoresizingMaskIntoConstraints = NO;
112 _expandButton.translatesAutoresizingMaskIntoConstraints = NO;
113 _detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
114 _interactionButton.translatesAutoresizingMaskIntoConstraints = NO;
115
116 [_expandButton addTarget:self
117 action:@selector(expand:)
118 forControlEvents:UIControlEventTouchUpInside];
119 [_interactionButton addTarget:self
120 action:@selector(collapse:)
121 forControlEvents:UIControlEventTouchUpInside];
122
123 [_articleContainer addSubview:_imageView];
124 [_articleContainer addSubview:_titleLabel];
125 [_articleContainer addSubview:_subtitleLabel];
126
127 [self.contentView addSubview:_articleContainer];
128 [self.contentView addSubview:_expandButton];
129
130 [NSLayoutConstraint activateConstraints:@[
131 [self.contentView.centerXAnchor
132 constraintEqualToAnchor:_expandButton.centerXAnchor],
133 [_expandButton.topAnchor
134 constraintEqualToAnchor:_articleContainer.bottomAnchor],
135 [_expandButton.bottomAnchor
136 constraintEqualToAnchor:self.contentView.bottomAnchor],
137 [_articleContainer.bottomAnchor
138 constraintGreaterThanOrEqualToAnchor:_imageView.bottomAnchor
139 constant:kStandardSpacing],
140 [_articleContainer.bottomAnchor
141 constraintGreaterThanOrEqualToAnchor:_subtitleLabel.bottomAnchor
142 constant:kStandardSpacing],
143 ]];
144
145 ApplyVisualConstraintsWithMetrics(
146 @[
147 @"H:|[container]|", @"H:|-[title]-[image(imageSize)]-|",
148 @"H:|-[text]-[image]", @"V:|-[image(imageSize)]",
149 @"V:|-[title]-[text]", @"V:|[container]"
150 ],
151 @{
152 @"image" : _imageView,
153 @"title" : _titleLabel,
154 @"text" : _subtitleLabel,
155 @"container" : _articleContainer
156 },
157 @{ @"imageSize" : @(kImageSize) });
158
159 _expandedConstraints = @[
160 [_detailLabel.topAnchor
161 constraintEqualToAnchor:_articleContainer.bottomAnchor],
162 [_detailLabel.bottomAnchor
163 constraintEqualToAnchor:_interactionButton.topAnchor],
164 [_interactionButton.bottomAnchor
165 constraintEqualToAnchor:self.contentView.bottomAnchor],
166 [_detailLabel.leadingAnchor
167 constraintEqualToAnchor:self.contentView.leadingAnchor],
168 [_detailLabel.trailingAnchor
169 constraintEqualToAnchor:self.contentView.trailingAnchor]
170 ];
171 _collapsedConstraints = @[
172 [self.contentView.centerXAnchor
173 constraintEqualToAnchor:_expandButton.centerXAnchor],
174 [_expandButton.topAnchor
175 constraintEqualToAnchor:_articleContainer.bottomAnchor],
176 [_expandButton.bottomAnchor
177 constraintEqualToAnchor:self.contentView.bottomAnchor]
178 ];
179 }
180 return self;
181 }
182
183 #pragma mark - Private
184
185 - (void)expand:(id)sender {
186 [self.delegate expandCell:self];
187 }
188
189 - (void)collapse:(id)sender {
190 [self.delegate collapseCell:self];
191 }
192
193 - (void)expand {
194 if (_expanded)
195 return;
196 _expanded = YES;
197
198 [self.contentView addSubview:_detailLabel];
199 [self.contentView addSubview:_interactionButton];
200 [_expandButton removeFromSuperview];
201
202 [NSLayoutConstraint deactivateConstraints:_collapsedConstraints];
203 [NSLayoutConstraint activateConstraints:_expandedConstraints];
204 }
205
206 - (void)collapse {
207 if (!_expanded)
208 return;
209 _expanded = NO;
210
211 [_detailLabel removeFromSuperview];
212 [_interactionButton removeFromSuperview];
213 [self.contentView addSubview:_expandButton];
214
215 [NSLayoutConstraint deactivateConstraints:_expandedConstraints];
216 [NSLayoutConstraint activateConstraints:_collapsedConstraints];
217 }
218
219 #pragma mark - UIView
220
221 // Implements -layoutSubviews as per instructions in documentation for
222 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
223 - (void)layoutSubviews {
224 [super layoutSubviews];
225
226 // Adjust the text label preferredMaxLayoutWidth when the parent's width
227 // changes, for instance on screen rotation.
228 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds);
229 _subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8;
230 _detailLabel.preferredMaxLayoutWidth = parentWidth;
231
232 // Re-layout with the new preferred width to allow the label to adjust its
233 // height.
234 [super layoutSubviews];
235 }
236
237 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698