OLD | NEW |
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/billing_address_selection_coordinator.h" | 5 #import "ios/chrome/browser/ui/payments/billing_address_selection_coordinator.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "components/autofill/core/browser/autofill_profile.h" | 8 #include "components/autofill/core/browser/autofill_profile.h" |
9 #include "components/strings/grit/components_strings.h" | 9 #include "components/strings/grit/components_strings.h" |
10 #include "ios/chrome/browser/payments/payment_request.h" | 10 #include "ios/chrome/browser/payments/payment_request.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 @interface BillingAddressSelectionCoordinator () | 23 @interface BillingAddressSelectionCoordinator () |
24 | 24 |
25 @property(nonatomic, strong) AddressEditCoordinator* addressEditCoordinator; | 25 @property(nonatomic, strong) AddressEditCoordinator* addressEditCoordinator; |
26 | 26 |
27 @property(nonatomic, strong) | 27 @property(nonatomic, strong) |
28 PaymentRequestSelectorViewController* viewController; | 28 PaymentRequestSelectorViewController* viewController; |
29 | 29 |
30 @property(nonatomic, strong) BillingAddressSelectionMediator* mediator; | 30 @property(nonatomic, strong) BillingAddressSelectionMediator* mediator; |
31 | 31 |
| 32 // Initializes and starts the AddressEditCoordinator. Sets |address| as the |
| 33 // address to be edited. |
| 34 - (void)startAddressEditCoordinatorWithAddress: |
| 35 (autofill::AutofillProfile*)address; |
| 36 |
32 // Called when the user selects a billing address. The cell is checked, the | 37 // Called when the user selects a billing address. The cell is checked, the |
33 // UI is locked so that the user can't interact with it, then the delegate is | 38 // UI is locked so that the user can't interact with it, then the delegate is |
34 // notified. The delay is here to let the user get a visual feedback of the | 39 // notified. The delay is here to let the user get a visual feedback of the |
35 // selection before this view disappears. | 40 // selection before this view disappears. |
36 - (void)delayedNotifyDelegateOfSelection: | 41 - (void)delayedNotifyDelegateOfSelection: |
37 (autofill::AutofillProfile*)billingAddress; | 42 (autofill::AutofillProfile*)billingAddress; |
38 | 43 |
39 @end | 44 @end |
40 | 45 |
41 @implementation BillingAddressSelectionCoordinator | 46 @implementation BillingAddressSelectionCoordinator |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
86 ->billing_profiles()[index]]; | 91 ->billing_profiles()[index]]; |
87 } | 92 } |
88 | 93 |
89 - (void)paymentRequestSelectorViewControllerDidFinish: | 94 - (void)paymentRequestSelectorViewControllerDidFinish: |
90 (PaymentRequestSelectorViewController*)controller { | 95 (PaymentRequestSelectorViewController*)controller { |
91 [self.delegate billingAddressSelectionCoordinatorDidReturn:self]; | 96 [self.delegate billingAddressSelectionCoordinatorDidReturn:self]; |
92 } | 97 } |
93 | 98 |
94 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: | 99 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: |
95 (PaymentRequestSelectorViewController*)controller { | 100 (PaymentRequestSelectorViewController*)controller { |
96 self.addressEditCoordinator = [[AddressEditCoordinator alloc] | 101 [self startAddressEditCoordinatorWithAddress:nil]; |
97 initWithBaseViewController:self.viewController]; | 102 } |
98 self.addressEditCoordinator.paymentRequest = self.paymentRequest; | 103 |
99 self.addressEditCoordinator.delegate = self; | 104 - (void)paymentRequestSelectorViewController: |
100 [self.addressEditCoordinator start]; | 105 (PaymentRequestSelectorViewController*)controller |
| 106 didChangeToState: |
| 107 (PaymentRequestSelectorState)state { |
| 108 // Update the data source with the new state. |
| 109 self.mediator.state = state; |
| 110 |
| 111 [self.viewController loadModel]; |
| 112 [self.viewController.collectionView reloadData]; |
| 113 } |
| 114 |
| 115 - (void)paymentRequestSelectorViewController: |
| 116 (PaymentRequestSelectorViewController*)controller |
| 117 didSelectItemAtIndexForEditing:(NSUInteger)index { |
| 118 DCHECK(index < self.paymentRequest->billing_profiles().size()); |
| 119 [self startAddressEditCoordinatorWithAddress:self.paymentRequest |
| 120 ->billing_profiles()[index]]; |
101 } | 121 } |
102 | 122 |
103 #pragma mark - AddressEditCoordinatorDelegate | 123 #pragma mark - AddressEditCoordinatorDelegate |
104 | 124 |
105 - (void)addressEditCoordinator:(AddressEditCoordinator*)coordinator | 125 - (void)addressEditCoordinator:(AddressEditCoordinator*)coordinator |
106 didFinishEditingAddress:(autofill::AutofillProfile*)address { | 126 didFinishEditingAddress:(autofill::AutofillProfile*)address { |
| 127 // Update the data source with the new data. |
| 128 [self.mediator createItems]; |
| 129 |
| 130 [self.viewController loadModel]; |
| 131 [self.viewController.collectionView reloadData]; |
| 132 |
107 [self.addressEditCoordinator stop]; | 133 [self.addressEditCoordinator stop]; |
108 self.addressEditCoordinator = nil; | 134 self.addressEditCoordinator = nil; |
109 | 135 |
110 // Inform |self.delegate| that |address| has been selected. | 136 if (self.mediator.state != PaymentRequestSelectorStateEdit) { |
111 [self.delegate billingAddressSelectionCoordinator:self | 137 // Inform |self.delegate| that |address| has been selected. |
112 didSelectBillingAddress:address]; | 138 [self.delegate billingAddressSelectionCoordinator:self |
| 139 didSelectBillingAddress:address]; |
| 140 } |
113 } | 141 } |
114 | 142 |
115 - (void)addressEditCoordinatorDidCancel:(AddressEditCoordinator*)coordinator { | 143 - (void)addressEditCoordinatorDidCancel:(AddressEditCoordinator*)coordinator { |
116 [self.addressEditCoordinator stop]; | 144 [self.addressEditCoordinator stop]; |
117 self.addressEditCoordinator = nil; | 145 self.addressEditCoordinator = nil; |
118 } | 146 } |
119 | 147 |
120 #pragma mark - Helper methods | 148 #pragma mark - Helper methods |
121 | 149 |
| 150 - (void)startAddressEditCoordinatorWithAddress: |
| 151 (autofill::AutofillProfile*)address { |
| 152 self.addressEditCoordinator = [[AddressEditCoordinator alloc] |
| 153 initWithBaseViewController:self.viewController]; |
| 154 self.addressEditCoordinator.paymentRequest = self.paymentRequest; |
| 155 self.addressEditCoordinator.address = address; |
| 156 self.addressEditCoordinator.delegate = self; |
| 157 [self.addressEditCoordinator start]; |
| 158 } |
| 159 |
122 - (void)delayedNotifyDelegateOfSelection: | 160 - (void)delayedNotifyDelegateOfSelection: |
123 (autofill::AutofillProfile*)billingAddress { | 161 (autofill::AutofillProfile*)billingAddress { |
124 self.viewController.view.userInteractionEnabled = NO; | 162 self.viewController.view.userInteractionEnabled = NO; |
125 __weak BillingAddressSelectionCoordinator* weakSelf = self; | 163 __weak BillingAddressSelectionCoordinator* weakSelf = self; |
126 dispatch_after( | 164 dispatch_after( |
127 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), | 165 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), |
128 dispatch_get_main_queue(), ^{ | 166 dispatch_get_main_queue(), ^{ |
129 [weakSelf.delegate billingAddressSelectionCoordinator:weakSelf | 167 [weakSelf.delegate billingAddressSelectionCoordinator:weakSelf |
130 didSelectBillingAddress:billingAddress]; | 168 didSelectBillingAddress:billingAddress]; |
131 }); | 169 }); |
132 } | 170 } |
133 | 171 |
134 @end | 172 @end |
OLD | NEW |