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