forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsc_payments_editor_coordinator.mm
184 lines (148 loc) · 6.44 KB
/
sc_payments_editor_coordinator.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/showcase/payments/sc_payments_editor_coordinator.h"
#import "base/mac/foundation_util.h"
#include "base/strings/utf_string_conversions.h"
#import "ios/chrome/browser/ui/autofill/autofill_ui_type.h"
#import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h"
#import "ios/chrome/browser/ui/autofill/cells/autofill_edit_item.h"
#import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrome.h"
#import "ios/chrome/browser/ui/payments/cells/payments_text_item.h"
#import "ios/chrome/browser/ui/payments/payment_request_edit_consumer.h"
#import "ios/chrome/browser/ui/payments/payment_request_edit_view_controller.h"
#import "ios/chrome/browser/ui/payments/payment_request_editor_field.h"
#import "ios/showcase/common/protocol_alerter.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface SCPaymentsEditorMediator
: NSObject<PaymentRequestEditViewControllerDataSource,
PaymentRequestEditViewControllerValidator>
// The reference to the city/province field.
@property(nonatomic, strong) EditorField* cityProvince;
// The consumer for this object.
@property(nonatomic, weak) id<PaymentRequestEditConsumer> consumer;
@end
@implementation SCPaymentsEditorMediator
@synthesize state = _state;
@synthesize cityProvince = _cityProvince;
@synthesize consumer = _consumer;
- (void)setConsumer:(id<PaymentRequestEditConsumer>)consumer {
_consumer = consumer;
[self.consumer setEditorFields:[self editorFields]];
}
- (void)loadCitiesAndProvinces {
NSArray<NSString*>* cities = @[ @"Ottawa", @"Montreal" ];
NSArray<NSString*>* provinces = @[ @"Quebec", @"Ontario" ];
self.cityProvince.value =
[NSString stringWithFormat:@"%@ / %@", cities[1], provinces[0]];
self.cityProvince.enabled = YES;
[self.consumer setOptions:@[ cities, provinces ]
forEditorField:self.cityProvince];
}
#pragma mark - Helper methods
- (NSArray<EditorField*>*)editorFields {
EditorField* name =
[[EditorField alloc] initWithAutofillUIType:AutofillUITypeProfileFullName
fieldType:EditorFieldTypeTextField
label:@"Name"
value:@"John Doe"
required:YES];
EditorField* country = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeProfileHomeAddressCountry
fieldType:EditorFieldTypeSelector
label:@"Country"
value:@"CAN"
required:YES];
[country setDisplayValue:@"Canada"];
self.cityProvince = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeProfileHomeAddressState
fieldType:EditorFieldTypeTextField
label:@"City/Province"
value:@"Loading..."
required:YES];
self.cityProvince.enabled = NO;
EditorField* address = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeProfileHomeAddressStreet
fieldType:EditorFieldTypeTextField
label:@"Address"
value:@""
required:YES];
EditorField* postalCode = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeProfileHomeAddressZip
fieldType:EditorFieldTypeTextField
label:@"Postal Code"
value:@""
required:NO];
EditorField* save = [[EditorField alloc]
initWithAutofillUIType:AutofillUITypeCreditCardSaveToChrome
fieldType:EditorFieldTypeSwitch
label:@"Save"
value:@"YES"
required:NO];
return @[ name, country, self.cityProvince, address, postalCode, save ];
}
#pragma mark - PaymentRequestEditViewControllerDataSource
- (NSString*)title {
return nil;
}
- (CollectionViewItem*)headerItem {
return nil;
}
- (BOOL)shouldHideBackgroundForHeaderItem {
return NO;
}
- (BOOL)shouldFormatValueForAutofillUIType:(AutofillUIType)type {
return NO;
}
- (NSString*)formatValue:(NSString*)value autofillUIType:(AutofillUIType)type {
return nil;
}
- (UIImage*)iconIdentifyingEditorField:(EditorField*)field {
return nil;
}
#pragma mark - PaymentRequestEditViewControllerValidator
- (NSString*)paymentRequestEditViewController:
(PaymentRequestEditViewController*)controller
validateField:(EditorField*)field {
return (!field.value.length && field.isRequired) ? @"Field is required" : nil;
}
@end
@interface SCPaymentsEditorCoordinator ()
@property(nonatomic, strong)
PaymentRequestEditViewController* paymentRequestEditViewController;
@property(nonatomic, strong) SCPaymentsEditorMediator* mediator;
@property(nonatomic, strong) ProtocolAlerter* alerter;
@end
@implementation SCPaymentsEditorCoordinator
@synthesize baseViewController = _baseViewController;
@synthesize paymentRequestEditViewController =
_paymentRequestEditViewController;
@synthesize mediator = _mediator;
@synthesize alerter = _alerter;
- (void)start {
self.alerter = [[ProtocolAlerter alloc] initWithProtocols:@[
@protocol(PaymentRequestEditViewControllerDelegate)
]];
self.alerter.baseViewController = self.baseViewController;
self.paymentRequestEditViewController =
[[PaymentRequestEditViewController alloc]
initWithLayout:[[MDCCollectionViewFlowLayout alloc] init]
style:CollectionViewControllerStyleAppBar];
[self.paymentRequestEditViewController setTitle:@"Add info"];
self.mediator = [[SCPaymentsEditorMediator alloc] init];
[self.mediator setConsumer:self.paymentRequestEditViewController];
[self.paymentRequestEditViewController setDataSource:self.mediator];
[self.paymentRequestEditViewController
setDelegate:static_cast<id<PaymentRequestEditViewControllerDelegate>>(
self.alerter)];
[self.paymentRequestEditViewController setValidatorDelegate:self.mediator];
[self.paymentRequestEditViewController loadModel];
// Set the options for the city/province field after the model is loaded.
[self.mediator loadCitiesAndProvinces];
[self.baseViewController
pushViewController:self.paymentRequestEditViewController
animated:YES];
}
@end