OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/shipping_address_selection_coordinator.h
" | 5 #import "ios/chrome/browser/ui/payments/shipping_address_selection_coordinator.h
" |
6 | 6 |
7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
8 #include "components/autofill/core/browser/autofill_profile.h" | 8 #include "components/autofill/core/browser/autofill_profile.h" |
9 #include "components/payments/core/strings_util.h" | 9 #include "components/payments/core/strings_util.h" |
10 #include "ios/chrome/browser/payments/payment_request.h" | 10 #include "ios/chrome/browser/payments/payment_request.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 @interface ShippingAddressSelectionCoordinator () | 28 @interface ShippingAddressSelectionCoordinator () |
29 | 29 |
30 @property(nonatomic, strong) AddressEditCoordinator* addressEditCoordinator; | 30 @property(nonatomic, strong) AddressEditCoordinator* addressEditCoordinator; |
31 | 31 |
32 @property(nonatomic, strong) | 32 @property(nonatomic, strong) |
33 PaymentRequestSelectorViewController* viewController; | 33 PaymentRequestSelectorViewController* viewController; |
34 | 34 |
35 @property(nonatomic, strong) ShippingAddressSelectionMediator* mediator; | 35 @property(nonatomic, strong) ShippingAddressSelectionMediator* mediator; |
36 | 36 |
| 37 // Initializes and starts the AddressEditCoordinator. Sets |address| as the |
| 38 // address to be edited. |
| 39 - (void)startAddressEditCoordinatorWithAddress: |
| 40 (autofill::AutofillProfile*)address; |
| 41 |
37 // Called when the user selects a shipping address. The cell is checked, the | 42 // Called when the user selects a shipping address. The cell is checked, the |
38 // UI is locked so that the user can't interact with it, then the delegate is | 43 // UI is locked so that the user can't interact with it, then the delegate is |
39 // notified. The delay is here to let the user get a visual feedback of the | 44 // notified. The delay is here to let the user get a visual feedback of the |
40 // selection before this view disappears. | 45 // selection before this view disappears. |
41 - (void)delayedNotifyDelegateOfSelection: | 46 - (void)delayedNotifyDelegateOfSelection: |
42 (autofill::AutofillProfile*)shippingAddress; | 47 (autofill::AutofillProfile*)shippingAddress; |
43 | 48 |
44 @end | 49 @end |
45 | 50 |
46 @implementation ShippingAddressSelectionCoordinator | 51 @implementation ShippingAddressSelectionCoordinator |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 ->shipping_profiles()[index]]; | 112 ->shipping_profiles()[index]]; |
108 } | 113 } |
109 | 114 |
110 - (void)paymentRequestSelectorViewControllerDidFinish: | 115 - (void)paymentRequestSelectorViewControllerDidFinish: |
111 (PaymentRequestSelectorViewController*)controller { | 116 (PaymentRequestSelectorViewController*)controller { |
112 [self.delegate shippingAddressSelectionCoordinatorDidReturn:self]; | 117 [self.delegate shippingAddressSelectionCoordinatorDidReturn:self]; |
113 } | 118 } |
114 | 119 |
115 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: | 120 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: |
116 (PaymentRequestSelectorViewController*)controller { | 121 (PaymentRequestSelectorViewController*)controller { |
117 self.addressEditCoordinator = [[AddressEditCoordinator alloc] | 122 [self startAddressEditCoordinatorWithAddress:nil]; |
118 initWithBaseViewController:self.viewController]; | 123 } |
119 self.addressEditCoordinator.paymentRequest = self.paymentRequest; | 124 |
120 self.addressEditCoordinator.delegate = self; | 125 - (void)paymentRequestSelectorViewController: |
121 [self.addressEditCoordinator start]; | 126 (PaymentRequestSelectorViewController*)controller |
| 127 didChangeToState: |
| 128 (PaymentRequestSelectorState)state { |
| 129 // Update the data source with the new state. |
| 130 self.mediator.state = state; |
| 131 |
| 132 [self.viewController loadModel]; |
| 133 [self.viewController.collectionView reloadData]; |
| 134 } |
| 135 |
| 136 - (void)paymentRequestSelectorViewController: |
| 137 (PaymentRequestSelectorViewController*)controller |
| 138 didSelectItemAtIndexForEditing:(NSUInteger)index { |
| 139 DCHECK(index < self.paymentRequest->shipping_profiles().size()); |
| 140 [self |
| 141 startAddressEditCoordinatorWithAddress:self.paymentRequest |
| 142 ->shipping_profiles()[index]]; |
122 } | 143 } |
123 | 144 |
124 #pragma mark - AddressEditCoordinatorDelegate | 145 #pragma mark - AddressEditCoordinatorDelegate |
125 | 146 |
126 - (void)addressEditCoordinator:(AddressEditCoordinator*)coordinator | 147 - (void)addressEditCoordinator:(AddressEditCoordinator*)coordinator |
127 didFinishEditingAddress:(autofill::AutofillProfile*)address { | 148 didFinishEditingAddress:(autofill::AutofillProfile*)address { |
| 149 // Update the data source with the new data. |
| 150 [self.mediator createItems]; |
| 151 |
| 152 [self.viewController loadModel]; |
| 153 [self.viewController.collectionView reloadData]; |
| 154 |
128 [self.addressEditCoordinator stop]; | 155 [self.addressEditCoordinator stop]; |
129 self.addressEditCoordinator = nil; | 156 self.addressEditCoordinator = nil; |
130 | 157 |
131 // Inform |self.delegate| that |address| has been selected. | 158 if (self.mediator.state != PaymentRequestSelectorStateEdit) { |
132 [self.delegate shippingAddressSelectionCoordinator:self | 159 // Inform |self.delegate| that |address| has been selected. |
133 didSelectShippingAddress:address]; | 160 [self.delegate shippingAddressSelectionCoordinator:self |
| 161 didSelectShippingAddress:address]; |
| 162 } |
134 } | 163 } |
135 | 164 |
136 - (void)addressEditCoordinatorDidCancel:(AddressEditCoordinator*)coordinator { | 165 - (void)addressEditCoordinatorDidCancel:(AddressEditCoordinator*)coordinator { |
137 [self.addressEditCoordinator stop]; | 166 [self.addressEditCoordinator stop]; |
138 self.addressEditCoordinator = nil; | 167 self.addressEditCoordinator = nil; |
139 } | 168 } |
140 | 169 |
141 #pragma mark - Helper methods | 170 #pragma mark - Helper methods |
142 | 171 |
| 172 - (void)startAddressEditCoordinatorWithAddress: |
| 173 (autofill::AutofillProfile*)address { |
| 174 self.addressEditCoordinator = [[AddressEditCoordinator alloc] |
| 175 initWithBaseViewController:self.viewController]; |
| 176 self.addressEditCoordinator.paymentRequest = self.paymentRequest; |
| 177 self.addressEditCoordinator.address = address; |
| 178 self.addressEditCoordinator.delegate = self; |
| 179 [self.addressEditCoordinator start]; |
| 180 } |
| 181 |
143 - (void)delayedNotifyDelegateOfSelection: | 182 - (void)delayedNotifyDelegateOfSelection: |
144 (autofill::AutofillProfile*)shippingAddress { | 183 (autofill::AutofillProfile*)shippingAddress { |
145 self.viewController.view.userInteractionEnabled = NO; | 184 self.viewController.view.userInteractionEnabled = NO; |
146 __weak ShippingAddressSelectionCoordinator* weakSelf = self; | 185 __weak ShippingAddressSelectionCoordinator* weakSelf = self; |
147 dispatch_after( | 186 dispatch_after( |
148 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), | 187 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), |
149 dispatch_get_main_queue(), ^{ | 188 dispatch_get_main_queue(), ^{ |
150 [weakSelf.mediator setState:PaymentRequestSelectorStatePending]; | 189 [weakSelf.mediator setState:PaymentRequestSelectorStatePending]; |
151 [weakSelf.viewController loadModel]; | 190 [weakSelf.viewController loadModel]; |
152 [weakSelf.viewController.collectionView reloadData]; | 191 [weakSelf.viewController.collectionView reloadData]; |
153 | 192 |
154 [weakSelf.delegate shippingAddressSelectionCoordinator:weakSelf | 193 [weakSelf.delegate shippingAddressSelectionCoordinator:weakSelf |
155 didSelectShippingAddress:shippingAddress]; | 194 didSelectShippingAddress:shippingAddress]; |
156 }); | 195 }); |
157 } | 196 } |
158 | 197 |
159 @end | 198 @end |
OLD | NEW |