Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(42)

Side by Side Diff: ios/chrome/browser/payments/shipping_address_selection_coordinator.mm

Issue 2827453004: [Payment Request] Refactors selector view controllers (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #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 "ios/chrome/browser/payments/payment_request.h" 10 #include "ios/chrome/browser/payments/payment_request.h"
11 #include "ios/chrome/browser/payments/payment_request_selector_view_controller.h "
10 #import "ios/chrome/browser/payments/payment_request_util.h" 12 #import "ios/chrome/browser/payments/payment_request_util.h"
13 #include "ios/chrome/browser/payments/shipping_address_selection_mediator.h"
11 14
12 #if !defined(__has_feature) || !__has_feature(objc_arc) 15 #if !defined(__has_feature) || !__has_feature(objc_arc)
13 #error "This file requires ARC support." 16 #error "This file requires ARC support."
14 #endif 17 #endif
15 18
16 namespace { 19 namespace {
17 using ::payment_request_util::GetShippingAddressSelectorErrorMessage; 20 using ::payment_request_util::GetShippingAddressSelectorErrorMessage;
21 using ::payments::GetShippingAddressSectionString;
22 using ::payments::GetShippingAddressSelectorInfoMessage;
18 } // namespace 23 } // namespace
19 24
20 @interface ShippingAddressSelectionCoordinator () 25 @interface ShippingAddressSelectionCoordinator ()
21 26
22 @property(nonatomic, strong) 27 @property(nonatomic, strong)
23 ShippingAddressSelectionViewController* viewController; 28 PaymentRequestSelectorViewController* viewController;
29
30 @property(nonatomic, strong) ShippingAddressSelectionMediator* mediator;
24 31
25 // Called when the user selects a shipping address. The cell is checked, the 32 // Called when the user selects a shipping address. The cell is checked, the
26 // 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
27 // 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
28 // selection before this view disappears. 35 // selection before this view disappears.
29 - (void)delayedNotifyDelegateOfSelection: 36 - (void)delayedNotifyDelegateOfSelection:
30 (autofill::AutofillProfile*)shippingAddress; 37 (autofill::AutofillProfile*)shippingAddress;
31 38
32 @end 39 @end
33 40
34 @implementation ShippingAddressSelectionCoordinator 41 @implementation ShippingAddressSelectionCoordinator
35 42
36 @synthesize paymentRequest = _paymentRequest; 43 @synthesize paymentRequest = _paymentRequest;
37 @synthesize delegate = _delegate; 44 @synthesize delegate = _delegate;
38 @synthesize viewController = _viewController; 45 @synthesize viewController = _viewController;
46 @synthesize mediator = _mediator;
39 47
40 - (void)start { 48 - (void)start {
41 _viewController = [[ShippingAddressSelectionViewController alloc] 49 self.mediator = [[ShippingAddressSelectionMediator alloc]
42 initWithPaymentRequest:_paymentRequest]; 50 initWithPaymentRequest:self.paymentRequest];
43 [_viewController setDelegate:self]; 51 [self.mediator setMessage:self.paymentRequest->shipping_options().empty()
gambard 2017/04/18 14:34:46 Are you sure you want to set message to something
gambard 2017/04/18 14:34:46 s/self.mediator setMessage:/self.mediator.message
Moe 2017/04/18 17:19:16 Done. Fixed similar cases as well.
Moe 2017/04/18 17:19:16 Yes. that's according to the specs and mocks. It's
44 [_viewController loadModel]; 52 ? base::SysUTF16ToNSString(
53 GetShippingAddressSelectorInfoMessage(
54 self.paymentRequest->shipping_type()))
55 : nil];
56
57 self.viewController = [[PaymentRequestSelectorViewController alloc] init];
58 [self.viewController
59 setTitle:base::SysUTF16ToNSString(GetShippingAddressSectionString(
60 self.paymentRequest->shipping_type()))];
61 [self.viewController setDelegate:self];
62 [self.viewController setDataSource:self.mediator];
63 [self.viewController loadModel];
45 64
46 DCHECK(self.baseViewController.navigationController); 65 DCHECK(self.baseViewController.navigationController);
47 [self.baseViewController.navigationController 66 [self.baseViewController.navigationController
48 pushViewController:_viewController 67 pushViewController:self.viewController
49 animated:YES]; 68 animated:YES];
50 } 69 }
51 70
52 - (void)stop { 71 - (void)stop {
53 [self.baseViewController.navigationController popViewControllerAnimated:YES]; 72 [self.baseViewController.navigationController popViewControllerAnimated:YES];
54 _viewController = nil; 73 self.viewController = nil;
74 self.mediator = nil;
55 } 75 }
56 76
57 - (void)stopSpinnerAndDisplayError { 77 - (void)stopSpinnerAndDisplayError {
58 // Re-enable user interactions that were disabled earlier in 78 // Re-enable user interactions that were disabled earlier in
59 // delayedNotifyDelegateOfSelection. 79 // delayedNotifyDelegateOfSelection.
60 _viewController.view.userInteractionEnabled = YES; 80 self.viewController.view.userInteractionEnabled = YES;
61 81
62 [_viewController setPending:NO]; 82 DCHECK(self.paymentRequest);
63 DCHECK(_paymentRequest); 83 [self.mediator
64 [_viewController 84 setMessage:GetShippingAddressSelectorErrorMessage(*self.paymentRequest)];
65 setErrorMessage:GetShippingAddressSelectorErrorMessage(*_paymentRequest)]; 85 [self.mediator setState:PaymentRequestSelectorStateError];
66 [_viewController loadModel]; 86 [self.viewController loadModel];
67 [[_viewController collectionView] reloadData]; 87 [[self.viewController collectionView] reloadData];
68 } 88 }
69 89
70 #pragma mark - ShippingAddressSelectionViewControllerDelegate 90 #pragma mark - PaymentRequestSelectorViewControllerDelegate
71 91
72 - (void)shippingAddressSelectionViewController: 92 - (void)paymentRequestSelectorViewController:
73 (ShippingAddressSelectionViewController*)controller 93 (PaymentRequestSelectorViewController*)controller
74 didSelectShippingAddress: 94 didSelectItemAtIndex:(NSUInteger)index {
75 (autofill::AutofillProfile*)shippingAddress { 95 // Update the data source with the selection.
76 [self delayedNotifyDelegateOfSelection:shippingAddress]; 96 [self.mediator setSelectedItemIndex:index];
97
98 DCHECK(index < self.paymentRequest->shipping_profiles().size());
99 [self delayedNotifyDelegateOfSelection:self.paymentRequest
100 ->shipping_profiles()[index]];
77 } 101 }
78 102
79 - (void)shippingAddressSelectionViewControllerDidReturn: 103 - (void)paymentRequestSelectorViewControllerDidFinish:
80 (ShippingAddressSelectionViewController*)controller { 104 (PaymentRequestSelectorViewController*)controller {
81 [_delegate shippingAddressSelectionCoordinatorDidReturn:self]; 105 [self.delegate shippingAddressSelectionCoordinatorDidReturn:self];
82 } 106 }
83 107
108 - (void)paymentRequestSelectorViewControllerDidSelectAddItem:
109 (PaymentRequestSelectorViewController*)controller {
110 // TODO(crbug.com/602666): Present a shipping address addition UI.
111 }
112
113 #pragma mark - Helper methods
114
84 - (void)delayedNotifyDelegateOfSelection: 115 - (void)delayedNotifyDelegateOfSelection:
85 (autofill::AutofillProfile*)shippingAddress { 116 (autofill::AutofillProfile*)shippingAddress {
86 _viewController.view.userInteractionEnabled = NO; 117 self.viewController.view.userInteractionEnabled = NO;
87 __weak ShippingAddressSelectionCoordinator* weakSelf = self; 118 __weak ShippingAddressSelectionCoordinator* weakSelf = self;
88 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 119 dispatch_after(
89 static_cast<int64_t>(0.2 * NSEC_PER_SEC)), 120 dispatch_time(DISPATCH_TIME_NOW,
90 dispatch_get_main_queue(), ^{ 121 static_cast<int64_t>(0.2 * NSEC_PER_SEC)),
91 ShippingAddressSelectionCoordinator* strongSelf = weakSelf; 122 dispatch_get_main_queue(), ^{
92 // Early return if the coordinator has been deallocated. 123 [weakSelf.mediator setState:PaymentRequestSelectorStatePending];
93 if (!strongSelf) 124 [weakSelf.viewController loadModel];
94 return; 125 [[weakSelf.viewController collectionView] reloadData];
95 126
96 [strongSelf.viewController setPending:YES]; 127 [weakSelf.delegate shippingAddressSelectionCoordinator:weakSelf
97 [strongSelf.viewController loadModel]; 128 didSelectShippingAddress:shippingAddress];
98 [[strongSelf.viewController collectionView] reloadData]; 129 });
99
100 [strongSelf.delegate
101 shippingAddressSelectionCoordinator:strongSelf
102 didSelectShippingAddress:shippingAddress];
103 });
104 } 130 }
105 131
106 @end 132 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698