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

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

Issue 2625693002: Suggestions UI - expandable item (Closed)
Patch Set: Add 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 }
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 }
34
35 @synthesize delegate = _delegate;
36 @synthesize expanded = _expanded;
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.delegate = self.delegate;
59 cell.titleLabel.text = _title;
60 cell.subtitleLabel.text = _subtitle;
61 cell.imageView.image = _image;
62 cell.detailLabel.text = _detail;
63 if (self.expanded)
64 [cell expand];
65 else
66 [cell retract];
67 }
68
69 @end
70
71 #pragma mark - SuggestionsExpandableCell
72
73 @implementation SuggestionsExpandableCell {
74 UIView* _articleContainer;
75 UIButton* _interactionButton;
76 UIButton* _expandButton;
77 BOOL _isExpanded;
78 }
79
80 @synthesize titleLabel = _titleLabel;
81 @synthesize subtitleLabel = _subtitleLabel;
82 @synthesize detailLabel = _detailLabel;
83 @synthesize imageView = _imageView;
84 @synthesize delegate = _delegate;
85
86 - (instancetype)initWithFrame:(CGRect)frame {
87 self = [super initWithFrame:frame];
88 if (self) {
89 _isExpanded = NO;
90 _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
91 _subtitleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
92 _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];
93 UIView* imageContainer = [[UIView alloc] initWithFrame:CGRectZero];
94 _articleContainer = [[UIView alloc] initWithFrame:CGRectZero];
95 _expandButton = [UIButton buttonWithType:UIButtonTypeSystem];
96 _detailLabel = [[UILabel alloc] initWithFrame:CGRectZero];
97 _interactionButton = [UIButton buttonWithType:UIButtonTypeSystem];
98
99 _subtitleLabel.numberOfLines = 0;
100 [_expandButton setTitle:@"See more" forState:UIControlStateNormal];
101 _detailLabel.numberOfLines = 0;
102 [_interactionButton setTitle:@"Less interaction"
103 forState:UIControlStateNormal];
104
105 imageContainer.translatesAutoresizingMaskIntoConstraints = NO;
106 _imageView.translatesAutoresizingMaskIntoConstraints = NO;
107 _titleLabel.translatesAutoresizingMaskIntoConstraints = NO;
108 _subtitleLabel.translatesAutoresizingMaskIntoConstraints = NO;
109 _articleContainer.translatesAutoresizingMaskIntoConstraints = NO;
110 _expandButton.translatesAutoresizingMaskIntoConstraints = NO;
111 _detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
112 _interactionButton.translatesAutoresizingMaskIntoConstraints = NO;
113
114 [_expandButton addTarget:self
115 action:@selector(expandPressed)
116 forControlEvents:UIControlEventTouchUpInside];
117 [_interactionButton addTarget:self
118 action:@selector(retractPressed)
119 forControlEvents:UIControlEventTouchUpInside];
120
121 [imageContainer addSubview:_imageView];
122 [_articleContainer addSubview:imageContainer];
123 [_articleContainer addSubview:_titleLabel];
124 [_articleContainer addSubview:_subtitleLabel];
125
126 [self.contentView addSubview:_articleContainer];
127 [self.contentView addSubview:_expandButton];
128
129 [NSLayoutConstraint activateConstraints:@[
130 [self.contentView.centerXAnchor
131 constraintEqualToAnchor:_expandButton.centerXAnchor],
132 [_expandButton.topAnchor
133 constraintEqualToAnchor:_articleContainer.bottomAnchor],
134 [_expandButton.bottomAnchor
135 constraintEqualToAnchor:self.contentView.bottomAnchor]
136 ]];
137
138 ApplyVisualConstraintsWithMetrics(
139 @[
140 @"H:|[container]|", @"H:|-[title]-[imageContainer(imageSize)]-|",
141 @"H:|[image(imageSize)]", @"H:|-[text]-[imageContainer]",
142 @"V:|[image(imageSize)]", @"V:|-[title]-[text]-|",
143 @"V:|-[imageContainer(>=imageSize)]-|", @"V:|[container]"
144 ],
145 @{
146 @"image" : _imageView,
147 @"imageContainer" : imageContainer,
148 @"title" : _titleLabel,
149 @"text" : _subtitleLabel,
150 @"container" : _articleContainer
151 },
152 @{ @"imageSize" : @(kImageSize) });
153 }
154 return self;
155 }
156
157 #pragma mark - Private
158
159 - (void)expandPressed {
160 [self.delegate expandCell:self];
161 }
162
163 - (void)retractPressed {
164 [self.delegate retractCell:self];
165 }
166
167 - (void)expand {
168 if (_isExpanded)
169 return;
170 _isExpanded = YES;
171
172 [self.contentView addSubview:_detailLabel];
173 [self.contentView addSubview:_interactionButton];
174 [_expandButton removeFromSuperview];
175
176 [NSLayoutConstraint activateConstraints:@[
177 [_detailLabel.topAnchor
178 constraintEqualToAnchor:_articleContainer.bottomAnchor],
179 [_detailLabel.bottomAnchor
180 constraintEqualToAnchor:_interactionButton.topAnchor],
181 [_interactionButton.bottomAnchor
182 constraintEqualToAnchor:self.contentView.bottomAnchor],
183 [_detailLabel.leadingAnchor
184 constraintEqualToAnchor:self.contentView.leadingAnchor],
185 [_detailLabel.trailingAnchor
186 constraintEqualToAnchor:self.contentView.trailingAnchor]
187 ]];
188 }
189
190 - (void)retract {
191 if (!_isExpanded)
192 return;
193 _isExpanded = NO;
194
195 [_detailLabel removeFromSuperview];
196 [_interactionButton removeFromSuperview];
197 [self.contentView addSubview:_expandButton];
198
199 [NSLayoutConstraint activateConstraints:@[
200 [self.contentView.centerXAnchor
201 constraintEqualToAnchor:_expandButton.centerXAnchor],
202 [_expandButton.topAnchor
203 constraintEqualToAnchor:_articleContainer.bottomAnchor],
204 [_expandButton.bottomAnchor
205 constraintEqualToAnchor:self.contentView.bottomAnchor]
206 ]];
207 }
208
209 #pragma mark - UIView
210
211 // Implements -layoutSubviews as per instructions in documentation for
212 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:].
213 - (void)layoutSubviews {
214 [super layoutSubviews];
215
216 // Adjust the text label preferredMaxLayoutWidth when the parent's width
217 // changes, for instance on screen rotation.
218 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds);
219 _subtitleLabel.preferredMaxLayoutWidth = parentWidth - kImageSize - 3 * 8;
220 _detailLabel.preferredMaxLayoutWidth = parentWidth;
221
222 // Re-layout with the new preferred width to allow the label to adjust its
223 // height.
224 [super layoutSubviews];
225 }
226
227 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698