Commit 33e4db7a authored by mahmadi's avatar mahmadi Committed by Commit bot

[Payment Request] Country selection coordinator

BUG=602666

Review-Url: https://codereview.chromium.org/2896623002
Cr-Commit-Position: refs/heads/master@{#473442}
parent 9afb0f4c
......@@ -13,6 +13,8 @@ source_set("payments") {
"address_edit_mediator.mm",
"address_edit_view_controller.h",
"address_edit_view_controller.mm",
"country_selection_coordinator.h",
"country_selection_coordinator.mm",
"credit_card_edit_coordinator.h",
"credit_card_edit_coordinator.mm",
"credit_card_edit_mediator.h",
......@@ -136,6 +138,7 @@ source_set("unit_tests") {
testonly = true
sources = [
"address_edit_coordinator_unittest.mm",
"country_selection_coordinator_unittest.mm",
"credit_card_edit_coordinator_unittest.mm",
"credit_card_edit_view_controller_unittest.mm",
"payment_items_display_coordinator_unittest.mm",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_PAYMENTS_COUNTRY_SELECTION_COORDINATOR_H_
#define IOS_CHROME_BROWSER_UI_PAYMENTS_COUNTRY_SELECTION_COORDINATOR_H_
#import <UIKit/UIKit.h>
#include <vector>
#import "ios/chrome/browser/chrome_coordinator.h"
#import "ios/chrome/browser/ui/payments/payment_request_picker_view_controller.h"
@class CountrySelectionCoordinator;
// Delegate protocol for CountrySelectionCoordinator.
@protocol CountrySelectionCoordinatorDelegate<NSObject>
// Notifies the delegate that the user has selected a country.
- (void)countrySelectionCoordinator:(CountrySelectionCoordinator*)coordinator
didSelectCountryWithCode:(NSString*)countryCode;
@end
// Coordinator responsible for creating and presenting the country selection
// view controller. This view controller will be presented by the view
// controller provided in the initializer.
@interface CountrySelectionCoordinator
: ChromeCoordinator<PaymentRequestPickerViewControllerDelegate>
// A map of country codes to country names.
@property(nonatomic, strong) NSDictionary<NSString*, NSString*>* countries;
// The country code for the currently selected country, if any.
@property(nonatomic, strong) NSString* selectedCountryCode;
// The delegate to be notified when the user selects a country or returns
// without selecting one.
@property(nonatomic, weak) id<CountrySelectionCoordinatorDelegate> delegate;
@end
#endif // IOS_CHROME_BROWSER_UI_PAYMENTS_COUNTRY_SELECTION_COORDINATOR_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/payments/country_selection_coordinator.h"
#include "base/strings/sys_string_conversions.h"
#import "ios/chrome/browser/ui/payments/payment_request_picker_row.h"
#include "ios/chrome/browser/ui/payments/payment_request_picker_view_controller.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// The delay in nano seconds before notifying the delegate of the selection.
// This is to let the user get a visual feedback of the selection before this
// view disappears.
const int64_t kDelegateNotificationDelayInNanoSeconds = 0.2 * NSEC_PER_SEC;
} // namespace
@interface CountrySelectionCoordinator ()
@property(nonatomic, strong) PaymentRequestPickerViewController* viewController;
// Called when the user selects a country. The cell is checked, the UI is locked
// so that the user can't interact with it, then the delegate is notified after
// kDelegateNotificationDelayInNanoSeconds.
- (void)delayedNotifyDelegateOfSelection:(NSString*)countryCode;
@end
@implementation CountrySelectionCoordinator
@synthesize delegate = _delegate;
@synthesize countries = _countries;
@synthesize selectedCountryCode = _selectedCountryCode;
@synthesize viewController = _viewController;
- (void)start {
// Create the rows to display in the view controller.
NSMutableArray<PickerRow*>* rows = [[NSMutableArray alloc] init];
__block PickerRow* selectedRow = nil;
[self.countries
enumerateKeysAndObjectsUsingBlock:^(NSString* countryCode,
NSString* countryName, BOOL* stop) {
PickerRow* row =
[[PickerRow alloc] initWithLabel:countryName value:countryCode];
[rows addObject:row];
if (self.selectedCountryCode == countryCode)
selectedRow = row;
}];
self.viewController =
[[PaymentRequestPickerViewController alloc] initWithRows:rows
selected:selectedRow];
self.viewController.title = l10n_util::GetNSString(IDS_IOS_AUTOFILL_COUNTRY);
self.viewController.delegate = self;
DCHECK(self.baseViewController.navigationController);
[self.baseViewController.navigationController
pushViewController:self.viewController
animated:YES];
}
- (void)stop {
[self.viewController.navigationController popViewControllerAnimated:YES];
self.viewController = nil;
}
#pragma mark - PaymentRequestPickerViewControllerDelegate
- (void)paymentRequestPickerViewController:
(PaymentRequestPickerViewController*)controller
didSelectRow:(PickerRow*)row {
[self delayedNotifyDelegateOfSelection:row.value];
}
#pragma mark - Helper methods
- (void)delayedNotifyDelegateOfSelection:(NSString*)countryCode {
self.viewController.view.userInteractionEnabled = NO;
__weak CountrySelectionCoordinator* weakSelf = self;
dispatch_after(
dispatch_time(DISPATCH_TIME_NOW, kDelegateNotificationDelayInNanoSeconds),
dispatch_get_main_queue(), ^{
weakSelf.viewController.view.userInteractionEnabled = YES;
[weakSelf.delegate countrySelectionCoordinator:weakSelf
didSelectCountryWithCode:countryCode];
});
}
@end
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/payments/country_selection_coordinator.h"
#include "base/mac/foundation_util.h"
#include "base/memory/ptr_util.h"
#include "base/test/ios/wait_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/platform_test.h"
class PaymentRequestCountrySelectionCoordinatorTest : public PlatformTest {};
// Tests that invoking start and stop on the coordinator presents and dismisses
// the payment request picker view controller, respectively.
TEST_F(PaymentRequestCountrySelectionCoordinatorTest, StartAndStop) {
UIViewController* base_view_controller = [[UIViewController alloc] init];
UINavigationController* navigation_controller =
[[UINavigationController alloc]
initWithRootViewController:base_view_controller];
CountrySelectionCoordinator* coordinator =
[[CountrySelectionCoordinator alloc]
initWithBaseViewController:base_view_controller];
EXPECT_EQ(1u, navigation_controller.viewControllers.count);
[coordinator start];
// Short delay to allow animation to complete.
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_EQ(2u, navigation_controller.viewControllers.count);
[coordinator stop];
// Short delay to allow animation to complete.
base::test::ios::SpinRunLoopWithMaxDelay(base::TimeDelta::FromSecondsD(1.0));
EXPECT_EQ(1u, navigation_controller.viewControllers.count);
}
......@@ -85,6 +85,7 @@ NSString* const kPaymentRequestPickerViewControllerAccessibilityID =
[[UISearchController alloc] initWithSearchResultsController:nil];
self.searchController.searchResultsUpdater = self;
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.hidesNavigationBarDuringPresentation = NO;
self.searchController.searchBar.accessibilityIdentifier =
kPaymentRequestPickerSearchBarAccessibilityID;
self.tableView.tableHeaderView = self.searchController.searchBar;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment