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/settings/cells/signin_promo_item.h" | |
6 | |
7 #include "base/strings/sys_string_conversions.h" | |
8 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | |
9 #include "ios/chrome/grit/ios_chromium_strings.h" | |
10 #include "ios/chrome/grit/ios_strings.h" | |
11 #import "ios/third_party/material_components_ios/src/components/Buttons/src/Mate
rialButtons.h" | |
12 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | |
13 #include "ui/base/l10n/l10n_util.h" | |
14 | |
15 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
16 #error "This file requires ARC support." | |
17 #endif | |
18 | |
19 namespace { | |
20 // Horizontal padding for label and buttons. | |
21 const CGFloat kHorizontalPadding = 40; | |
22 // Vertical padding for the image and the label. | |
23 const CGFloat kVerticalPadding = 12; | |
24 // Vertical padding for buttons. | |
25 const CGFloat kButtonVerticalPadding = 6; | |
26 // Image size. | |
27 const CGFloat kImageFixedSize = 48; | |
28 // Button height. | |
29 const CGFloat kButtonHeight = 36; | |
30 } | |
31 | |
32 @implementation SigninPromoItem | |
33 | |
34 @synthesize profileImage = _profileImage; | |
35 @synthesize profileName = _profileName; | |
36 @synthesize profileEmail = _profileEmail; | |
37 | |
38 - (instancetype)initWithType:(NSInteger)type { | |
39 self = [super initWithType:type]; | |
40 if (self) { | |
41 self.cellClass = [SigninPromoCell class]; | |
42 } | |
43 return self; | |
44 } | |
45 | |
46 #pragma mark - CollectionViewItem | |
47 | |
48 - (void)configureCell:(SigninPromoCell*)cell { | |
49 [super configureCell:cell]; | |
50 cell.imageView.image = _profileImage; | |
51 cell.textLabel.text = l10n_util::GetNSString(IDS_IOS_SIGNIN_PROMO_SETTINGS); | |
52 [cell.signinButton | |
53 setTitle:l10n_util::GetNSStringF(IDS_IOS_SIGNIN_PROMO_CONTINUE_AS, | |
54 base::SysNSStringToUTF16(_profileName)) | |
55 forState:UIControlStateNormal]; | |
56 [cell.notMeButton | |
57 setTitle:l10n_util::GetNSStringF(IDS_IOS_SIGNIN_PROMO_NOT, | |
58 base::SysNSStringToUTF16(_profileEmail)) | |
59 forState:UIControlStateNormal]; | |
60 } | |
61 | |
62 @end | |
63 | |
64 @implementation SigninPromoCell | |
65 | |
66 @synthesize imageView = _imageView; | |
67 @synthesize textLabel = _textLabel; | |
68 @synthesize signinButton = _signinButton; | |
69 @synthesize notMeButton = _notMeButton; | |
70 | |
71 - (instancetype)initWithFrame:(CGRect)frame { | |
72 self = [super initWithFrame:frame]; | |
73 if (self) { | |
74 self.isAccessibilityElement = YES; | |
75 [self addSubviews]; | |
76 [self setDefaultViewStyling]; | |
77 [self setViewConstraints]; | |
78 } | |
79 return self; | |
80 } | |
81 | |
82 // Create and add subviews. | |
83 - (void)addSubviews { | |
84 UIView* contentView = self.contentView; | |
85 contentView.clipsToBounds = YES; | |
86 | |
87 _imageView = [[UIImageView alloc] init]; | |
88 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
89 [contentView addSubview:_imageView]; | |
90 | |
91 _textLabel = [[UILabel alloc] init]; | |
92 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
93 [contentView addSubview:_textLabel]; | |
94 | |
95 _signinButton = [[MDCFlatButton alloc] init]; | |
96 _signinButton.translatesAutoresizingMaskIntoConstraints = NO; | |
97 _signinButton.accessibilityIdentifier = @"signin_promo_button"; | |
98 [contentView addSubview:_signinButton]; | |
99 | |
100 _notMeButton = [[MDCFlatButton alloc] init]; | |
101 _notMeButton.translatesAutoresizingMaskIntoConstraints = NO; | |
102 _notMeButton.accessibilityIdentifier = @"signin_promo_no_button"; | |
103 [contentView addSubview:_notMeButton]; | |
104 } | |
105 | |
106 - (void)setDefaultViewStyling { | |
107 _imageView.contentMode = UIViewContentModeCenter; | |
108 _imageView.layer.masksToBounds = YES; | |
109 _imageView.contentMode = UIViewContentModeScaleAspectFit; | |
110 _textLabel.font = [MDCTypography buttonFont]; | |
111 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
112 _textLabel.numberOfLines = 0; | |
113 _textLabel.textAlignment = NSTextAlignmentCenter; | |
114 [_signinButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] | |
115 forState:UIControlStateNormal]; | |
116 _signinButton.customTitleColor = [UIColor whiteColor]; | |
117 _signinButton.inkColor = [UIColor colorWithWhite:1 alpha:0.2]; | |
118 _notMeButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; | |
119 _notMeButton.uppercaseTitle = NO; | |
120 } | |
121 | |
122 - (void)setViewConstraints { | |
123 UIView* contentView = self.contentView; | |
124 | |
125 [NSLayoutConstraint activateConstraints:@[ | |
126 // Set vertical anchors. | |
127 [_imageView.topAnchor constraintEqualToAnchor:contentView.topAnchor | |
128 constant:kVerticalPadding * 2], | |
129 [_textLabel.topAnchor constraintEqualToAnchor:_imageView.bottomAnchor | |
130 constant:kVerticalPadding], | |
131 [_signinButton.topAnchor | |
132 constraintEqualToAnchor:_textLabel.bottomAnchor | |
133 constant:kVerticalPadding + kButtonVerticalPadding], | |
134 [_notMeButton.topAnchor constraintEqualToAnchor:_signinButton.bottomAnchor | |
135 constant:kButtonVerticalPadding * 2], | |
136 [contentView.bottomAnchor | |
137 constraintEqualToAnchor:_notMeButton.bottomAnchor | |
138 constant:kVerticalPadding + kButtonVerticalPadding], | |
139 | |
140 // Set horizontal anchors. | |
141 [_imageView.centerXAnchor | |
142 constraintEqualToAnchor:contentView.centerXAnchor], | |
143 [_textLabel.centerXAnchor | |
144 constraintEqualToAnchor:contentView.centerXAnchor], | |
145 [_signinButton.centerXAnchor | |
146 constraintEqualToAnchor:contentView.centerXAnchor], | |
147 [_signinButton.leadingAnchor | |
148 constraintEqualToAnchor:contentView.leadingAnchor | |
149 constant:kHorizontalPadding], | |
150 [contentView.trailingAnchor | |
151 constraintEqualToAnchor:_signinButton.trailingAnchor | |
152 constant:kHorizontalPadding], | |
153 [_notMeButton.centerXAnchor | |
154 constraintEqualToAnchor:contentView.centerXAnchor], | |
155 [_notMeButton.leadingAnchor | |
156 constraintEqualToAnchor:contentView.leadingAnchor | |
157 constant:kHorizontalPadding], | |
158 [contentView.trailingAnchor | |
159 constraintEqualToAnchor:_notMeButton.trailingAnchor | |
160 constant:kHorizontalPadding], | |
161 | |
162 // Fix width and height. | |
163 [_imageView.widthAnchor constraintEqualToConstant:kImageFixedSize], | |
164 [_imageView.heightAnchor constraintEqualToConstant:kImageFixedSize], | |
165 [_signinButton.heightAnchor constraintEqualToConstant:kButtonHeight], | |
166 [_notMeButton.heightAnchor constraintEqualToConstant:kButtonHeight], | |
167 ]]; | |
168 } | |
169 | |
170 // Implements -layoutSubviews as per instructions in documentation for | |
171 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
172 - (void)layoutSubviews { | |
173 [super layoutSubviews]; | |
174 | |
175 // Adjust the text label preferredMaxLayoutWidth when the parent's width | |
176 // changes, for instance on screen rotation. | |
177 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | |
178 _textLabel.preferredMaxLayoutWidth = parentWidth - 2 * kHorizontalPadding; | |
179 | |
180 // Re-layout with the new preferred width to allow the label to adjust its | |
181 // height. | |
182 [super layoutSubviews]; | |
183 } | |
184 | |
185 #pragma mark - Action | |
186 | |
187 - (void)accessibilitySigninAction:(id)unused { | |
188 // TODO(jlebel): Need to implement | |
189 NSLog(@"Sign-in action"); | |
190 } | |
191 | |
192 - (void)accessibilityNotMeAction:(id)unused { | |
193 // TODO(jlebel): Need to implement | |
194 NSLog(@"Not me action"); | |
195 } | |
196 | |
197 #pragma mark - UICollectionReusableView | |
198 | |
199 - (void)prepareForReuse { | |
200 [super prepareForReuse]; | |
201 _imageView.image = nil; | |
202 _textLabel.text = nil; | |
203 _signinButton = nil; | |
204 _notMeButton = nil; | |
205 } | |
206 | |
207 #pragma mark - NSObject(Accessibility) | |
208 | |
209 - (NSArray<UIAccessibilityCustomAction*>*)accessibilityCustomActions { | |
210 NSString* signinActionName = | |
211 [_signinButton titleForState:UIControlStateNormal]; | |
212 UIAccessibilityCustomAction* signinCustomAction = | |
213 [[UIAccessibilityCustomAction alloc] | |
214 initWithName:signinActionName | |
215 target:self | |
216 selector:@selector(accessibilitySigninAction:)]; | |
217 NSString* notMeActionName = [_notMeButton titleForState:UIControlStateNormal]; | |
218 UIAccessibilityCustomAction* notMeCustomAction = | |
219 [[UIAccessibilityCustomAction alloc] | |
220 initWithName:notMeActionName | |
221 target:self | |
222 selector:@selector(accessibilityNotMeAction:)]; | |
223 return [NSArray arrayWithObjects:signinCustomAction, notMeCustomAction, nil]; | |
224 } | |
225 | |
226 - (NSString*)accessibilityLabel { | |
227 return l10n_util::GetNSString(IDS_IOS_SIGNIN_PROMO_SETTINGS); | |
228 } | |
229 | |
230 @end | |
OLD | NEW |