OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ios/chrome/browser/ui/payments/country_selection_coordinator.h" |
| 6 |
| 7 #include "base/strings/sys_string_conversions.h" |
| 8 #import "ios/chrome/browser/ui/payments/payment_request_picker_row.h" |
| 9 #include "ios/chrome/browser/ui/payments/payment_request_picker_view_controller.
h" |
| 10 #include "ios/chrome/grit/ios_strings.h" |
| 11 #include "ui/base/l10n/l10n_util.h" |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 namespace { |
| 18 // The delay in nano seconds before notifying the delegate of the selection. |
| 19 // This is to let the user get a visual feedback of the selection before this |
| 20 // view disappears. |
| 21 const int64_t kDelegateNotificationDelayInNanoSeconds = 0.2 * NSEC_PER_SEC; |
| 22 } // namespace |
| 23 |
| 24 @interface CountrySelectionCoordinator () |
| 25 |
| 26 @property(nonatomic, strong) PaymentRequestPickerViewController* viewController; |
| 27 |
| 28 // Called when the user selects a country. The cell is checked, the UI is locked |
| 29 // so that the user can't interact with it, then the delegate is notified after |
| 30 // kDelegateNotificationDelayInNanoSeconds. |
| 31 - (void)delayedNotifyDelegateOfSelection:(NSString*)countryCode; |
| 32 |
| 33 @end |
| 34 |
| 35 @implementation CountrySelectionCoordinator |
| 36 |
| 37 @synthesize delegate = _delegate; |
| 38 @synthesize countries = _countries; |
| 39 @synthesize selectedCountryCode = _selectedCountryCode; |
| 40 @synthesize viewController = _viewController; |
| 41 |
| 42 - (void)start { |
| 43 // Create the rows to display in the view controller. |
| 44 NSMutableArray<PickerRow*>* rows = [[NSMutableArray alloc] init]; |
| 45 __block PickerRow* selectedRow = nil; |
| 46 [self.countries |
| 47 enumerateKeysAndObjectsUsingBlock:^(NSString* countryCode, |
| 48 NSString* countryName, BOOL* stop) { |
| 49 PickerRow* row = |
| 50 [[PickerRow alloc] initWithLabel:countryName value:countryCode]; |
| 51 [rows addObject:row]; |
| 52 if (self.selectedCountryCode == countryCode) |
| 53 selectedRow = row; |
| 54 }]; |
| 55 self.viewController = |
| 56 [[PaymentRequestPickerViewController alloc] initWithRows:rows |
| 57 selected:selectedRow]; |
| 58 self.viewController.title = l10n_util::GetNSString(IDS_IOS_AUTOFILL_COUNTRY); |
| 59 self.viewController.delegate = self; |
| 60 |
| 61 DCHECK(self.baseViewController.navigationController); |
| 62 [self.baseViewController.navigationController |
| 63 pushViewController:self.viewController |
| 64 animated:YES]; |
| 65 } |
| 66 |
| 67 - (void)stop { |
| 68 [self.viewController.navigationController popViewControllerAnimated:YES]; |
| 69 self.viewController = nil; |
| 70 } |
| 71 |
| 72 #pragma mark - PaymentRequestPickerViewControllerDelegate |
| 73 |
| 74 - (void)paymentRequestPickerViewController: |
| 75 (PaymentRequestPickerViewController*)controller |
| 76 didSelectRow:(PickerRow*)row { |
| 77 [self delayedNotifyDelegateOfSelection:row.value]; |
| 78 } |
| 79 |
| 80 #pragma mark - Helper methods |
| 81 |
| 82 - (void)delayedNotifyDelegateOfSelection:(NSString*)countryCode { |
| 83 self.viewController.view.userInteractionEnabled = NO; |
| 84 __weak CountrySelectionCoordinator* weakSelf = self; |
| 85 dispatch_after( |
| 86 dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds), |
| 87 dispatch_get_main_queue(), ^{ |
| 88 weakSelf.viewController.view.userInteractionEnabled = YES; |
| 89 [weakSelf.delegate countrySelectionCoordinator:weakSelf |
| 90 didSelectCountryWithCode:countryCode]; |
| 91 }); |
| 92 } |
| 93 |
| 94 @end |
OLD | NEW |