OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #import "ios/chrome/browser/payments/shipping_address_selection_view_controller.
h" | |
6 | |
7 #include "base/mac/foundation_util.h" | |
8 | |
9 #include "base/strings/sys_string_conversions.h" | |
10 #include "components/autofill/core/browser/autofill_profile.h" | |
11 #include "components/payments/core/strings_util.h" | |
12 #include "components/strings/grit/components_strings.h" | |
13 #include "ios/chrome/browser/application_context.h" | |
14 #import "ios/chrome/browser/payments/cells/autofill_profile_item.h" | |
15 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | |
16 #include "ios/chrome/browser/payments/payment_request.h" | |
17 #import "ios/chrome/browser/payments/payment_request_util.h" | |
18 #import "ios/chrome/browser/payments/shipping_address_selection_view_controller_
actions.h" | |
19 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" | |
20 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" | |
21 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | |
22 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | |
23 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | |
24 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | |
25 #include "ios/chrome/browser/ui/uikit_ui_util.h" | |
26 #include "ios/chrome/grit/ios_strings.h" | |
27 #include "ios/chrome/grit/ios_theme_resources.h" | |
28 #include "ui/base/l10n/l10n_util.h" | |
29 | |
30 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
31 #error "This file requires ARC support." | |
32 #endif | |
33 | |
34 namespace { | |
35 using ::payment_request_util::GetNameLabelFromAutofillProfile; | |
36 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile; | |
37 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile; | |
38 using ::payments::GetShippingAddressSelectorInfoMessage; | |
39 using ::payments::GetShippingAddressSectionString; | |
40 | |
41 NSString* const kShippingAddressSelectionCollectionViewID = | |
42 @"kShippingAddressSelectionCollectionViewID"; | |
43 | |
44 const CGFloat kSeparatorEdgeInset = 14; | |
45 | |
46 typedef NS_ENUM(NSInteger, SectionIdentifier) { | |
47 SectionIdentifierShippingAddress = kSectionIdentifierEnumZero, | |
48 }; | |
49 | |
50 typedef NS_ENUM(NSInteger, ItemType) { | |
51 ItemTypeSpinner = kItemTypeEnumZero, | |
52 ItemTypeMessage, | |
53 ItemTypeShippingAddress, // This is a repeated item type. | |
54 ItemTypeAddShippingAddress, | |
55 }; | |
56 | |
57 } // namespace | |
58 | |
59 @interface ShippingAddressSelectionViewController ()< | |
60 ShippingAddressSelectionViewControllerActions> { | |
61 // The PaymentRequest object having a copy of web::PaymentRequest as provided | |
62 // by the page invoking the Payment Request API. This is a weak pointer and | |
63 // should outlive this class. | |
64 PaymentRequest* _paymentRequest; | |
65 | |
66 // The currently selected item. May be nil. | |
67 __weak AutofillProfileItem* _selectedItem; | |
68 } | |
69 | |
70 @end | |
71 | |
72 @implementation ShippingAddressSelectionViewController | |
73 | |
74 @synthesize pending = _pending; | |
75 @synthesize errorMessage = _errorMessage; | |
76 @synthesize delegate = _delegate; | |
77 | |
78 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | |
79 DCHECK(paymentRequest); | |
80 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | |
81 self.title = base::SysUTF16ToNSString( | |
82 GetShippingAddressSectionString(paymentRequest->shipping_type())); | |
83 | |
84 // Set up leading (return) button. | |
85 UIBarButtonItem* returnButton = | |
86 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | |
87 target:nil | |
88 action:@selector(onReturn)]; | |
89 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | |
90 self.navigationItem.leftBarButtonItem = returnButton; | |
91 | |
92 _paymentRequest = paymentRequest; | |
93 } | |
94 return self; | |
95 } | |
96 | |
97 - (void)onReturn { | |
98 [_delegate shippingAddressSelectionViewControllerDidReturn:self]; | |
99 } | |
100 | |
101 #pragma mark - CollectionViewController methods | |
102 | |
103 - (void)loadModel { | |
104 [super loadModel]; | |
105 CollectionViewModel* model = self.collectionViewModel; | |
106 _selectedItem = nil; | |
107 | |
108 [model addSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
109 | |
110 if (_pending) { | |
111 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; | |
112 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); | |
113 [model addItem:statusItem | |
114 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
115 return; | |
116 } | |
117 | |
118 PaymentsTextItem* messageItem = | |
119 [[PaymentsTextItem alloc] initWithType:ItemTypeMessage]; | |
120 if (_errorMessage) { | |
121 messageItem.text = _errorMessage; | |
122 messageItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | |
123 } else { | |
124 messageItem.text = | |
125 base::SysUTF16ToNSString(GetShippingAddressSelectorInfoMessage( | |
126 _paymentRequest->shipping_type())); | |
127 } | |
128 [model addItem:messageItem | |
129 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
130 | |
131 for (auto* shippingAddress : _paymentRequest->shipping_profiles()) { | |
132 DCHECK(shippingAddress); | |
133 AutofillProfileItem* item = | |
134 [[AutofillProfileItem alloc] initWithType:ItemTypeShippingAddress]; | |
135 item.accessibilityTraits |= UIAccessibilityTraitButton; | |
136 item.name = GetNameLabelFromAutofillProfile(*shippingAddress); | |
137 item.address = GetShippingAddressLabelFromAutofillProfile(*shippingAddress); | |
138 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*shippingAddress); | |
139 if (_paymentRequest->selected_shipping_profile() == shippingAddress) { | |
140 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
141 _selectedItem = item; | |
142 } | |
143 [model addItem:item | |
144 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
145 } | |
146 | |
147 PaymentsTextItem* addShippingAddress = | |
148 [[PaymentsTextItem alloc] initWithType:ItemTypeAddShippingAddress]; | |
149 addShippingAddress.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_ADDRESS); | |
150 addShippingAddress.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | |
151 addShippingAddress.accessibilityTraits |= UIAccessibilityTraitButton; | |
152 [model addItem:addShippingAddress | |
153 toSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
154 } | |
155 | |
156 - (void)viewDidLoad { | |
157 [super viewDidLoad]; | |
158 self.collectionView.accessibilityIdentifier = | |
159 kShippingAddressSelectionCollectionViewID; | |
160 | |
161 // Customize collection view settings. | |
162 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | |
163 self.styler.separatorInset = | |
164 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); | |
165 } | |
166 | |
167 #pragma mark UICollectionViewDataSource | |
168 | |
169 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView | |
170 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
171 UICollectionViewCell* cell = | |
172 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; | |
173 | |
174 NSInteger itemType = | |
175 [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
176 switch (itemType) { | |
177 case ItemTypeMessage: { | |
178 PaymentsTextCell* messageCell = | |
179 base::mac::ObjCCastStrict<PaymentsTextCell>(cell); | |
180 messageCell.textLabel.textColor = | |
181 _errorMessage ? [[MDCPalette cr_redPalette] tint600] | |
182 : [[MDCPalette greyPalette] tint600]; | |
183 break; | |
184 } | |
185 default: | |
186 break; | |
187 } | |
188 return cell; | |
189 } | |
190 | |
191 #pragma mark UICollectionViewDelegate | |
192 | |
193 - (void)collectionView:(UICollectionView*)collectionView | |
194 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
195 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | |
196 | |
197 CollectionViewModel* model = self.collectionViewModel; | |
198 | |
199 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | |
200 if (item.type == ItemTypeShippingAddress) { | |
201 // Update the currently selected cell, if any. | |
202 if (_selectedItem) { | |
203 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | |
204 [self reconfigureCellsForItems:@[ _selectedItem ] | |
205 inSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
206 } | |
207 | |
208 // Update the newly selected cell. | |
209 AutofillProfileItem* newlySelectedItem = | |
210 base::mac::ObjCCastStrict<AutofillProfileItem>(item); | |
211 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
212 [self reconfigureCellsForItems:@[ newlySelectedItem ] | |
213 inSectionWithIdentifier:SectionIdentifierShippingAddress]; | |
214 | |
215 // Update the reference to the selected item. | |
216 _selectedItem = newlySelectedItem; | |
217 | |
218 // Notify the delegate of the selection. | |
219 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; | |
220 DCHECK(index < (NSInteger)_paymentRequest->shipping_profiles().size()); | |
221 [_delegate shippingAddressSelectionViewController:self | |
222 didSelectShippingAddress: | |
223 _paymentRequest->shipping_profiles()[index]]; | |
224 } | |
225 // TODO(crbug.com/602666): Present a shipping address addition UI when | |
226 // itemType == ItemTypeAddShippingAddress. | |
227 } | |
228 | |
229 #pragma mark MDCCollectionViewStylingDelegate | |
230 | |
231 - (CGFloat)collectionView:(UICollectionView*)collectionView | |
232 cellHeightAtIndexPath:(NSIndexPath*)indexPath { | |
233 CollectionViewItem* item = | |
234 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
235 switch (item.type) { | |
236 case ItemTypeSpinner: | |
237 case ItemTypeMessage: | |
238 case ItemTypeShippingAddress: | |
239 case ItemTypeAddShippingAddress: | |
240 return [MDCCollectionViewCell | |
241 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) | |
242 forItem:item]; | |
243 default: | |
244 NOTREACHED(); | |
245 return MDCCellDefaultOneLineHeight; | |
246 } | |
247 } | |
248 | |
249 - (BOOL)collectionView:(UICollectionView*)collectionView | |
250 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | |
251 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
252 if (type == ItemTypeMessage) { | |
253 return YES; | |
254 } else { | |
255 return NO; | |
256 } | |
257 } | |
258 | |
259 @end | |
OLD | NEW |