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/payments/shipping_address_selection_coordinator.h" | 5 #import "ios/chrome/browser/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 "ios/chrome/browser/payments/payment_request.h" | 10 #include "ios/chrome/browser/payments/payment_request.h" |
| 11 #include "ios/chrome/browser/payments/payment_request_selector_view_controller.h
" |
10 #import "ios/chrome/browser/payments/payment_request_util.h" | 12 #import "ios/chrome/browser/payments/payment_request_util.h" |
| 13 #include "ios/chrome/browser/payments/shipping_address_selection_mediator.h" |
11 | 14 |
12 #if !defined(__has_feature) || !__has_feature(objc_arc) | 15 #if !defined(__has_feature) || !__has_feature(objc_arc) |
13 #error "This file requires ARC support." | 16 #error "This file requires ARC support." |
14 #endif | 17 #endif |
15 | 18 |
16 namespace { | 19 namespace { |
17 using ::payment_request_util::GetShippingAddressSelectorErrorMessage; | 20 using ::payment_request_util::GetShippingAddressSelectorErrorMessage; |
| 21 using ::payments::GetShippingAddressSectionString; |
| 22 using ::payments::GetShippingAddressSelectorInfoMessage; |
18 } // namespace | 23 } // namespace |
19 | 24 |
20 @interface ShippingAddressSelectionCoordinator () | 25 @interface ShippingAddressSelectionCoordinator () |
21 | 26 |
22 @property(nonatomic, strong) | 27 @property(nonatomic, strong) |
23 ShippingAddressSelectionViewController* viewController; | 28 PaymentRequestSelectorViewController* viewController; |
| 29 |
| 30 @property(nonatomic, strong) ShippingAddressSelectionMediator* mediator; |
24 | 31 |
25 // Called when the user selects a shipping address. The cell is checked, the | 32 // Called when the user selects a shipping address. The cell is checked, the |
26 // UI is locked so that the user can't interact with it, then the delegate is | 33 // UI is locked so that the user can't interact with it, then the delegate is |
27 // notified. The delay is here to let the user get a visual feedback of the | 34 // notified. The delay is here to let the user get a visual feedback of the |
28 // selection before this view disappears. | 35 // selection before this view disappears. |
29 - (void)delayedNotifyDelegateOfSelection: | 36 - (void)delayedNotifyDelegateOfSelection: |
30 (autofill::AutofillProfile*)shippingAddress; | 37 (autofill::AutofillProfile*)shippingAddress; |
31 | 38 |
32 @end | 39 @end |
33 | 40 |
34 @implementation ShippingAddressSelectionCoordinator | 41 @implementation ShippingAddressSelectionCoordinator |
35 | 42 |
36 @synthesize paymentRequest = _paymentRequest; | 43 @synthesize paymentRequest = _paymentRequest; |
37 @synthesize delegate = _delegate; | 44 @synthesize delegate = _delegate; |
38 @synthesize viewController = _viewController; | 45 @synthesize viewController = _viewController; |
| 46 @synthesize mediator = _mediator; |
39 | 47 |
40 - (void)start { | 48 - (void)start { |
41 _viewController = [[ShippingAddressSelectionViewController alloc] | 49 self.mediator = [[ShippingAddressSelectionMediator alloc] |
42 initWithPaymentRequest:_paymentRequest]; | 50 initWithPaymentRequest:self.paymentRequest]; |
43 [_viewController setDelegate:self]; | 51 self.mediator.headerText = |
44 [_viewController loadModel]; | 52 self.paymentRequest->shipping_options().empty() |
| 53 ? base::SysUTF16ToNSString(GetShippingAddressSelectorInfoMessage( |
| 54 self.paymentRequest->shipping_type())) |
| 55 : nil; |
| 56 |
| 57 self.viewController = [[PaymentRequestSelectorViewController alloc] init]; |
| 58 self.viewController.title = base::SysUTF16ToNSString( |
| 59 GetShippingAddressSectionString(self.paymentRequest->shipping_type())); |
| 60 self.viewController.delegate = self; |
| 61 self.viewController.dataSource = self.mediator; |
| 62 [self.viewController loadModel]; |
45 | 63 |
46 DCHECK(self.baseViewController.navigationController); | 64 DCHECK(self.baseViewController.navigationController); |
47 [self.baseViewController.navigationController | 65 [self.baseViewController.navigationController |
48 pushViewController:_viewController | 66 pushViewController:self.viewController |
49 animated:YES]; | 67 animated:YES]; |
50 } | 68 } |
51 | 69 |
52 - (void)stop { | 70 - (void)stop { |
53 [self.baseViewController.navigationController popViewControllerAnimated:YES]; | 71 [self.baseViewController.navigationController popViewControllerAnimated:YES]; |
54 _viewController = nil; | 72 self.viewController = nil; |
| 73 self.mediator = nil; |
55 } | 74 } |
56 | 75 |
57 - (void)stopSpinnerAndDisplayError { | 76 - (void)stopSpinnerAndDisplayError { |
58 // Re-enable user interactions that were disabled earlier in | 77 // Re-enable user interactions that were disabled earlier in |
59 // delayedNotifyDelegateOfSelection. | 78 // delayedNotifyDelegateOfSelection. |
60 _viewController.view.userInteractionEnabled = YES; | 79 self.viewController.view.userInteractionEnabled = YES; |
61 | 80 |
62 [_viewController setPending:NO]; | 81 DCHECK(self.paymentRequest); |
63 DCHECK(_paymentRequest); | 82 self.mediator.headerText = |
64 [_viewController | 83 GetShippingAddressSelectorErrorMessage(*self.paymentRequest); |
65 setErrorMessage:GetShippingAddressSelectorErrorMessage(*_paymentRequest)]; | 84 self.mediator.state = PaymentRequestSelectorStateError; |
66 [_viewController loadModel]; | 85 [self.viewController loadModel]; |
67 [[_viewController collectionView] reloadData]; | 86 [self.viewController.collectionView reloadData]; |
68 } | 87 } |
69 | 88 |
70 #pragma mark - ShippingAddressSelectionViewControllerDelegate | 89 #pragma mark - PaymentRequestSelectorViewControllerDelegate |
71 | 90 |
72 - (void)shippingAddressSelectionViewController: | 91 - (void)paymentRequestSelectorViewController: |
73 (ShippingAddressSelectionViewController*)controller | 92 (PaymentRequestSelectorViewController*)controller |
74 didSelectShippingAddress: | 93 didSelectItemAtIndex:(NSUInteger)index { |
75 (autofill::AutofillProfile*)shippingAddress { | 94 // Update the data source with the selection. |
76 [self delayedNotifyDelegateOfSelection:shippingAddress]; | 95 self.mediator.selectedItemIndex = index; |
| 96 |
| 97 DCHECK(index < self.paymentRequest->shipping_profiles().size()); |
| 98 [self delayedNotifyDelegateOfSelection:self.paymentRequest |
| 99 ->shipping_profiles()[index]]; |
77 } | 100 } |
78 | 101 |
79 - (void)shippingAddressSelectionViewControllerDidReturn: | 102 - (void)paymentRequestSelectorViewControllerDidFinish: |
80 (ShippingAddressSelectionViewController*)controller { | 103 (PaymentRequestSelectorViewController*)controller { |
81 [_delegate shippingAddressSelectionCoordinatorDidReturn:self]; | 104 [self.delegate shippingAddressSelectionCoordinatorDidReturn:self]; |
82 } | 105 } |
83 | 106 |
| 107 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: |
| 108 (PaymentRequestSelectorViewController*)controller { |
| 109 // TODO(crbug.com/602666): Present a shipping address addition UI. |
| 110 } |
| 111 |
| 112 #pragma mark - Helper methods |
| 113 |
84 - (void)delayedNotifyDelegateOfSelection: | 114 - (void)delayedNotifyDelegateOfSelection: |
85 (autofill::AutofillProfile*)shippingAddress { | 115 (autofill::AutofillProfile*)shippingAddress { |
86 _viewController.view.userInteractionEnabled = NO; | 116 self.viewController.view.userInteractionEnabled = NO; |
87 __weak ShippingAddressSelectionCoordinator* weakSelf = self; | 117 __weak ShippingAddressSelectionCoordinator* weakSelf = self; |
88 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, | 118 dispatch_after( |
89 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), | 119 dispatch_time(DISPATCH_TIME_NOW, |
90 dispatch_get_main_queue(), ^{ | 120 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), |
91 ShippingAddressSelectionCoordinator* strongSelf = weakSelf; | 121 dispatch_get_main_queue(), ^{ |
92 // Early return if the coordinator has been deallocated. | 122 [weakSelf.mediator setState:PaymentRequestSelectorStatePending]; |
93 if (!strongSelf) | 123 [weakSelf.viewController loadModel]; |
94 return; | 124 [[weakSelf.viewController collectionView] reloadData]; |
95 | 125 |
96 [strongSelf.viewController setPending:YES]; | 126 [weakSelf.delegate shippingAddressSelectionCoordinator:weakSelf |
97 [strongSelf.viewController loadModel]; | 127 didSelectShippingAddress:shippingAddress]; |
98 [[strongSelf.viewController collectionView] reloadData]; | 128 }); |
99 | |
100 [strongSelf.delegate | |
101 shippingAddressSelectionCoordinator:strongSelf | |
102 didSelectShippingAddress:shippingAddress]; | |
103 }); | |
104 } | 129 } |
105 | 130 |
106 @end | 131 @end |
OLD | NEW |