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/payments/shipping_option_selection_coordinator.h" | 5 #import "ios/chrome/browser/payments/shipping_option_selection_coordinator.h" |
6 | 6 |
7 #include "base/logging.h" | |
7 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #include "components/payments/core/strings_util.h" | |
8 #include "ios/chrome/browser/payments/payment_request.h" | 10 #include "ios/chrome/browser/payments/payment_request.h" |
9 #import "ios/chrome/browser/payments/payment_request_util.h" | 11 #import "ios/chrome/browser/payments/payment_request_util.h" |
12 #include "ios/chrome/browser/payments/shipping_option_selection_mediator.h" | |
10 #include "ios/web/public/payments/payment_request.h" | 13 #include "ios/web/public/payments/payment_request.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::GetShippingOptionSelectorErrorMessage; | 20 using ::payment_request_util::GetShippingOptionSelectorErrorMessage; |
21 using ::payments::GetShippingOptionSectionString; | |
18 } // namespace | 22 } // namespace |
19 | 23 |
20 @interface ShippingOptionSelectionCoordinator () | 24 @interface ShippingOptionSelectionCoordinator () |
21 | 25 |
22 @property(nonatomic, strong) | 26 @property(nonatomic, strong) |
23 ShippingOptionSelectionViewController* viewController; | 27 PaymentRequestSelectorViewController* viewController; |
28 | |
29 @property(nonatomic, strong) ShippingOptionSelectionMediator* mediator; | |
24 | 30 |
25 // Called when the user selects a shipping option. The cell is checked, the | 31 // Called when the user selects a shipping option. The cell is checked, the |
26 // UI is locked so that the user can't interact with it, then the delegate is | 32 // 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 | 33 // notified. The delay is here to let the user get a visual feedback of the |
28 // selection before this view disappears. | 34 // selection before this view disappears. |
29 - (void)delayedNotifyDelegateOfSelection: | 35 - (void)delayedNotifyDelegateOfSelection: |
30 (web::PaymentShippingOption*)shippingOption; | 36 (web::PaymentShippingOption*)shippingOption; |
31 | 37 |
32 @end | 38 @end |
33 | 39 |
34 @implementation ShippingOptionSelectionCoordinator | 40 @implementation ShippingOptionSelectionCoordinator |
35 | 41 |
36 @synthesize paymentRequest = _paymentRequest; | 42 @synthesize paymentRequest = _paymentRequest; |
37 @synthesize delegate = _delegate; | 43 @synthesize delegate = _delegate; |
38 @synthesize viewController = _viewController; | 44 @synthesize viewController = _viewController; |
45 @synthesize mediator = _mediator; | |
39 | 46 |
40 - (void)start { | 47 - (void)start { |
41 _viewController = [[ShippingOptionSelectionViewController alloc] | 48 self.mediator = [[ShippingOptionSelectionMediator alloc] |
42 initWithPaymentRequest:_paymentRequest]; | 49 initWithPaymentRequest:self.paymentRequest]; |
43 [_viewController setDelegate:self]; | 50 |
44 [_viewController loadModel]; | 51 self.viewController = [[PaymentRequestSelectorViewController alloc] init]; |
52 self.viewController.title = base::SysUTF16ToNSString( | |
53 GetShippingOptionSectionString(self.paymentRequest->shipping_type())); | |
54 self.viewController.delegate = self; | |
55 self.viewController.dataSource = self.mediator; | |
56 [self.viewController loadModel]; | |
45 | 57 |
46 DCHECK(self.baseViewController.navigationController); | 58 DCHECK(self.baseViewController.navigationController); |
47 [self.baseViewController.navigationController | 59 [self.baseViewController.navigationController |
48 pushViewController:_viewController | 60 pushViewController:self.viewController |
49 animated:YES]; | 61 animated:YES]; |
50 } | 62 } |
51 | 63 |
52 - (void)stop { | 64 - (void)stop { |
53 [self.baseViewController.navigationController popViewControllerAnimated:YES]; | 65 [self.baseViewController.navigationController popViewControllerAnimated:YES]; |
54 _viewController = nil; | 66 self.viewController = nil; |
67 self.mediator = nil; | |
55 } | 68 } |
56 | 69 |
57 - (void)stopSpinnerAndDisplayError { | 70 - (void)stopSpinnerAndDisplayError { |
58 // Re-enable user interactions that were disabled earlier in | 71 // Re-enable user interactions that were disabled earlier in |
59 // delayedNotifyDelegateOfSelection. | 72 // delayedNotifyDelegateOfSelection. |
60 _viewController.view.userInteractionEnabled = YES; | 73 self.viewController.view.userInteractionEnabled = YES; |
61 | 74 |
62 [_viewController setPending:NO]; | 75 DCHECK(self.paymentRequest); |
63 DCHECK(_paymentRequest); | 76 self.mediator.headerText = |
64 [_viewController | 77 GetShippingOptionSelectorErrorMessage(*self.paymentRequest); |
65 setErrorMessage:GetShippingOptionSelectorErrorMessage(*_paymentRequest)]; | 78 self.mediator.state = PaymentRequestSelectorStateError; |
66 [_viewController loadModel]; | 79 [self.viewController loadModel]; |
67 [[_viewController collectionView] reloadData]; | 80 [self.viewController.collectionView reloadData]; |
68 } | 81 } |
69 | 82 |
70 #pragma mark - ShippingOptionSelectionViewControllerDelegate | 83 #pragma mark - PaymentRequestSelectorViewControllerDelegate |
71 | 84 |
72 - (void)shippingOptionSelectionViewController: | 85 - (void)paymentRequestSelectorViewController: |
73 (ShippingOptionSelectionViewController*)controller | 86 (PaymentRequestSelectorViewController*)controller |
74 didSelectShippingOption: | 87 didSelectItemAtIndex:(NSUInteger)index { |
75 (web::PaymentShippingOption*)shippingOption { | 88 // Update the data source with the selection. |
76 [self delayedNotifyDelegateOfSelection:shippingOption]; | 89 self.mediator.selectedItemIndex = index; |
90 | |
91 DCHECK(index < self.paymentRequest->shipping_options().size()); | |
92 [self delayedNotifyDelegateOfSelection:self.paymentRequest | |
93 ->shipping_options()[index]]; | |
77 } | 94 } |
78 | 95 |
79 - (void)shippingOptionSelectionViewControllerDidReturn: | 96 - (void)paymentRequestSelectorViewControllerDidFinish: |
80 (ShippingOptionSelectionViewController*)controller { | 97 (PaymentRequestSelectorViewController*)controller { |
81 [_delegate shippingOptionSelectionCoordinatorDidReturn:self]; | 98 [self.delegate shippingOptionSelectionCoordinatorDidReturn:self]; |
82 } | 99 } |
83 | 100 |
101 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: | |
102 (PaymentRequestSelectorViewController*)controller { | |
103 NOTREACHED() << "This should not get called."; | |
macourteau
2017/04/19 18:03:13
What about moving this into the base class, and no
Moe
2017/04/19 20:12:44
There's no base class to move this to. Any coordin
macourteau
2017/04/20 14:42:59
TIL about protocols in Objective-C. :)
n00b quest
Moe
2017/04/20 15:23:30
Good idea! TIL too!
| |
104 } | |
105 | |
106 #pragma mark - Helper methods | |
107 | |
84 - (void)delayedNotifyDelegateOfSelection: | 108 - (void)delayedNotifyDelegateOfSelection: |
85 (web::PaymentShippingOption*)shippingOption { | 109 (web::PaymentShippingOption*)shippingOption { |
86 _viewController.view.userInteractionEnabled = NO; | 110 self.viewController.view.userInteractionEnabled = NO; |
87 __weak ShippingOptionSelectionCoordinator* weakSelf = self; | 111 __weak ShippingOptionSelectionCoordinator* weakSelf = self; |
88 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, | 112 dispatch_after( |
89 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), | 113 dispatch_time(DISPATCH_TIME_NOW, |
90 dispatch_get_main_queue(), ^{ | 114 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), |
91 ShippingOptionSelectionCoordinator* strongSelf = weakSelf; | 115 dispatch_get_main_queue(), ^{ |
92 // Early return if the coordinator has been deallocated. | 116 [weakSelf.mediator setState:PaymentRequestSelectorStatePending]; |
93 if (!strongSelf) | 117 [weakSelf.viewController loadModel]; |
94 return; | 118 [weakSelf.viewController.collectionView reloadData]; |
95 [strongSelf.viewController setPending:YES]; | |
96 [strongSelf.viewController loadModel]; | |
97 [[strongSelf.viewController collectionView] reloadData]; | |
98 | 119 |
99 [strongSelf.delegate | 120 [weakSelf.delegate shippingOptionSelectionCoordinator:weakSelf |
100 shippingOptionSelectionCoordinator:strongSelf | 121 didSelectShippingOption:shippingOption]; |
101 didSelectShippingOption:shippingOption]; | 122 }); |
102 }); | |
103 } | 123 } |
104 | 124 |
105 @end | 125 @end |
OLD | NEW |