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

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

Issue 2908033002: [Payment Request] Refactors edit view controller (Closed)
Patch Set: Addressed comments Created 3 years, 6 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
(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/payments/credit_card_edit_view_controller.h"
6
7 #import "base/mac/foundation_util.h"
8 #include "base/memory/ptr_util.h"
9 #include "components/strings/grit/components_strings.h"
10 #import "ios/chrome/browser/ui/autofill/autofill_ui_type.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"
13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collec tion_view_controller.h"
14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item .h"
15 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
16 #import "ios/chrome/browser/ui/payments/payment_request_edit_view_controller+int ernal.h"
17 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h"
18 #import "ios/chrome/browser/ui/uikit_ui_util.h"
19 #include "ui/base/l10n/l10n_util.h"
20
21 #if !defined(__has_feature) || !__has_feature(objc_arc)
22 #error "This file requires ARC support."
23 #endif
24
25 namespace {
26
27 NSString* const kCreditCardEditCollectionViewId =
28 @"kCreditCardEditCollectionViewId";
29
30 typedef NS_ENUM(NSInteger, SectionIdentifier) {
31 SectionIdentifierSaveCard = kSectionIdentifierEnumStart,
32 };
33
34 typedef NS_ENUM(NSInteger, ItemType) {
35 ItemTypeSaveCard = kItemTypeEnumStart,
36 };
37
38 } // namespace
39
40 @interface CreditCardEditViewController () {
41 // Indicates whether the credit card being created should be saved locally.
42 BOOL _saveCreditCard;
43 }
44
45 // The list of field definitions for the editor.
46 @property(nonatomic, strong) NSArray<EditorField*>* fields;
47
48 @end
49
50 @implementation CreditCardEditViewController
51
52 @synthesize delegate = _delegate;
53 @synthesize dataSource = _dataSource;
54 @synthesize fields = _fields;
55
56 #pragma mark - Setters
57
58 - (void)setDelegate:(id<CreditCardEditViewControllerDelegate>)delegate {
59 [super setDelegate:delegate];
60 _delegate = delegate;
61 }
62
63 - (void)setDataSource:(id<CreditCardEditViewControllerDataSource>)dataSource {
64 [super setDataSource:dataSource];
65 _dataSource = dataSource;
66 }
67
68 #pragma mark - PaymentRequestEditViewControllerActions methods
69
70 - (void)onCancel {
71 [super onCancel];
72
73 [_delegate creditCardEditViewControllerDidCancel:self];
74 }
75
76 - (void)onDone {
77 [super onDone];
78
79 if (![self validateForm])
80 return;
81
82 [_delegate creditCardEditViewController:self
83 didFinishEditingFields:_fields
84 saveCreditCard:_saveCreditCard];
85 }
86
87 #pragma mark - CollectionViewController methods
88
89 - (void)loadModel {
90 [super loadModel];
91
92 // If editing a credit card, set the card type icon (e.g. "Visa").
93 if (_dataSource.state == EditViewControllerStateEdit) {
94 for (EditorField* field in _fields) {
95 if (field.autofillUIType == AutofillUITypeCreditCardNumber) {
96 AutofillEditItem* item =
97 base::mac::ObjCCastStrict<AutofillEditItem>(field.item);
98 item.identifyingIcon =
99 [_dataSource cardTypeIconFromCardNumber:item.textFieldValue];
100 }
101 }
102 }
103 }
104
105 - (void)loadFooterItems {
106 CollectionViewModel* model = self.collectionViewModel;
107
108 // "Save card" section. Visible only when creating a card.
109 if (_dataSource.state == EditViewControllerStateCreate) {
110 [model addSectionWithIdentifier:SectionIdentifierSaveCard];
111 CollectionViewSwitchItem* saveCardItem =
112 [[CollectionViewSwitchItem alloc] initWithType:ItemTypeSaveCard];
113 saveCardItem.text =
114 l10n_util::GetNSString(IDS_PAYMENTS_SAVE_CARD_TO_DEVICE_CHECKBOX);
115 saveCardItem.on = YES;
116 [model addItem:saveCardItem
117 toSectionWithIdentifier:SectionIdentifierSaveCard];
118 }
119
120 [super loadFooterItems];
121 }
122
123 #pragma mark - UITextFieldDelegate
124
125 // This method is called as the text is being typed in, pasted, or deleted. Asks
126 // the delegate if the text should be changed. Should always return YES. During
127 // typing/pasting text, |newText| contains one or more new characters. When user
128 // deletes text, |newText| is empty. |range| is the range of characters to be
129 // replaced.
130 - (BOOL)textField:(UITextField*)textField
131 shouldChangeCharactersInRange:(NSRange)range
132 replacementString:(NSString*)newText {
133 NSIndexPath* indexPath = [self indexPathForCurrentTextField];
134 AutofillEditItem* item = base::mac::ObjCCastStrict<AutofillEditItem>(
135 [self.collectionViewModel itemAtIndexPath:indexPath]);
136
137 // If the user is typing in the credit card number field, update the card type
138 // icon (e.g. "Visa") to reflect the number being typed.
139 if (item.autofillUIType == AutofillUITypeCreditCardNumber) {
140 // Obtain the text being typed.
141 NSString* updatedText =
142 [textField.text stringByReplacingCharactersInRange:range
143 withString:newText];
144 item.identifyingIcon = [_dataSource cardTypeIconFromCardNumber:updatedText];
145
146 // Update the cell.
147 [self reconfigureCellsForItems:@[ item ]];
148 }
149
150 return YES;
151 }
152
153 #pragma mark - UICollectionViewDataSource
154
155 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
156 cellForItemAtIndexPath:(NSIndexPath*)indexPath {
157 UICollectionViewCell* cell =
158 [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
159 CollectionViewItem* item =
160 [self.collectionViewModel itemAtIndexPath:indexPath];
161
162 switch (item.type) {
163 case ItemTypeSaveCard: {
164 CollectionViewSwitchCell* switchCell =
165 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell);
166 [switchCell.switchView addTarget:self
167 action:@selector(saveCardSwitchToggled:)
168 forControlEvents:UIControlEventValueChanged];
169 break;
170 }
171 default:
172 break;
173 }
174
175 return cell;
176 }
177
178 #pragma mark MDCCollectionViewStylingDelegate
179
180 - (CGFloat)collectionView:(UICollectionView*)collectionView
181 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
182 CollectionViewItem* item =
183 [self.collectionViewModel itemAtIndexPath:indexPath];
184 switch (item.type) {
185 case ItemTypeSaveCard:
186 return [MDCCollectionViewCell
187 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
188 forItem:item];
189 default:
190 return
191 [super collectionView:collectionView cellHeightAtIndexPath:indexPath];
192 }
193 }
194
195 - (BOOL)collectionView:(UICollectionView*)collectionView
196 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
197 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
198 switch (type) {
199 case ItemTypeSaveCard:
200 return YES;
201 default:
202 return [super collectionView:collectionView
203 hidesInkViewAtIndexPath:indexPath];
204 }
205 }
206
207 #pragma mark Switch Actions
208
209 - (void)saveCardSwitchToggled:(UISwitch*)sender {
210 _saveCreditCard = sender.isOn;
211 }
212
213 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698