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

Side by Side Diff: ios/chrome/browser/ui/suggestions/suggestions_expandable_item.mm

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

Powered by Google App Engine
This is Rietveld 408576698