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

Side by Side Diff: ios/chrome/browser/ui/collection_view/cells/collection_view_account_item.mm

Issue 2588713002: Upstream Chrome on iOS source code [4/11]. (Closed)
Patch Set: Created 4 years 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 2016 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/collection_view/cells/collection_view_account_ite m.h"
6
7 #include "base/mac/foundation_util.h"
8 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
9 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h"
10 #include "ui/base/l10n/l10n_util.h"
11
12 #if !defined(__has_feature) || !__has_feature(objc_arc)
13 #error "This file requires ARC support."
14 #endif
15
16 namespace {
17 // Padding used on the leading and trailing edges of the cell.
18 const CGFloat kHorizontalPadding = 16;
19
20 // Padding used between the image and text.
21 const CGFloat kHorizontalPaddingBetweenImageAndText = 10;
22
23 // Padding used between the text and error icon.
24 const CGFloat kHorizontalPaddingBetweenTextAndError = 5;
25
26 // Image fixed horizontal size.
27 const CGFloat kHorizontalImageFixedSize = 40;
28
29 // Error icon fixed horizontal size.
30 const CGFloat kHorizontalErrorIconFixedSize = 25;
31 }
32
33 @implementation CollectionViewAccountItem
34
35 @synthesize image = _image;
36 @synthesize text = _text;
37 @synthesize detailText = _detailText;
38 @synthesize accessoryType = _accessoryType;
39 @synthesize shouldDisplayError = _shouldDisplayError;
40 @synthesize chromeIdentity = _chromeIdentity;
41 @synthesize enabled = _enabled;
42
43 - (instancetype)initWithType:(NSInteger)type {
44 self = [super initWithType:type];
45 if (self) {
46 self.cellClass = [CollectionViewAccountCell class];
47 self.accessibilityTraits |= UIAccessibilityTraitButton;
48 self.enabled = YES;
49 }
50 return self;
51 }
52
53 #pragma mark - CollectionViewItem
54
55 - (void)configureCell:(CollectionViewAccountCell*)cell {
56 [super configureCell:cell];
57 cell.imageView.image = self.image;
58 cell.textLabel.text = self.text;
59 cell.detailTextLabel.text = self.detailText;
60 cell.accessoryType = self.accessoryType;
61 if (self.shouldDisplayError) {
62 cell.errorIcon.image = [UIImage imageNamed:@"settings_error"];
63 cell.detailTextLabel.textColor = [[MDCPalette cr_redPalette] tint500];
64 } else {
65 cell.errorIcon.image = nil;
66 cell.detailTextLabel.textColor = [[MDCPalette greyPalette] tint500];
67 }
68
69 if (self.isEnabled) {
70 cell.userInteractionEnabled = YES;
71 cell.contentView.alpha = 1;
72 UIImageView* accessoryImage =
73 base::mac::ObjCCastStrict<UIImageView>(cell.accessoryView);
74 accessoryImage.tintColor =
75 [accessoryImage.tintColor colorWithAlphaComponent:1];
76 } else {
77 cell.userInteractionEnabled = NO;
78 cell.contentView.alpha = 0.5;
79 UIImageView* accessoryImage =
80 base::mac::ObjCCastStrict<UIImageView>(cell.accessoryView);
81 accessoryImage.tintColor =
82 [accessoryImage.tintColor colorWithAlphaComponent:0.5];
83 }
84 }
85
86 @end
87
88 @interface CollectionViewAccountCell () {
89 // Constraint used to set padding between image and text when image exists.
90 NSLayoutConstraint* _textLeadingAnchorConstraint;
91
92 // Constraint used to set the errorIcon width depending on it's existence.
93 NSLayoutConstraint* _errorIconWidthConstraint;
94 }
95 @end
96
97 @implementation CollectionViewAccountCell
98
99 @synthesize imageView = _imageView;
100 @synthesize textLabel = _textLabel;
101 @synthesize detailTextLabel = _detailTextLabel;
102 @synthesize errorIcon = _errorIcon;
103
104 - (instancetype)initWithFrame:(CGRect)frame {
105 self = [super initWithFrame:frame];
106 if (self) {
107 self.isAccessibilityElement = YES;
108 [self addSubviews];
109 [self setDefaultViewStyling];
110 [self setViewConstraints];
111 }
112 return self;
113 }
114
115 // Create and add subviews.
116 - (void)addSubviews {
117 UIView* contentView = self.contentView;
118 contentView.clipsToBounds = YES;
119
120 _imageView = [[UIImageView alloc] init];
121 _imageView.translatesAutoresizingMaskIntoConstraints = NO;
122 [contentView addSubview:_imageView];
123
124 _errorIcon = [[UIImageView alloc] init];
125 _errorIcon.translatesAutoresizingMaskIntoConstraints = NO;
126 [contentView addSubview:_errorIcon];
127
128 _textLabel = [[UILabel alloc] init];
129 _textLabel.translatesAutoresizingMaskIntoConstraints = NO;
130 [contentView addSubview:_textLabel];
131
132 _detailTextLabel = [[UILabel alloc] init];
133 _detailTextLabel.translatesAutoresizingMaskIntoConstraints = NO;
134 [contentView addSubview:_detailTextLabel];
135 }
136
137 // Set default imageView styling and default font and text colors for labels.
138 - (void)setDefaultViewStyling {
139 _imageView.contentMode = UIViewContentModeCenter;
140 _imageView.layer.masksToBounds = YES;
141
142 _textLabel.font = [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14];
143 _textLabel.textColor = [[MDCPalette greyPalette] tint900];
144 _detailTextLabel.font =
145 [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:14];
146 _detailTextLabel.textColor = [[MDCPalette greyPalette] tint500];
147 _imageView.contentMode = UIViewContentModeScaleAspectFit;
148 }
149
150 // Set constraints on subviews.
151 - (void)setViewConstraints {
152 UIView* contentView = self.contentView;
153
154 // This view is used to center the two leading textLabels.
155 UIView* verticalCenteringView = [[UIView alloc] init];
156 verticalCenteringView.translatesAutoresizingMaskIntoConstraints = NO;
157 [contentView addSubview:verticalCenteringView];
158
159 _textLeadingAnchorConstraint = [_textLabel.leadingAnchor
160 constraintEqualToAnchor:_imageView.trailingAnchor];
161 _errorIconWidthConstraint = [_errorIcon.widthAnchor
162 constraintEqualToConstant:kHorizontalErrorIconFixedSize];
163 [NSLayoutConstraint activateConstraints:@[
164 // Set leading anchors.
165 [_imageView.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor
166 constant:kHorizontalPadding],
167 [_detailTextLabel.leadingAnchor
168 constraintEqualToAnchor:_textLabel.leadingAnchor],
169
170 // Fix image widths.
171 [_imageView.widthAnchor
172 constraintEqualToConstant:kHorizontalImageFixedSize],
173 _errorIconWidthConstraint,
174
175 // Set vertical anchors. This approach assumes the cell height is set by
176 // the view controller. Contents are pinned to centerY, rather than pushing
177 // against the top/bottom boundaries.
178 [_imageView.centerYAnchor
179 constraintEqualToAnchor:contentView.centerYAnchor],
180 [_textLabel.topAnchor
181 constraintEqualToAnchor:verticalCenteringView.topAnchor],
182 [_textLabel.bottomAnchor
183 constraintEqualToAnchor:_detailTextLabel.topAnchor],
184 [_detailTextLabel.bottomAnchor
185 constraintEqualToAnchor:verticalCenteringView.bottomAnchor],
186 [verticalCenteringView.centerYAnchor
187 constraintEqualToAnchor:contentView.centerYAnchor],
188 [_errorIcon.centerYAnchor
189 constraintEqualToAnchor:contentView.centerYAnchor],
190 // Set trailing anchors.
191 [_errorIcon.trailingAnchor
192 constraintEqualToAnchor:contentView.trailingAnchor
193 constant:-kHorizontalPaddingBetweenImageAndText],
194 [_detailTextLabel.trailingAnchor
195 constraintEqualToAnchor:_errorIcon.leadingAnchor
196 constant:-kHorizontalPaddingBetweenTextAndError],
197 _textLeadingAnchorConstraint,
198 [_textLabel.trailingAnchor
199 constraintLessThanOrEqualToAnchor:_errorIcon.leadingAnchor
200 constant:
201 -kHorizontalPaddingBetweenTextAndError],
202 ]];
203
204 // This is needed so the image doesn't get pushed out if both text and detail
205 // are long.
206 [_textLabel
207 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
208 forAxis:UILayoutConstraintAxisHorizontal];
209 [_detailTextLabel
210 setContentCompressionResistancePriority:UILayoutPriorityDefaultLow
211 forAxis:UILayoutConstraintAxisHorizontal];
212 }
213
214 #pragma mark - UIView
215
216 - (void)layoutSubviews {
217 [super layoutSubviews];
218
219 // Creates the image rounded corners.
220 _imageView.layer.cornerRadius = _imageView.image.size.width / 2.0f;
221
222 // Adjust the leading margin depending on existence of image.
223 if (_imageView.image) {
224 _textLeadingAnchorConstraint.constant =
225 kHorizontalPaddingBetweenImageAndText;
226 } else {
227 _textLeadingAnchorConstraint.constant = 0;
228 }
229
230 if (_errorIcon.image) {
231 _errorIconWidthConstraint.constant = kHorizontalErrorIconFixedSize;
232 } else {
233 _errorIconWidthConstraint.constant = 0;
234 }
235 }
236
237 #pragma mark - UICollectionReusableView
238
239 - (void)prepareForReuse {
240 [super prepareForReuse];
241 self.imageView.image = nil;
242 self.textLabel.text = nil;
243 self.detailTextLabel.text = nil;
244 self.errorIcon.image = nil;
245 self.accessoryType = MDCCollectionViewCellAccessoryNone;
246 self.userInteractionEnabled = YES;
247 self.contentView.alpha = 1;
248 UIImageView* accessoryImage =
249 base::mac::ObjCCastStrict<UIImageView>(self.accessoryView);
250 accessoryImage.tintColor =
251 [accessoryImage.tintColor colorWithAlphaComponent:1];
252 }
253
254 #pragma mark - NSObject(Accessibility)
255
256 - (NSString*)accessibilityLabel {
257 return self.textLabel.text;
258 }
259
260 - (NSString*)accessibilityValue {
261 return self.detailTextLabel.text;
262 }
263
264 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698