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/payment_method_selection_coordinator.h" | 5 #import "ios/chrome/browser/payments/payment_method_selection_coordinator.h" |
6 | 6 |
7 #include "components/autofill/core/browser/credit_card.h" | 7 #include "components/autofill/core/browser/credit_card.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #include "ios/chrome/browser/payments/payment_method_selection_mediator.h" |
8 #include "ios/chrome/browser/payments/payment_request.h" | 10 #include "ios/chrome/browser/payments/payment_request.h" |
| 11 #include "ui/base/l10n/l10n_util.h" |
9 | 12 |
10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
11 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
12 #endif | 15 #endif |
13 | 16 |
14 @interface PaymentMethodSelectionCoordinator () { | 17 namespace { |
15 CreditCardEditCoordinator* _creditCardEditCoordinator; | 18 // The delay in nano seconds before notifying the delegate of the selection. |
16 } | 19 const int64_t kDelegateNotificationDelayInNanoSeconds = 0.2 * NSEC_PER_SEC; |
| 20 } // namespace |
| 21 |
| 22 @interface PaymentMethodSelectionCoordinator () |
17 | 23 |
18 @property(nonatomic, strong) | 24 @property(nonatomic, strong) |
19 PaymentMethodSelectionViewController* viewController; | 25 CreditCardEditCoordinator* creditCardEditCoordinator; |
| 26 |
| 27 @property(nonatomic, strong) |
| 28 PaymentRequestSelectorViewController* viewController; |
| 29 |
| 30 @property(nonatomic, strong) PaymentMethodSelectionMediator* mediator; |
20 | 31 |
21 // Called when the user selects a payment method. The cell is checked, the | 32 // Called when the user selects a payment method. The cell is checked, the |
22 // UI is locked so that the user can't interact with it, then the delegate is | 33 // UI is locked so that the user can't interact with it, then the delegate is |
23 // notified. The delay is here to let the user get a visual feedback of the | 34 // notified. The delay is here to let the user get a visual feedback of the |
24 // selection before this view disappears. | 35 // selection before this view disappears. |
25 - (void)delayedNotifyDelegateOfSelection:(autofill::CreditCard*)paymentMethod; | 36 - (void)delayedNotifyDelegateOfSelection:(autofill::CreditCard*)paymentMethod; |
26 | 37 |
27 @end | 38 @end |
28 | 39 |
29 @implementation PaymentMethodSelectionCoordinator | 40 @implementation PaymentMethodSelectionCoordinator |
30 @synthesize paymentRequest = _paymentRequest; | 41 @synthesize paymentRequest = _paymentRequest; |
31 @synthesize delegate = _delegate; | 42 @synthesize delegate = _delegate; |
| 43 @synthesize creditCardEditCoordinator = _creditCardEditCoordinator; |
32 @synthesize viewController = _viewController; | 44 @synthesize viewController = _viewController; |
| 45 @synthesize mediator = _mediator; |
33 | 46 |
34 - (void)start { | 47 - (void)start { |
35 _viewController = [[PaymentMethodSelectionViewController alloc] | 48 self.mediator = [[PaymentMethodSelectionMediator alloc] |
36 initWithPaymentRequest:_paymentRequest]; | 49 initWithPaymentRequest:self.paymentRequest]; |
37 [_viewController setDelegate:self]; | |
38 [_viewController loadModel]; | |
39 | 50 |
40 DCHECK([self baseViewController].navigationController); | 51 self.viewController = [[PaymentRequestSelectorViewController alloc] init]; |
41 [[self baseViewController].navigationController | 52 self.viewController.title = |
42 pushViewController:_viewController | 53 l10n_util::GetNSString(IDS_PAYMENTS_METHOD_OF_PAYMENT_LABEL); |
| 54 self.viewController.delegate = self; |
| 55 self.viewController.dataSource = self.mediator; |
| 56 [self.viewController loadModel]; |
| 57 |
| 58 DCHECK(self.baseViewController.navigationController); |
| 59 [self.baseViewController.navigationController |
| 60 pushViewController:self.viewController |
43 animated:YES]; | 61 animated:YES]; |
44 } | 62 } |
45 | 63 |
46 - (void)stop { | 64 - (void)stop { |
47 [[self baseViewController].navigationController | 65 [self.baseViewController.navigationController popViewControllerAnimated:YES]; |
48 popViewControllerAnimated:YES]; | 66 [self.creditCardEditCoordinator stop]; |
49 [_creditCardEditCoordinator stop]; | 67 self.creditCardEditCoordinator = nil; |
50 _creditCardEditCoordinator = nil; | 68 self.viewController = nil; |
51 _viewController = nil; | 69 self.mediator = nil; |
52 } | 70 } |
53 | 71 |
54 #pragma mark - PaymentMethodSelectionViewControllerDelegate | 72 #pragma mark - PaymentRequestSelectorViewControllerDelegate |
55 | 73 |
56 - (void)paymentMethodSelectionViewController: | 74 - (void)paymentRequestSelectorViewController: |
57 (PaymentMethodSelectionViewController*)controller | 75 (PaymentRequestSelectorViewController*)controller |
58 didSelectPaymentMethod: | 76 didSelectItemAtIndex:(NSUInteger)index { |
59 (autofill::CreditCard*)paymentMethod { | 77 // Update the data source with the selection. |
60 [self delayedNotifyDelegateOfSelection:paymentMethod]; | 78 self.mediator.selectedItemIndex = index; |
| 79 |
| 80 DCHECK(index < self.paymentRequest->credit_cards().size()); |
| 81 [self delayedNotifyDelegateOfSelection:self.paymentRequest |
| 82 ->credit_cards()[index]]; |
61 } | 83 } |
62 | 84 |
63 - (void)paymentMethodSelectionViewControllerDidReturn: | 85 - (void)paymentRequestSelectorViewControllerDidFinish: |
64 (PaymentMethodSelectionViewController*)controller { | 86 (PaymentRequestSelectorViewController*)controller { |
65 [_delegate paymentMethodSelectionCoordinatorDidReturn:self]; | 87 [self.delegate paymentMethodSelectionCoordinatorDidReturn:self]; |
66 } | 88 } |
67 | 89 |
68 - (void)delayedNotifyDelegateOfSelection:(autofill::CreditCard*)paymentMethod { | 90 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: |
69 _viewController.view.userInteractionEnabled = NO; | 91 (PaymentRequestSelectorViewController*)controller { |
70 __weak PaymentMethodSelectionCoordinator* weakSelf = self; | 92 self.creditCardEditCoordinator = [[CreditCardEditCoordinator alloc] |
71 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, | 93 initWithBaseViewController:self.viewController]; |
72 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), | 94 self.creditCardEditCoordinator.paymentRequest = self.paymentRequest; |
73 dispatch_get_main_queue(), ^{ | 95 self.creditCardEditCoordinator.delegate = self; |
74 PaymentMethodSelectionCoordinator* strongSelf = weakSelf; | 96 [self.creditCardEditCoordinator start]; |
75 // Early return if the coordinator has been deallocated. | |
76 if (!strongSelf) | |
77 return; | |
78 | |
79 strongSelf.viewController.view.userInteractionEnabled = YES; | |
80 [strongSelf.delegate | |
81 paymentMethodSelectionCoordinator:strongSelf | |
82 didSelectPaymentMethod:paymentMethod]; | |
83 }); | |
84 } | |
85 | |
86 - (void)paymentMethodSelectionViewControllerDidSelectAddCard: | |
87 (PaymentMethodSelectionViewController*)controller { | |
88 _creditCardEditCoordinator = [[CreditCardEditCoordinator alloc] | |
89 initWithBaseViewController:_viewController]; | |
90 [_creditCardEditCoordinator setPaymentRequest:_paymentRequest]; | |
91 [_creditCardEditCoordinator setDelegate:self]; | |
92 [_creditCardEditCoordinator start]; | |
93 } | 97 } |
94 | 98 |
95 #pragma mark - CreditCardEditCoordinatorDelegate | 99 #pragma mark - CreditCardEditCoordinatorDelegate |
96 | 100 |
97 - (void)creditCardEditCoordinator:(CreditCardEditCoordinator*)coordinator | 101 - (void)creditCardEditCoordinator:(CreditCardEditCoordinator*)coordinator |
98 didFinishEditingCreditCard:(autofill::CreditCard*)creditCard { | 102 didFinishEditingCreditCard:(autofill::CreditCard*)creditCard { |
99 [_creditCardEditCoordinator stop]; | 103 [self.creditCardEditCoordinator stop]; |
100 _creditCardEditCoordinator = nil; | 104 self.creditCardEditCoordinator = nil; |
101 | 105 |
102 // Inform |_delegate| that this card has been selected. | 106 // Inform |self.delegate| that this card has been selected. |
103 [_delegate paymentMethodSelectionCoordinator:self | 107 [self.delegate paymentMethodSelectionCoordinator:self |
104 didSelectPaymentMethod:creditCard]; | 108 didSelectPaymentMethod:creditCard]; |
105 } | 109 } |
106 | 110 |
107 - (void)creditCardEditCoordinatorDidCancel: | 111 - (void)creditCardEditCoordinatorDidCancel: |
108 (CreditCardEditCoordinator*)coordinator { | 112 (CreditCardEditCoordinator*)coordinator { |
109 [_creditCardEditCoordinator stop]; | 113 [self.creditCardEditCoordinator stop]; |
110 _creditCardEditCoordinator = nil; | 114 self.creditCardEditCoordinator = nil; |
| 115 } |
| 116 |
| 117 #pragma mark - Helper methods |
| 118 |
| 119 - (void)delayedNotifyDelegateOfSelection:(autofill::CreditCard*)paymentMethod { |
| 120 self.viewController.view.userInteractionEnabled = NO; |
| 121 __weak PaymentMethodSelectionCoordinator* weakSelf = self; |
| 122 dispatch_after( |
| 123 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), |
| 124 dispatch_get_main_queue(), ^{ |
| 125 weakSelf.viewController.view.userInteractionEnabled = YES; |
| 126 [weakSelf.delegate paymentMethodSelectionCoordinator:weakSelf |
| 127 didSelectPaymentMethod:paymentMethod]; |
| 128 }); |
111 } | 129 } |
112 | 130 |
113 @end | 131 @end |
OLD | NEW |