| 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/ui/payments/contact_info_selection_coordinator.h" | 5 #import "ios/chrome/browser/ui/payments/contact_info_selection_coordinator.h" |
| 6 | 6 |
| 7 #include "base/strings/sys_string_conversions.h" | 7 #include "base/strings/sys_string_conversions.h" |
| 8 #include "components/autofill/core/browser/autofill_profile.h" | 8 #include "components/autofill/core/browser/autofill_profile.h" |
| 9 #include "components/payments/core/strings_util.h" | 9 #include "components/payments/core/strings_util.h" |
| 10 #include "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 namespace { | 21 namespace { |
| 22 // The delay in nano seconds before notifying the delegate of the selection. | 22 // The delay in nano seconds before notifying the delegate of the selection. |
| 23 // This is here to let the user get a visual feedback of the selection before | 23 // This is here to let the user get a visual feedback of the selection before |
| 24 // this view disappears. | 24 // this view disappears. |
| 25 const int64_t kDelegateNotificationDelayInNanoSeconds = 0.2 * NSEC_PER_SEC; | 25 const int64_t kDelegateNotificationDelayInNanoSeconds = 0.2 * NSEC_PER_SEC; |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 @interface ContactInfoSelectionCoordinator () | 28 @interface ContactInfoSelectionCoordinator () |
| 29 | 29 |
| 30 @property(nonatomic, strong) | 30 @property(nonatomic, strong) |
| 31 ContactInfoEditCoordinator* contactInfoEditCoordinator; |
| 32 |
| 33 @property(nonatomic, strong) |
| 31 PaymentRequestSelectorViewController* viewController; | 34 PaymentRequestSelectorViewController* viewController; |
| 32 | 35 |
| 33 @property(nonatomic, strong) ContactInfoSelectionMediator* mediator; | 36 @property(nonatomic, strong) ContactInfoSelectionMediator* mediator; |
| 34 | 37 |
| 35 // Called when the user selects a contact profile. The cell is checked, the | 38 // Called when the user selects a contact profile. The cell is checked, the |
| 36 // UI is locked so that the user can't interact with it, then the delegate is | 39 // UI is locked so that the user can't interact with it, then the delegate is |
| 37 // notified. | 40 // notified. |
| 38 - (void)delayedNotifyDelegateOfSelection: | 41 - (void)delayedNotifyDelegateOfSelection: |
| 39 (autofill::AutofillProfile*)contactProfile; | 42 (autofill::AutofillProfile*)contactProfile; |
| 40 | 43 |
| 41 @end | 44 @end |
| 42 | 45 |
| 43 @implementation ContactInfoSelectionCoordinator | 46 @implementation ContactInfoSelectionCoordinator |
| 44 | 47 |
| 45 @synthesize paymentRequest = _paymentRequest; | 48 @synthesize paymentRequest = _paymentRequest; |
| 46 @synthesize delegate = _delegate; | 49 @synthesize delegate = _delegate; |
| 50 @synthesize contactInfoEditCoordinator = _contactInfoEditCoordinator; |
| 47 @synthesize viewController = _viewController; | 51 @synthesize viewController = _viewController; |
| 48 @synthesize mediator = _mediator; | 52 @synthesize mediator = _mediator; |
| 49 | 53 |
| 50 - (void)start { | 54 - (void)start { |
| 51 self.mediator = [[ContactInfoSelectionMediator alloc] | 55 self.mediator = [[ContactInfoSelectionMediator alloc] |
| 52 initWithPaymentRequest:self.paymentRequest]; | 56 initWithPaymentRequest:self.paymentRequest]; |
| 53 | 57 |
| 54 self.viewController = [[PaymentRequestSelectorViewController alloc] init]; | 58 self.viewController = [[PaymentRequestSelectorViewController alloc] init]; |
| 55 self.viewController.title = | 59 self.viewController.title = |
| 56 l10n_util::GetNSString(IDS_PAYMENT_REQUEST_CONTACT_INFO_SECTION_NAME); | 60 l10n_util::GetNSString(IDS_PAYMENT_REQUEST_CONTACT_INFO_SECTION_NAME); |
| 57 self.viewController.delegate = self; | 61 self.viewController.delegate = self; |
| 58 self.viewController.dataSource = self.mediator; | 62 self.viewController.dataSource = self.mediator; |
| 59 [self.viewController loadModel]; | 63 [self.viewController loadModel]; |
| 60 | 64 |
| 61 DCHECK(self.baseViewController.navigationController); | 65 DCHECK(self.baseViewController.navigationController); |
| 62 [self.baseViewController.navigationController | 66 [self.baseViewController.navigationController |
| 63 pushViewController:self.viewController | 67 pushViewController:self.viewController |
| 64 animated:YES]; | 68 animated:YES]; |
| 65 } | 69 } |
| 66 | 70 |
| 67 - (void)stop { | 71 - (void)stop { |
| 68 [self.baseViewController.navigationController popViewControllerAnimated:YES]; | 72 [self.baseViewController.navigationController popViewControllerAnimated:YES]; |
| 73 [self.contactInfoEditCoordinator stop]; |
| 74 self.contactInfoEditCoordinator = nil; |
| 69 self.viewController = nil; | 75 self.viewController = nil; |
| 70 self.mediator = nil; | 76 self.mediator = nil; |
| 71 } | 77 } |
| 72 | 78 |
| 73 #pragma mark - PaymentRequestSelectorViewControllerDelegate | 79 #pragma mark - PaymentRequestSelectorViewControllerDelegate |
| 74 | 80 |
| 75 - (void)paymentRequestSelectorViewController: | 81 - (void)paymentRequestSelectorViewController: |
| 76 (PaymentRequestSelectorViewController*)controller | 82 (PaymentRequestSelectorViewController*)controller |
| 77 didSelectItemAtIndex:(NSUInteger)index { | 83 didSelectItemAtIndex:(NSUInteger)index { |
| 78 // Update the data source with the selection. | 84 // Update the data source with the selection. |
| 79 self.mediator.selectedItemIndex = index; | 85 self.mediator.selectedItemIndex = index; |
| 80 | 86 |
| 81 DCHECK(index < self.paymentRequest->contact_profiles().size()); | 87 DCHECK(index < self.paymentRequest->contact_profiles().size()); |
| 82 [self delayedNotifyDelegateOfSelection:self.paymentRequest | 88 [self delayedNotifyDelegateOfSelection:self.paymentRequest |
| 83 ->contact_profiles()[index]]; | 89 ->contact_profiles()[index]]; |
| 84 } | 90 } |
| 85 | 91 |
| 86 - (void)paymentRequestSelectorViewControllerDidFinish: | 92 - (void)paymentRequestSelectorViewControllerDidFinish: |
| 87 (PaymentRequestSelectorViewController*)controller { | 93 (PaymentRequestSelectorViewController*)controller { |
| 88 [self.delegate contactInfoSelectionCoordinatorDidReturn:self]; | 94 [self.delegate contactInfoSelectionCoordinatorDidReturn:self]; |
| 89 } | 95 } |
| 90 | 96 |
| 91 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: | 97 - (void)paymentRequestSelectorViewControllerDidSelectAddItem: |
| 92 (PaymentRequestSelectorViewController*)controller { | 98 (PaymentRequestSelectorViewController*)controller { |
| 93 // TODO(crbug.com/602666): Display contact info editor. | 99 self.contactInfoEditCoordinator = [[ContactInfoEditCoordinator alloc] |
| 100 initWithBaseViewController:self.viewController]; |
| 101 self.contactInfoEditCoordinator.paymentRequest = self.paymentRequest; |
| 102 self.contactInfoEditCoordinator.delegate = self; |
| 103 [self.contactInfoEditCoordinator start]; |
| 104 } |
| 105 |
| 106 #pragma mark - ContactInfoEditCoordinatorDelegate |
| 107 |
| 108 - (void)contactInfoEditCoordinator:(ContactInfoEditCoordinator*)coordinator |
| 109 didFinishEditingProfile:(autofill::AutofillProfile*)profile { |
| 110 [self.contactInfoEditCoordinator stop]; |
| 111 self.contactInfoEditCoordinator = nil; |
| 112 |
| 113 // Inform |self.delegate| that |profile| has been selected. |
| 114 [self.delegate contactInfoSelectionCoordinator:self |
| 115 didSelectContactProfile:profile]; |
| 116 } |
| 117 |
| 118 - (void)contactInfoEditCoordinatorDidCancel: |
| 119 (ContactInfoEditCoordinator*)coordinator { |
| 120 [self.contactInfoEditCoordinator stop]; |
| 121 self.contactInfoEditCoordinator = nil; |
| 94 } | 122 } |
| 95 | 123 |
| 96 #pragma mark - Helper methods | 124 #pragma mark - Helper methods |
| 97 | 125 |
| 98 - (void)delayedNotifyDelegateOfSelection: | 126 - (void)delayedNotifyDelegateOfSelection: |
| 99 (autofill::AutofillProfile*)contactProfile { | 127 (autofill::AutofillProfile*)contactProfile { |
| 100 self.viewController.view.userInteractionEnabled = NO; | 128 self.viewController.view.userInteractionEnabled = NO; |
| 101 __weak ContactInfoSelectionCoordinator* weakSelf = self; | 129 __weak ContactInfoSelectionCoordinator* weakSelf = self; |
| 102 dispatch_after( | 130 dispatch_after( |
| 103 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), | 131 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), |
| 104 dispatch_get_main_queue(), ^{ | 132 dispatch_get_main_queue(), ^{ |
| 105 [weakSelf.delegate contactInfoSelectionCoordinator:weakSelf | 133 [weakSelf.delegate contactInfoSelectionCoordinator:weakSelf |
| 106 didSelectContactProfile:contactProfile]; | 134 didSelectContactProfile:contactProfile]; |
| 107 }); | 135 }); |
| 108 } | 136 } |
| 109 | 137 |
| 110 @end | 138 @end |
| OLD | NEW |