Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
|
lpromero
2017/03/09 10:55:03
2017
jlebel
2017/03/09 20:34:07
Done.
| |
| 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 #import "ios/chrome/browser/ui/uikit_ui_util.h" | |
| 10 #include "ios/chrome/grit/ios_chromium_strings.h" | |
| 11 #include "ios/chrome/grit/ios_strings.h" | |
| 12 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat erialPalettes.h" | |
| 13 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF ontLoader.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 | |
| 16 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 17 #error "This file requires ARC support." | |
| 18 #endif | |
| 19 | |
| 20 namespace { | |
| 21 // Horizontal padding for label and buttons. | |
| 22 const CGFloat kHorizontalPadding = 40; | |
| 23 // Vertical padding for the image and the label. | |
| 24 const CGFloat kVerticalPadding = 12; | |
| 25 // Vertical paddingn for buttons. | |
|
lpromero
2017/03/09 10:55:03
padding
jlebel
2017/03/09 20:34:08
Done.
| |
| 26 const CGFloat kButtonVerticalPadding = 6; | |
| 27 // Image size. | |
| 28 const CGFloat kImageFixedSize = 48; | |
| 29 // Button height. | |
| 30 const CGFloat kButtonHeight = 36; | |
| 31 } | |
| 32 | |
| 33 @implementation SigninPromoItem | |
| 34 | |
| 35 @synthesize image = _image; | |
| 36 @synthesize profileName = _profileName; | |
| 37 @synthesize profileEmail = _profileEmail; | |
| 38 | |
| 39 - (instancetype)initWithType:(NSInteger)type { | |
| 40 self = [super initWithType:type]; | |
| 41 if (self) { | |
| 42 self.cellClass = [SigninPromoCell class]; | |
| 43 self.accessibilityTraits |= UIAccessibilityTraitButton; | |
|
lpromero
2017/03/09 10:55:02
I wouldn't care for accessibility within the item,
| |
| 44 } | |
| 45 return self; | |
| 46 } | |
| 47 | |
| 48 #pragma mark - CollectionViewItem | |
| 49 | |
| 50 - (void)configureCell:(SigninPromoCell*)cell { | |
| 51 [super configureCell:cell]; | |
| 52 [cell configureWithProfileName:_profileName | |
| 53 profileEmail:_profileEmail | |
| 54 profileImage:_image]; | |
| 55 } | |
| 56 | |
| 57 @end | |
| 58 | |
| 59 @implementation SigninPromoCell { | |
| 60 // Profie name. | |
| 61 NSString* _profileName; | |
|
lpromero
2017/03/09 10:55:02
Doesn't seem needed to keep it in an ivar.
jlebel
2017/03/09 20:34:07
Done.
| |
| 62 // Profile imageView. | |
| 63 UIImageView* _imageView; | |
| 64 // Cell title. | |
| 65 UILabel* _textLabel; | |
| 66 // Signin button. | |
| 67 MDCFlatButton* _signinButton; | |
| 68 // Dismiss button. | |
| 69 MDCFlatButton* _dismissButton; | |
| 70 } | |
| 71 | |
| 72 - (instancetype)initWithFrame:(CGRect)frame { | |
| 73 self = [super initWithFrame:frame]; | |
| 74 if (self) { | |
| 75 self.isAccessibilityElement = YES; | |
| 76 [self addSubviews]; | |
| 77 [self setDefaultViewStyling]; | |
| 78 [self setViewConstraints]; | |
| 79 } | |
| 80 return self; | |
| 81 } | |
| 82 | |
| 83 - (void)configureWithProfileName:(NSString*)profileName | |
| 84 profileEmail:(NSString*)profileEmail | |
| 85 profileImage:(UIImage*)profileImage { | |
| 86 _textLabel.text = l10n_util::GetNSString(IDS_IOS_SIGNIN_PROMO_SETTINGS); | |
| 87 _profileName = profileName; | |
| 88 [self updateSigninButtonTitle]; | |
| 89 [_dismissButton | |
| 90 setTitle:l10n_util::GetNSStringF(IDS_IOS_SIGNIN_PROMO_NOT, | |
| 91 base::SysNSStringToUTF16(profileEmail)) | |
| 92 forState:UIControlStateNormal]; | |
| 93 _imageView.image = profileImage; | |
| 94 } | |
| 95 | |
| 96 - (void)updateSigninButtonTitle { | |
| 97 int messageId = IDS_IOS_SIGNIN_PROMO_CONTINUE_AS; | |
| 98 if (IsCompact(self)) { | |
|
lpromero
2017/03/09 10:55:03
You mean it returns NO here? Probably because when
jlebel
2017/03/09 20:34:07
Done.
| |
| 99 messageId = IDS_IOS_SIGNIN_PROMO_CONTINUE_AS_COMPACT; | |
| 100 } | |
| 101 [_signinButton setTitle:l10n_util::GetNSStringF( | |
| 102 messageId, base::SysNSStringToUTF16(_profileName)) | |
| 103 forState:UIControlStateNormal]; | |
| 104 } | |
| 105 | |
| 106 // Create and add subviews. | |
| 107 - (void)addSubviews { | |
| 108 UIView* contentView = self.contentView; | |
| 109 contentView.clipsToBounds = YES; | |
| 110 | |
| 111 _imageView = [[UIImageView alloc] init]; | |
| 112 _imageView.translatesAutoresizingMaskIntoConstraints = NO; | |
| 113 [contentView addSubview:_imageView]; | |
| 114 | |
| 115 _textLabel = [[UILabel alloc] init]; | |
| 116 _textLabel.translatesAutoresizingMaskIntoConstraints = NO; | |
| 117 [contentView addSubview:_textLabel]; | |
| 118 | |
| 119 _signinButton = [[MDCFlatButton alloc] init]; | |
| 120 _signinButton.translatesAutoresizingMaskIntoConstraints = NO; | |
| 121 _signinButton.accessibilityIdentifier = @"signin_promo_button"; | |
| 122 [contentView addSubview:_signinButton]; | |
| 123 | |
| 124 _dismissButton = [[MDCFlatButton alloc] init]; | |
| 125 _dismissButton.translatesAutoresizingMaskIntoConstraints = NO; | |
| 126 _dismissButton.accessibilityIdentifier = @"signin_promo_no_button"; | |
| 127 [contentView addSubview:_dismissButton]; | |
| 128 } | |
| 129 | |
| 130 - (void)setDefaultViewStyling { | |
| 131 _imageView.contentMode = UIViewContentModeCenter; | |
| 132 _imageView.layer.masksToBounds = YES; | |
| 133 _imageView.contentMode = UIViewContentModeScaleAspectFit; | |
| 134 _textLabel.font = [[MDFRobotoFontLoader sharedInstance] mediumFontOfSize:14]; | |
|
lpromero
2017/03/09 10:55:03
Please use MDCTypography instead.
jlebel
2017/03/13 17:08:42
Done.
| |
| 135 _textLabel.textColor = [[MDCPalette greyPalette] tint900]; | |
| 136 _textLabel.numberOfLines = 0; | |
| 137 _textLabel.textAlignment = NSTextAlignmentCenter; | |
| 138 [_signinButton setBackgroundColor:[[MDCPalette cr_bluePalette] tint500] | |
| 139 forState:UIControlStateNormal]; | |
| 140 _signinButton.customTitleColor = [UIColor whiteColor]; | |
| 141 _signinButton.inkColor = [UIColor colorWithWhite:1 alpha:0.2]; | |
| 142 _dismissButton.customTitleColor = [[MDCPalette cr_bluePalette] tint500]; | |
| 143 _dismissButton.uppercaseTitle = NO; | |
| 144 } | |
| 145 | |
| 146 - (void)setViewConstraints { | |
| 147 UIView* contentView = self.contentView; | |
| 148 | |
| 149 [NSLayoutConstraint activateConstraints:@[ | |
| 150 // Set vertical anchors. | |
| 151 [_imageView.topAnchor constraintEqualToAnchor:contentView.topAnchor | |
| 152 constant:kVerticalPadding * 2], | |
| 153 [_textLabel.topAnchor constraintEqualToAnchor:_imageView.bottomAnchor | |
| 154 constant:kVerticalPadding], | |
| 155 [_signinButton.topAnchor | |
| 156 constraintEqualToAnchor:_textLabel.bottomAnchor | |
| 157 constant:kVerticalPadding + kButtonVerticalPadding], | |
| 158 [_dismissButton.topAnchor | |
| 159 constraintEqualToAnchor:_signinButton.bottomAnchor | |
| 160 constant:kButtonVerticalPadding * 2], | |
| 161 [contentView.bottomAnchor | |
| 162 constraintEqualToAnchor:_dismissButton.bottomAnchor | |
| 163 constant:kVerticalPadding + kButtonVerticalPadding], | |
| 164 | |
| 165 // Set horizontal anchors. | |
| 166 [_imageView.centerXAnchor | |
| 167 constraintEqualToAnchor:contentView.centerXAnchor], | |
| 168 [_textLabel.centerXAnchor | |
| 169 constraintEqualToAnchor:contentView.centerXAnchor], | |
| 170 [_signinButton.centerXAnchor | |
| 171 constraintEqualToAnchor:contentView.centerXAnchor], | |
| 172 [_signinButton.leadingAnchor | |
| 173 constraintEqualToAnchor:contentView.leadingAnchor | |
| 174 constant:kHorizontalPadding], | |
| 175 [contentView.trailingAnchor | |
| 176 constraintEqualToAnchor:_signinButton.trailingAnchor | |
| 177 constant:kHorizontalPadding], | |
| 178 [_dismissButton.centerXAnchor | |
| 179 constraintEqualToAnchor:contentView.centerXAnchor], | |
| 180 [_dismissButton.leadingAnchor | |
| 181 constraintEqualToAnchor:contentView.leadingAnchor | |
| 182 constant:kHorizontalPadding], | |
| 183 [contentView.trailingAnchor | |
| 184 constraintEqualToAnchor:_dismissButton.trailingAnchor | |
| 185 constant:kHorizontalPadding], | |
| 186 | |
| 187 // Fix width and height. | |
| 188 [_imageView.widthAnchor constraintEqualToConstant:kImageFixedSize], | |
| 189 [_imageView.heightAnchor constraintEqualToConstant:kImageFixedSize], | |
| 190 [_signinButton.heightAnchor constraintEqualToConstant:kButtonHeight], | |
| 191 [_dismissButton.heightAnchor constraintEqualToConstant:kButtonHeight], | |
| 192 ]]; | |
| 193 } | |
| 194 | |
| 195 // Implements -layoutSubviews as per instructions in documentation for | |
| 196 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | |
| 197 - (void)layoutSubviews { | |
| 198 [super layoutSubviews]; | |
| 199 | |
| 200 // Adjust the text label preferredMaxLayoutWidth when the parent's width | |
| 201 // changes, for instance on screen rotation. | |
| 202 CGFloat parentWidth = CGRectGetWidth(self.contentView.bounds); | |
| 203 _textLabel.preferredMaxLayoutWidth = parentWidth - 2 * kHorizontalPadding; | |
| 204 | |
| 205 // Re-layout with the new preferred width to allow the label to adjust its | |
| 206 // height. | |
| 207 [super layoutSubviews]; | |
| 208 } | |
| 209 | |
| 210 - (void)traitCollectionDidChange: | |
| 211 (nullable UITraitCollection*)previousTraitCollection { | |
| 212 [super traitCollectionDidChange:previousTraitCollection]; | |
| 213 if (self.traitCollection.horizontalSizeClass != | |
| 214 previousTraitCollection.horizontalSizeClass) { | |
| 215 [self updateSigninButtonTitle]; | |
| 216 } | |
| 217 } | |
| 218 | |
| 219 #pragma mark - UICollectionReusableView | |
| 220 | |
| 221 - (void)prepareForReuse { | |
| 222 [super prepareForReuse]; | |
| 223 _profileName = nil; | |
| 224 _imageView.image = nil; | |
| 225 _textLabel.text = nil; | |
| 226 _signinButton = nil; | |
| 227 _dismissButton = nil; | |
| 228 } | |
| 229 | |
| 230 #pragma mark - NSObject(Accessibility) | |
|
lpromero
2017/03/09 10:55:02
Let's discuss accessibility possibilities IRL.
| |
| 231 | |
| 232 - (NSString*)accessibilityLabel { | |
| 233 return l10n_util::GetNSString(IDS_IOS_SIGN_IN_TO_CHROME_SETTING_TITLE); | |
| 234 } | |
| 235 | |
| 236 - (NSString*)accessibilityValue { | |
| 237 return [NSString stringWithFormat:@"%@", _textLabel.text]; | |
| 238 } | |
| 239 | |
| 240 @end | |
| OLD | NEW |