OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/chrome/browser/payments/cells/shipping_address_item.h" | 5 #import "ios/chrome/browser/payments/cells/autofill_profile_item.h" |
6 | 6 |
7 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" | 7 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 8 #import "ios/chrome/browser/ui/uikit_ui_util.h" |
8 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | 9 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
9 | 10 |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
11 #error "This file requires ARC support." | 12 #error "This file requires ARC support." |
12 #endif | 13 #endif |
13 | 14 |
14 namespace { | 15 namespace { |
15 // Padding of the leading and trailing edges of the cell. | 16 // Padding of the leading and trailing edges of the cell. |
16 const CGFloat kHorizontalPadding = 16; | 17 const CGFloat kHorizontalPadding = 16; |
17 | 18 |
18 // Padding of the top and bottom edges of the cell. | 19 // Padding of the top and bottom edges of the cell. |
19 const CGFloat kVerticalPadding = 16; | 20 const CGFloat kVerticalPadding = 16; |
20 | 21 |
21 // Spacing between the labels. | 22 // Spacing between the labels. |
22 const CGFloat kVerticalSpacingBetweenLabels = 8; | 23 const CGFloat kVerticalSpacingBetweenLabels = 8; |
23 } // namespace | 24 } // namespace |
24 | 25 |
25 @implementation ShippingAddressItem | 26 @implementation AutofillProfileItem |
26 | 27 |
27 @synthesize name = _name; | 28 @synthesize name = _name; |
28 @synthesize address = _address; | 29 @synthesize address = _address; |
29 @synthesize phoneNumber = _phoneNumber; | 30 @synthesize phoneNumber = _phoneNumber; |
| 31 @synthesize email = _email; |
| 32 @synthesize notification = _notification; |
30 @synthesize accessoryType = _accessoryType; | 33 @synthesize accessoryType = _accessoryType; |
31 | 34 |
32 #pragma mark CollectionViewItem | 35 #pragma mark CollectionViewItem |
33 | 36 |
34 - (instancetype)initWithType:(NSInteger)type { | 37 - (instancetype)initWithType:(NSInteger)type { |
35 self = [super initWithType:type]; | 38 self = [super initWithType:type]; |
36 if (self) { | 39 if (self) { |
37 self.cellClass = [ShippingAddressCell class]; | 40 self.cellClass = [AutofillProfileCell class]; |
38 } | 41 } |
39 return self; | 42 return self; |
40 } | 43 } |
41 | 44 |
42 - (void)configureCell:(ShippingAddressCell*)cell { | 45 - (void)configureCell:(AutofillProfileCell*)cell { |
43 [super configureCell:cell]; | 46 [super configureCell:cell]; |
44 cell.accessoryType = self.accessoryType; | 47 cell.accessoryType = self.accessoryType; |
45 cell.nameLabel.text = self.name; | 48 cell.nameLabel.text = self.name; |
46 cell.addressLabel.text = self.address; | 49 cell.addressLabel.text = self.address; |
47 cell.phoneNumberLabel.text = self.phoneNumber; | 50 cell.phoneNumberLabel.text = self.phoneNumber; |
| 51 cell.emailLabel.text = self.email; |
| 52 cell.notificationLabel.text = self.notification; |
48 } | 53 } |
49 | 54 |
50 @end | 55 @end |
51 | 56 |
52 @implementation ShippingAddressCell | 57 @implementation AutofillProfileCell { |
| 58 UIStackView* _stackView; |
| 59 } |
53 | 60 |
54 @synthesize nameLabel = _nameLabel; | 61 @synthesize nameLabel = _nameLabel; |
55 @synthesize addressLabel = _addressLabel; | 62 @synthesize addressLabel = _addressLabel; |
56 @synthesize phoneNumberLabel = _phoneNumberLabel; | 63 @synthesize phoneNumberLabel = _phoneNumberLabel; |
| 64 @synthesize emailLabel = _emailLabel; |
| 65 @synthesize notificationLabel = _notificationLabel; |
57 | 66 |
58 - (instancetype)initWithFrame:(CGRect)frame { | 67 - (instancetype)initWithFrame:(CGRect)frame { |
59 self = [super initWithFrame:frame]; | 68 self = [super initWithFrame:frame]; |
60 if (self) { | 69 if (self) { |
61 self.isAccessibilityElement = YES; | 70 self.isAccessibilityElement = YES; |
62 [self addSubviews]; | 71 [self addSubviews]; |
63 [self setDefaultViewStyling]; | 72 [self setDefaultViewStyling]; |
64 [self setViewConstraints]; | 73 [self setViewConstraints]; |
65 } | 74 } |
66 return self; | 75 return self; |
67 } | 76 } |
68 | 77 |
69 // Create and add subviews. | 78 // Create and add subviews. |
70 - (void)addSubviews { | 79 - (void)addSubviews { |
71 UIView* contentView = self.contentView; | 80 UIView* contentView = self.contentView; |
72 contentView.clipsToBounds = YES; | 81 contentView.clipsToBounds = YES; |
73 | 82 |
| 83 _stackView = [[UIStackView alloc] initWithArrangedSubviews:@[]]; |
| 84 _stackView.axis = UILayoutConstraintAxisVertical; |
| 85 _stackView.layoutMarginsRelativeArrangement = YES; |
| 86 _stackView.layoutMargins = |
| 87 UIEdgeInsetsMake(kVerticalPadding, kHorizontalPadding, kVerticalPadding, |
| 88 kHorizontalPadding); |
| 89 _stackView.alignment = UIStackViewAlignmentLeading; |
| 90 _stackView.spacing = kVerticalSpacingBetweenLabels; |
| 91 _stackView.translatesAutoresizingMaskIntoConstraints = NO; |
| 92 [contentView addSubview:_stackView]; |
| 93 |
74 _nameLabel = [[UILabel alloc] init]; | 94 _nameLabel = [[UILabel alloc] init]; |
75 _nameLabel.translatesAutoresizingMaskIntoConstraints = NO; | 95 [_stackView addArrangedSubview:_nameLabel]; |
76 [contentView addSubview:_nameLabel]; | |
77 | 96 |
78 _addressLabel = [[UILabel alloc] init]; | 97 _addressLabel = [[UILabel alloc] init]; |
79 _addressLabel.translatesAutoresizingMaskIntoConstraints = NO; | 98 [_stackView addArrangedSubview:_addressLabel]; |
80 [contentView addSubview:_addressLabel]; | |
81 | 99 |
82 _phoneNumberLabel = [[UILabel alloc] init]; | 100 _phoneNumberLabel = [[UILabel alloc] init]; |
83 _phoneNumberLabel.translatesAutoresizingMaskIntoConstraints = NO; | 101 [_stackView addArrangedSubview:_phoneNumberLabel]; |
84 [contentView addSubview:_phoneNumberLabel]; | 102 |
| 103 _emailLabel = [[UILabel alloc] init]; |
| 104 [_stackView addArrangedSubview:_emailLabel]; |
| 105 |
| 106 _notificationLabel = [[UILabel alloc] init]; |
| 107 [_stackView addArrangedSubview:_notificationLabel]; |
85 } | 108 } |
86 | 109 |
87 // Set default font and text colors for labels. | 110 // Set default font and text colors for labels. |
88 - (void)setDefaultViewStyling { | 111 - (void)setDefaultViewStyling { |
89 _nameLabel.font = [MDCTypography body2Font]; | 112 _nameLabel.font = [MDCTypography body2Font]; |
90 _nameLabel.textColor = [[MDCPalette greyPalette] tint900]; | 113 _nameLabel.textColor = [[MDCPalette greyPalette] tint900]; |
91 _nameLabel.numberOfLines = 0; | 114 _nameLabel.numberOfLines = 0; |
92 _nameLabel.lineBreakMode = NSLineBreakByWordWrapping; | 115 _nameLabel.lineBreakMode = NSLineBreakByWordWrapping; |
93 | 116 |
94 _addressLabel.font = [MDCTypography body1Font]; | 117 _addressLabel.font = [MDCTypography body1Font]; |
95 _addressLabel.textColor = [[MDCPalette greyPalette] tint900]; | 118 _addressLabel.textColor = [[MDCPalette greyPalette] tint900]; |
96 _addressLabel.numberOfLines = 0; | 119 _addressLabel.numberOfLines = 0; |
97 _addressLabel.lineBreakMode = NSLineBreakByWordWrapping; | 120 _addressLabel.lineBreakMode = NSLineBreakByWordWrapping; |
98 | 121 |
99 _phoneNumberLabel.font = [MDCTypography body1Font]; | 122 _phoneNumberLabel.font = [MDCTypography body1Font]; |
100 _phoneNumberLabel.textColor = [[MDCPalette greyPalette] tint900]; | 123 _phoneNumberLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 124 |
| 125 _emailLabel.font = [MDCTypography body1Font]; |
| 126 _emailLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 127 |
| 128 _notificationLabel.font = [MDCTypography body1Font]; |
| 129 _notificationLabel.textColor = [[MDCPalette cr_bluePalette] tint500]; |
101 } | 130 } |
102 | 131 |
103 // Set constraints on subviews. | 132 // Set constraints on subviews. |
104 - (void)setViewConstraints { | 133 - (void)setViewConstraints { |
105 UIView* contentView = self.contentView; | 134 AddSameSizeConstraint(self.contentView, _stackView); |
106 | |
107 [NSLayoutConstraint activateConstraints:@[ | |
108 // Set leading anchors. | |
109 [_nameLabel.leadingAnchor constraintEqualToAnchor:contentView.leadingAnchor | |
110 constant:kHorizontalPadding], | |
111 [_addressLabel.leadingAnchor | |
112 constraintEqualToAnchor:_nameLabel.leadingAnchor], | |
113 [_phoneNumberLabel.leadingAnchor | |
114 constraintEqualToAnchor:_addressLabel.leadingAnchor], | |
115 | |
116 // Set vertical anchors. | |
117 [_nameLabel.topAnchor constraintEqualToAnchor:contentView.topAnchor | |
118 constant:kVerticalPadding], | |
119 [_addressLabel.topAnchor | |
120 constraintEqualToAnchor:_nameLabel.bottomAnchor | |
121 constant:kVerticalSpacingBetweenLabels], | |
122 [_addressLabel.bottomAnchor | |
123 constraintEqualToAnchor:_phoneNumberLabel.topAnchor | |
124 constant:-kVerticalSpacingBetweenLabels], | |
125 [_phoneNumberLabel.bottomAnchor | |
126 constraintEqualToAnchor:contentView.bottomAnchor | |
127 constant:-kVerticalPadding], | |
128 | |
129 // Set trailing anchors. | |
130 [_nameLabel.trailingAnchor | |
131 constraintLessThanOrEqualToAnchor:contentView.trailingAnchor | |
132 constant:-kHorizontalPadding], | |
133 [_addressLabel.trailingAnchor | |
134 constraintLessThanOrEqualToAnchor:_nameLabel.trailingAnchor], | |
135 [_phoneNumberLabel.trailingAnchor | |
136 constraintLessThanOrEqualToAnchor:_addressLabel.trailingAnchor], | |
137 ]]; | |
138 } | 135 } |
139 | 136 |
140 #pragma mark - UIView | 137 #pragma mark - UIView |
141 | 138 |
142 // Implement -layoutSubviews as per instructions in documentation for | 139 // Implement -layoutSubviews as per instructions in documentation for |
143 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. | 140 // +[MDCCollectionViewCell cr_preferredHeightForWidth:forItem:]. |
144 - (void)layoutSubviews { | 141 - (void)layoutSubviews { |
| 142 _nameLabel.hidden = !_nameLabel.text; |
| 143 _addressLabel.hidden = !_addressLabel.text; |
| 144 _phoneNumberLabel.hidden = !_phoneNumberLabel.text; |
| 145 _emailLabel.hidden = !_emailLabel.text; |
| 146 _notificationLabel.hidden = !_notificationLabel.text; |
| 147 |
145 // When the accessory type is None, the content view of the cell (and thus) | 148 // When the accessory type is None, the content view of the cell (and thus) |
146 // the labels inside it span larger than when there is a Checkmark accessory | 149 // the labels inside it span larger than when there is a Checkmark accessory |
147 // type. That means that toggling the accessory type can induce a rewrapping | 150 // type. That means that toggling the accessory type can induce a rewrapping |
148 // of the texts, which is not visually pleasing. To alleviate that issue | 151 // of the texts, which is not visually pleasing. To alleviate that issue |
149 // always lay out the cell as if there was a Checkmark accessory type. | 152 // always lay out the cell as if there was a Checkmark accessory type. |
150 // | 153 // |
151 // Force the accessory type to Checkmark for the duration of layout. | 154 // Force the accessory type to Checkmark for the duration of layout. |
152 MDCCollectionViewCellAccessoryType realAccessoryType = self.accessoryType; | 155 MDCCollectionViewCellAccessoryType realAccessoryType = self.accessoryType; |
153 self.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | 156 self.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
154 | 157 |
155 [super layoutSubviews]; | 158 [super layoutSubviews]; |
156 | 159 |
157 // Adjust labels' preferredMaxLayoutWidth when the parent's width changes, for | 160 // Adjust preferredMaxLayoutWidth of _nameLabel and _addressLabel when the |
158 // instance on screen rotation. | 161 // parent's width changes, for instance on screen rotation. |
159 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); | 162 CGFloat parentWidth = CGRectGetWidth(self.contentView.frame); |
160 CGFloat preferredMaxLayoutWidth = parentWidth - (2 * kHorizontalPadding); | 163 CGFloat preferredMaxLayoutWidth = parentWidth - (2 * kHorizontalPadding); |
161 _nameLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | 164 _nameLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; |
162 _addressLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | 165 _addressLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; |
163 _phoneNumberLabel.preferredMaxLayoutWidth = preferredMaxLayoutWidth; | |
164 | 166 |
165 // Re-layout with the new preferred width to allow the label to adjust its | 167 // Re-layout with the new preferred width to allow the label to adjust its |
166 // height. | 168 // height. |
167 [super layoutSubviews]; | 169 [super layoutSubviews]; |
168 | 170 |
169 // Restore the real accessory type at the end of the layout. | 171 // Restore the real accessory type at the end of the layout. |
170 self.accessoryType = realAccessoryType; | 172 self.accessoryType = realAccessoryType; |
171 } | 173 } |
172 | 174 |
173 #pragma mark - UICollectionReusableView | 175 #pragma mark - UICollectionReusableView |
174 | 176 |
175 - (void)prepareForReuse { | 177 - (void)prepareForReuse { |
176 [super prepareForReuse]; | 178 [super prepareForReuse]; |
177 self.nameLabel.text = nil; | 179 self.nameLabel.text = nil; |
178 self.addressLabel.text = nil; | 180 self.addressLabel.text = nil; |
179 self.phoneNumberLabel.text = nil; | 181 self.phoneNumberLabel.text = nil; |
| 182 self.emailLabel.text = nil; |
| 183 self.notificationLabel.text = nil; |
180 self.accessoryType = MDCCollectionViewCellAccessoryNone; | 184 self.accessoryType = MDCCollectionViewCellAccessoryNone; |
181 } | 185 } |
182 | 186 |
183 #pragma mark - NSObject(Accessibility) | 187 #pragma mark - NSObject(Accessibility) |
184 | 188 |
185 - (NSString*)accessibilityLabel { | 189 - (NSString*)accessibilityLabel { |
186 return [NSString stringWithFormat:@"%@, %@, %@", self.nameLabel.text, | 190 return [NSString |
187 self.addressLabel.text, | 191 stringWithFormat:@"%@, %@, %@, %@, %@", self.nameLabel.text, |
188 self.phoneNumberLabel.text]; | 192 self.addressLabel.text, self.phoneNumberLabel.text, |
| 193 self.emailLabel.text, self.notificationLabel.text]; |
189 } | 194 } |
190 | 195 |
191 @end | 196 @end |
OLD | NEW |