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