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

Side by Side Diff: ios/chrome/browser/ui/payments/credit_card_edit_view_controller.mm

Issue 2876603005: [Payment Request] Refactors the edit view controller (Closed)
Patch Set: Addressed comments Created 3 years, 7 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
1 // Copyright 2017 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/ui/payments/credit_card_edit_view_controller.h" 5 #import "ios/chrome/browser/ui/payments/credit_card_edit_view_controller.h"
6 6
7 #import "base/mac/foundation_util.h" 7 #import "base/mac/foundation_util.h"
8 #include "base/memory/ptr_util.h" 8 #include "base/memory/ptr_util.h"
9 #include "components/strings/grit/components_strings.h" 9 #include "components/strings/grit/components_strings.h"
10 #import "ios/chrome/browser/ui/autofill/autofill_ui_type.h" 10 #import "ios/chrome/browser/ui/autofill/autofill_ui_type.h"
11 #import "ios/chrome/browser/ui/autofill/cells/autofill_edit_item.h" 11 #import "ios/chrome/browser/ui/autofill/cells/autofill_edit_item.h"
12 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h" 12 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h"
13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item .h"
14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collec tion_view_controller.h" 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collec tion_view_controller.h"
15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item .h" 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item .h"
16 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" 15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
17 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" 16 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
18 #import "ios/chrome/browser/ui/payments/payment_request_edit_view_controller+int ernal.h" 17 #import "ios/chrome/browser/ui/payments/payment_request_edit_view_controller+int ernal.h"
19 #import "ios/chrome/browser/ui/payments/payment_request_edit_view_controller_act ions.h" 18 #import "ios/chrome/browser/ui/payments/payment_request_edit_view_controller_act ions.h"
20 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h" 19 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h"
21 #import "ios/chrome/browser/ui/uikit_ui_util.h" 20 #import "ios/chrome/browser/ui/uikit_ui_util.h"
22 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h" 21 #import "ios/third_party/material_components_ios/src/components/Typography/src/M aterialTypography.h"
23 #include "ui/base/l10n/l10n_util.h" 22 #include "ui/base/l10n/l10n_util.h"
24 23
25 #if !defined(__has_feature) || !__has_feature(objc_arc) 24 #if !defined(__has_feature) || !__has_feature(objc_arc)
26 #error "This file requires ARC support." 25 #error "This file requires ARC support."
27 #endif 26 #endif
28 27
29 namespace { 28 namespace {
30 29
31 NSString* const kCreditCardEditCollectionViewId = 30 NSString* const kCreditCardEditCollectionViewId =
32 @"kCreditCardEditCollectionViewId"; 31 @"kCreditCardEditCollectionViewId";
33 32
34 typedef NS_ENUM(NSInteger, SectionIdentifier) { 33 typedef NS_ENUM(NSInteger, SectionIdentifier) {
35 SectionIdentifierAcceptedMethods = kSectionIdentifierEnumStart, 34 SectionIdentifierSaveCard = kSectionIdentifierEnumStart,
36 SectionIdentifierCardSummary,
37 SectionIdentifierBillingAddress,
38 SectionIdentifierSaveCard,
39 }; 35 };
40 36
41 typedef NS_ENUM(NSInteger, ItemType) { 37 typedef NS_ENUM(NSInteger, ItemType) {
42 ItemTypeAcceptedMethods = kItemTypeEnumStart, 38 ItemTypeSaveCard = kItemTypeEnumStart,
43 ItemTypeCardSummary,
44 ItemTypeBillingAddress,
45 ItemTypeSaveCard,
46 }; 39 };
47 40
48 } // namespace 41 } // namespace
49 42
50 @interface CreditCardEditViewController () { 43 @interface CreditCardEditViewController () {
51 NSArray<EditorField*>* _fields; 44 NSArray<EditorField*>* _fields;
52 45
53 // Indicates whether the credit card being created should be saved locally. 46 // Indicates whether the credit card being created should be saved locally.
54 BOOL _saveCreditCard; 47 BOOL _saveCreditCard;
55 } 48 }
56 49
57 @end 50 @end
58 51
59 @implementation CreditCardEditViewController 52 @implementation CreditCardEditViewController
60 53
61 @synthesize delegate = _delegate; 54 @synthesize delegate = _delegate;
62 @synthesize dataSource = _dataSource; 55 @synthesize dataSource = _dataSource;
63 @synthesize billingAddressGUID = _billingAddressGUID;
64 56
65 #pragma mark - Initialization 57 #pragma mark - Initialization
66 58
67 - (instancetype)init { 59 - (instancetype)init {
68 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; 60 self = [super initWithStyle:CollectionViewControllerStyleAppBar];
69 if (self) { 61 if (self) {
70 _saveCreditCard = YES; 62 _saveCreditCard = YES;
71 63
72 // Set up leading (cancel) button. 64 // Set up leading (cancel) button.
73 UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc] 65 UIBarButtonItem* cancelButton = [[UIBarButtonItem alloc]
(...skipping 19 matching lines...) Expand all
93 NSForegroundColorAttributeName : [UIColor lightGrayColor] 85 NSForegroundColorAttributeName : [UIColor lightGrayColor]
94 } 86 }
95 forState:UIControlStateDisabled]; 87 forState:UIControlStateDisabled];
96 [doneButton setAccessibilityLabel:l10n_util::GetNSString(IDS_ACCNAME_DONE)]; 88 [doneButton setAccessibilityLabel:l10n_util::GetNSString(IDS_ACCNAME_DONE)];
97 [self navigationItem].rightBarButtonItem = doneButton; 89 [self navigationItem].rightBarButtonItem = doneButton;
98 } 90 }
99 91
100 return self; 92 return self;
101 } 93 }
102 94
95 - (void)setDelegate:(id<CreditCardEditViewControllerDelegate>)delegate {
96 [super setDelegate:delegate];
97 _delegate = delegate;
98 }
99
103 - (void)setDataSource:(id<CreditCardEditViewControllerDataSource>)dataSource { 100 - (void)setDataSource:(id<CreditCardEditViewControllerDataSource>)dataSource {
104 [super setDataSource:dataSource]; 101 [super setDataSource:dataSource];
105 _dataSource = dataSource; 102 _dataSource = dataSource;
106 _fields = [dataSource editorFields]; 103 _fields = [dataSource editorFields];
107 } 104 }
108 105
109 - (BOOL)validateForm {
110 if (![super validateForm])
111 return NO;
112
113 // TODO(crbug.com/602666): Uncomment the following when billing address
114 // selection UI is ready.
115 // Validate the billing address GUID.
116 // NSString* errorMessage =
117 // !_billingAddressGUID ? l10n_util::GetNSString(
118 // IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE)
119 // : nil;
120 // [self addOrRemoveErrorMessage:errorMessage
121 // inSectionWithIdentifier:SectionIdentifierBillingAddress];
122 // if (errorMessage) {
123 // return NO;
124 // }
125
126 return YES;
127 }
128
129 #pragma mark - PaymentRequestEditViewControllerActions methods 106 #pragma mark - PaymentRequestEditViewControllerActions methods
130 107
131 - (void)onCancel { 108 - (void)onCancel {
132 [_delegate creditCardEditViewControllerDidCancel:self]; 109 [_delegate creditCardEditViewControllerDidCancel:self];
133 } 110 }
134 111
135 - (void)onDone { 112 - (void)onDone {
136 if (![self validateForm]) 113 if (![self validateForm])
137 return; 114 return;
138 115
139 [_delegate creditCardEditViewController:self 116 [_delegate creditCardEditViewController:self
140 didFinishEditingFields:_fields 117 didFinishEditingFields:_fields
141 billingAddressID:_billingAddressGUID
142 saveCreditCard:_saveCreditCard]; 118 saveCreditCard:_saveCreditCard];
143 } 119 }
144 120
145 #pragma mark - CollectionViewController methods 121 #pragma mark - CollectionViewController methods
146 122
147 - (void)loadModel { 123 - (void)loadModel {
148 [super loadModel]; 124 [super loadModel];
149 125
150 // If editing a credit card, set the card type icon (e.g. "Visa"). 126 // If editing a credit card, set the card type icon (e.g. "Visa").
151 if (_dataSource.state == CreditCardEditViewControllerStateEdit) { 127 if (_dataSource.state == EditViewControllerStateEdit) {
152 for (EditorField* field in _fields) { 128 for (EditorField* field in _fields) {
153 AutofillEditItem* item = field.item;
154 if (field.autofillUIType == AutofillUITypeCreditCardNumber) { 129 if (field.autofillUIType == AutofillUITypeCreditCardNumber) {
130 AutofillEditItem* item =
131 base::mac::ObjCCastStrict<AutofillEditItem>(field.item);
155 item.cardTypeIcon = 132 item.cardTypeIcon =
156 [_dataSource cardTypeIconFromCardNumber:field.value]; 133 [_dataSource cardTypeIconFromCardNumber:item.textFieldValue];
157 } 134 }
158 } 135 }
159 } 136 }
160 } 137 }
161 138
162 - (void)loadHeaderItems {
163 [super loadHeaderItems];
164 CollectionViewModel* model = self.collectionViewModel;
165
166 // Server card summary section.
167 CollectionViewItem* serverCardSummaryItem =
168 [_dataSource serverCardSummaryItem];
169 if (serverCardSummaryItem) {
170 [model addSectionWithIdentifier:SectionIdentifierCardSummary];
171 serverCardSummaryItem.type = ItemTypeCardSummary;
172 [model addItem:serverCardSummaryItem
173 toSectionWithIdentifier:SectionIdentifierCardSummary];
174 }
175
176 // Accepted payment methods section.
177 CollectionViewItem* acceptedMethodsItem =
178 [_dataSource acceptedPaymentMethodsItem];
179 if (acceptedMethodsItem) {
180 [model addSectionWithIdentifier:SectionIdentifierAcceptedMethods];
181 acceptedMethodsItem.type = ItemTypeAcceptedMethods;
182 [model addItem:acceptedMethodsItem
183 toSectionWithIdentifier:SectionIdentifierAcceptedMethods];
184 }
185 }
186
187 - (void)loadFooterItems { 139 - (void)loadFooterItems {
188 CollectionViewModel* model = self.collectionViewModel; 140 CollectionViewModel* model = self.collectionViewModel;
189 141
190 // Billing Address section.
191 [model addSectionWithIdentifier:SectionIdentifierBillingAddress];
192 CollectionViewDetailItem* billingAddressItem =
193 [[CollectionViewDetailItem alloc] initWithType:ItemTypeBillingAddress];
194 billingAddressItem.text = [NSString
195 stringWithFormat:@"%@*",
196 l10n_util::GetNSString(IDS_PAYMENTS_BILLING_ADDRESS)];
197 billingAddressItem.accessoryType =
198 MDCCollectionViewCellAccessoryDisclosureIndicator;
199 [model addItem:billingAddressItem
200 toSectionWithIdentifier:SectionIdentifierBillingAddress];
201
202 if (_billingAddressGUID) {
203 billingAddressItem.detailText =
204 [_dataSource billingAddressLabelForProfileWithGUID:_billingAddressGUID];
205 }
206
207 // "Save card" section. Visible only when creating a card. 142 // "Save card" section. Visible only when creating a card.
208 if (_dataSource.state == CreditCardEditViewControllerStateCreate) { 143 if (_dataSource.state == EditViewControllerStateCreate) {
209 [model addSectionWithIdentifier:SectionIdentifierSaveCard]; 144 [model addSectionWithIdentifier:SectionIdentifierSaveCard];
210 CollectionViewSwitchItem* saveCardItem = 145 CollectionViewSwitchItem* saveCardItem =
211 [[CollectionViewSwitchItem alloc] initWithType:ItemTypeSaveCard]; 146 [[CollectionViewSwitchItem alloc] initWithType:ItemTypeSaveCard];
212 saveCardItem.text = 147 saveCardItem.text =
213 l10n_util::GetNSString(IDS_PAYMENTS_SAVE_CARD_TO_DEVICE_CHECKBOX); 148 l10n_util::GetNSString(IDS_PAYMENTS_SAVE_CARD_TO_DEVICE_CHECKBOX);
214 saveCardItem.on = _saveCreditCard; 149 saveCardItem.on = _saveCreditCard;
215 [model addItem:saveCardItem 150 [model addItem:saveCardItem
216 toSectionWithIdentifier:SectionIdentifierSaveCard]; 151 toSectionWithIdentifier:SectionIdentifierSaveCard];
217 } 152 }
218 153
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
252 #pragma mark - UICollectionViewDataSource 187 #pragma mark - UICollectionViewDataSource
253 188
254 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView 189 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
255 cellForItemAtIndexPath:(NSIndexPath*)indexPath { 190 cellForItemAtIndexPath:(NSIndexPath*)indexPath {
256 UICollectionViewCell* cell = 191 UICollectionViewCell* cell =
257 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; 192 [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
258 CollectionViewItem* item = 193 CollectionViewItem* item =
259 [self.collectionViewModel itemAtIndexPath:indexPath]; 194 [self.collectionViewModel itemAtIndexPath:indexPath];
260 195
261 switch (item.type) { 196 switch (item.type) {
262 case ItemTypeBillingAddress: {
263 CollectionViewDetailCell* billingCell =
264 base::mac::ObjCCastStrict<CollectionViewDetailCell>(cell);
265 billingCell.textLabel.font = [MDCTypography body2Font];
266 billingCell.textLabel.textColor = [[MDCPalette greyPalette] tint900];
267 billingCell.detailTextLabel.font = [MDCTypography body1Font];
268 billingCell.detailTextLabel.textColor =
269 [[MDCPalette cr_bluePalette] tint600];
270 break;
271 }
272 case ItemTypeSaveCard: { 197 case ItemTypeSaveCard: {
273 CollectionViewSwitchCell* switchCell = 198 CollectionViewSwitchCell* switchCell =
274 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); 199 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell);
275 [switchCell.switchView addTarget:self 200 [switchCell.switchView addTarget:self
276 action:@selector(saveCardSwitchToggled:) 201 action:@selector(saveCardSwitchToggled:)
277 forControlEvents:UIControlEventValueChanged]; 202 forControlEvents:UIControlEventValueChanged];
278 break; 203 break;
279 } 204 }
280 default: 205 default:
281 break; 206 break;
282 } 207 }
283 208
284 return cell; 209 return cell;
285 } 210 }
286 211
287 #pragma mark UICollectionViewDelegate
288
289 - (void)collectionView:(UICollectionView*)collectionView
290 didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
291 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
292
293 CollectionViewItem* item =
294 [self.collectionViewModel itemAtIndexPath:indexPath];
295 switch (item.type) {
296 case ItemTypeAcceptedMethods:
297 case ItemTypeCardSummary:
298 case ItemTypeSaveCard:
299 break;
300 case ItemTypeBillingAddress: {
301 // TODO(crbug.com/602666): Display a list of billing addresses.
302 break;
303 }
304 default:
305 break;
306 }
307 }
308
309 #pragma mark MDCCollectionViewStylingDelegate 212 #pragma mark MDCCollectionViewStylingDelegate
310 213
311 - (CGFloat)collectionView:(UICollectionView*)collectionView 214 - (CGFloat)collectionView:(UICollectionView*)collectionView
312 cellHeightAtIndexPath:(NSIndexPath*)indexPath { 215 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
313 CollectionViewItem* item = 216 CollectionViewItem* item =
314 [self.collectionViewModel itemAtIndexPath:indexPath]; 217 [self.collectionViewModel itemAtIndexPath:indexPath];
315 switch (item.type) { 218 switch (item.type) {
316 case ItemTypeBillingAddress:
317 return MDCCellDefaultOneLineHeight;
318 case ItemTypeAcceptedMethods:
319 case ItemTypeCardSummary:
320 case ItemTypeSaveCard: 219 case ItemTypeSaveCard:
321 return [MDCCollectionViewCell 220 return [MDCCollectionViewCell
322 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) 221 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
323 forItem:item]; 222 forItem:item];
324 default: 223 default:
325 return 224 return
326 [super collectionView:collectionView cellHeightAtIndexPath:indexPath]; 225 [super collectionView:collectionView cellHeightAtIndexPath:indexPath];
327 } 226 }
328 } 227 }
329 228
330 - (BOOL)collectionView:(UICollectionView*)collectionView 229 - (BOOL)collectionView:(UICollectionView*)collectionView
331 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { 230 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
332 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; 231 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
333 switch (type) { 232 switch (type) {
334 case ItemTypeAcceptedMethods:
335 case ItemTypeCardSummary:
336 case ItemTypeSaveCard: 233 case ItemTypeSaveCard:
337 return YES; 234 return YES;
338 default: 235 default:
339 return [super collectionView:collectionView 236 return [super collectionView:collectionView
340 hidesInkViewAtIndexPath:indexPath]; 237 hidesInkViewAtIndexPath:indexPath];
341 } 238 }
342 } 239 }
343 240
344 - (BOOL)collectionView:(UICollectionView*)collectionView
345 shouldHideItemBackgroundAtIndexPath:(NSIndexPath*)indexPath {
346 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
347 switch (type) {
348 case ItemTypeAcceptedMethods:
349 return YES;
350 default:
351 return [super collectionView:collectionView
352 shouldHideItemBackgroundAtIndexPath:indexPath];
353 }
354 }
355
356 #pragma mark Switch Actions 241 #pragma mark Switch Actions
357 242
358 - (void)saveCardSwitchToggled:(UISwitch*)sender { 243 - (void)saveCardSwitchToggled:(UISwitch*)sender {
359 _saveCreditCard = sender.isOn; 244 _saveCreditCard = sender.isOn;
360 } 245 }
361 246
362 @end 247 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698