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 #import "base/ios/weak_nsobject.h" | |
8 #include "base/mac/scoped_nsobject.h" | |
9 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
10 #import "ios/chrome/browser/payments/payment_request_util.h" | 8 #import "ios/chrome/browser/payments/payment_request_util.h" |
11 | 9 |
12 @interface ShippingOptionSelectionCoordinator () { | 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
13 base::WeakNSProtocol<id<ShippingOptionSelectionCoordinatorDelegate>> | 11 #error "This file requires ARC support." |
14 _delegate; | 12 #endif |
15 base::scoped_nsobject<ShippingOptionSelectionViewController> _viewController; | 13 |
16 } | 14 @interface ShippingOptionSelectionCoordinator () |
| 15 |
| 16 @property(nonatomic, strong) |
| 17 ShippingOptionSelectionViewController* viewController; |
17 | 18 |
18 // Called when the user selects a shipping option. The cell is checked, the | 19 // Called when the user selects a shipping option. The cell is checked, the |
19 // UI is locked so that the user can't interact with it, then the delegate is | 20 // UI is locked so that the user can't interact with it, then the delegate is |
20 // notified. The delay is here to let the user get a visual feedback of the | 21 // notified. The delay is here to let the user get a visual feedback of the |
21 // selection before this view disappears. | 22 // selection before this view disappears. |
22 - (void)delayedNotifyDelegateOfSelection: | 23 - (void)delayedNotifyDelegateOfSelection: |
23 (web::PaymentShippingOption*)shippingOption; | 24 (web::PaymentShippingOption*)shippingOption; |
24 | 25 |
25 @end | 26 @end |
26 | 27 |
27 @implementation ShippingOptionSelectionCoordinator | 28 @implementation ShippingOptionSelectionCoordinator |
28 | 29 |
29 @synthesize paymentRequest = _paymentRequest; | 30 @synthesize paymentRequest = _paymentRequest; |
30 | 31 @synthesize delegate = _delegate; |
31 - (id<ShippingOptionSelectionCoordinatorDelegate>)delegate { | 32 @synthesize viewController = _viewController; |
32 return _delegate.get(); | |
33 } | |
34 | |
35 - (void)setDelegate:(id<ShippingOptionSelectionCoordinatorDelegate>)delegate { | |
36 _delegate.reset(delegate); | |
37 } | |
38 | 33 |
39 - (void)start { | 34 - (void)start { |
40 _viewController.reset([[ShippingOptionSelectionViewController alloc] | 35 _viewController = [[ShippingOptionSelectionViewController alloc] |
41 initWithPaymentRequest:_paymentRequest]); | 36 initWithPaymentRequest:_paymentRequest]; |
42 [_viewController setDelegate:self]; | 37 [_viewController setDelegate:self]; |
43 [_viewController loadModel]; | 38 [_viewController loadModel]; |
44 | 39 |
45 DCHECK(self.baseViewController.navigationController); | 40 DCHECK(self.baseViewController.navigationController); |
46 [self.baseViewController.navigationController | 41 [self.baseViewController.navigationController |
47 pushViewController:_viewController | 42 pushViewController:_viewController |
48 animated:YES]; | 43 animated:YES]; |
49 } | 44 } |
50 | 45 |
51 - (void)stop { | 46 - (void)stop { |
52 [self.baseViewController.navigationController popViewControllerAnimated:YES]; | 47 [self.baseViewController.navigationController popViewControllerAnimated:YES]; |
53 _viewController.reset(); | 48 _viewController = nil; |
54 } | 49 } |
55 | 50 |
56 - (void)stopSpinnerAndDisplayError { | 51 - (void)stopSpinnerAndDisplayError { |
57 // Re-enable user interactions that were disabled earlier in | 52 // Re-enable user interactions that were disabled earlier in |
58 // delayedNotifyDelegateOfSelection. | 53 // delayedNotifyDelegateOfSelection. |
59 _viewController.get().view.userInteractionEnabled = YES; | 54 _viewController.view.userInteractionEnabled = YES; |
60 | 55 |
61 [_viewController setIsLoading:NO]; | 56 [_viewController setIsLoading:NO]; |
62 NSString* errorMessage = | 57 NSString* errorMessage = |
63 payment_request_util::GetShippingOptionSelectorErrorMessage( | 58 payment_request_util::GetShippingOptionSelectorErrorMessage( |
64 _paymentRequest); | 59 _paymentRequest); |
65 [_viewController setErrorMessage:errorMessage]; | 60 [_viewController setErrorMessage:errorMessage]; |
66 [_viewController loadModel]; | 61 [_viewController loadModel]; |
67 [[_viewController collectionView] reloadData]; | 62 [[_viewController collectionView] reloadData]; |
68 } | 63 } |
69 | 64 |
70 #pragma mark - ShippingOptionSelectionViewControllerDelegate | 65 #pragma mark - ShippingOptionSelectionViewControllerDelegate |
71 | 66 |
72 - (void)shippingOptionSelectionViewController: | 67 - (void)shippingOptionSelectionViewController: |
73 (ShippingOptionSelectionViewController*)controller | 68 (ShippingOptionSelectionViewController*)controller |
74 didSelectShippingOption: | 69 didSelectShippingOption: |
75 (web::PaymentShippingOption*)shippingOption { | 70 (web::PaymentShippingOption*)shippingOption { |
76 [self delayedNotifyDelegateOfSelection:shippingOption]; | 71 [self delayedNotifyDelegateOfSelection:shippingOption]; |
77 } | 72 } |
78 | 73 |
79 - (void)shippingOptionSelectionViewControllerDidReturn: | 74 - (void)shippingOptionSelectionViewControllerDidReturn: |
80 (ShippingOptionSelectionViewController*)controller { | 75 (ShippingOptionSelectionViewController*)controller { |
81 [_delegate shippingOptionSelectionCoordinatorDidReturn:self]; | 76 [_delegate shippingOptionSelectionCoordinatorDidReturn:self]; |
82 } | 77 } |
83 | 78 |
84 - (void)delayedNotifyDelegateOfSelection: | 79 - (void)delayedNotifyDelegateOfSelection: |
85 (web::PaymentShippingOption*)shippingOption { | 80 (web::PaymentShippingOption*)shippingOption { |
86 _viewController.get().view.userInteractionEnabled = NO; | 81 _viewController.view.userInteractionEnabled = NO; |
87 base::WeakNSObject<ShippingOptionSelectionCoordinator> weakSelf(self); | 82 __weak ShippingOptionSelectionCoordinator* weakSelf = self; |
88 dispatch_after( | 83 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, |
89 dispatch_time(DISPATCH_TIME_NOW, | 84 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), |
90 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), | 85 dispatch_get_main_queue(), ^{ |
91 dispatch_get_main_queue(), ^{ | 86 ShippingOptionSelectionCoordinator* strongSelf = weakSelf; |
92 base::scoped_nsobject<ShippingOptionSelectionCoordinator> strongSelf( | 87 [strongSelf.viewController setIsLoading:YES]; |
93 [weakSelf retain]); | 88 [strongSelf.viewController loadModel]; |
94 // Early return if the coordinator has been deallocated. | 89 [[strongSelf.viewController collectionView] reloadData]; |
95 if (!strongSelf) | |
96 return; | |
97 [_viewController setIsLoading:YES]; | |
98 [_viewController loadModel]; | |
99 [[_viewController collectionView] reloadData]; | |
100 | 90 |
101 [_delegate shippingOptionSelectionCoordinator:self | 91 [strongSelf.delegate |
102 didSelectShippingOption:shippingOption]; | 92 shippingOptionSelectionCoordinator:strongSelf |
103 }); | 93 didSelectShippingOption:shippingOption]; |
| 94 }); |
104 } | 95 } |
105 | 96 |
106 @end | 97 @end |
OLD | NEW |