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