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/payments/shipping_option_selection_view_controller.h
" | 5 #import "ios/chrome/browser/payments/shipping_option_selection_view_controller.h
" |
6 | 6 |
7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
8 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "components/payments/core/currency_formatter.h" | 10 #include "components/payments/core/currency_formatter.h" |
11 #include "components/payments/core/strings_util.h" | 11 #include "components/payments/core/strings_util.h" |
12 #include "components/strings/grit/components_strings.h" | 12 #include "components/strings/grit/components_strings.h" |
13 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | 13 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
14 #include "ios/chrome/browser/payments/payment_request.h" | 14 #include "ios/chrome/browser/payments/payment_request.h" |
15 #import "ios/chrome/browser/payments/shipping_option_selection_view_controller_a
ctions.h" | 15 #import "ios/chrome/browser/payments/shipping_option_selection_view_controller_a
ctions.h" |
16 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" | 16 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" |
17 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" | 17 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | |
20 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 19 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
21 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | 20 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
22 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | 21 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
23 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 22 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
24 #include "ios/chrome/grit/ios_strings.h" | 23 #include "ios/chrome/grit/ios_strings.h" |
25 #include "ios/chrome/grit/ios_theme_resources.h" | 24 #include "ios/chrome/grit/ios_theme_resources.h" |
26 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | 25 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
27 #include "ios/web/public/payments/payment_request.h" | 26 #include "ios/web/public/payments/payment_request.h" |
28 #include "ui/base/l10n/l10n_util.h" | 27 #include "ui/base/l10n/l10n_util.h" |
29 | 28 |
(...skipping 22 matching lines...) Expand all Loading... |
52 } // namespace | 51 } // namespace |
53 | 52 |
54 @interface ShippingOptionSelectionViewController ()< | 53 @interface ShippingOptionSelectionViewController ()< |
55 ShippingOptionSelectionViewControllerActions> { | 54 ShippingOptionSelectionViewControllerActions> { |
56 // The PaymentRequest object having a copy of web::PaymentRequest as provided | 55 // The PaymentRequest object having a copy of web::PaymentRequest as provided |
57 // by the page invoking the Payment Request API. This is a weak pointer and | 56 // by the page invoking the Payment Request API. This is a weak pointer and |
58 // should outlive this class. | 57 // should outlive this class. |
59 PaymentRequest* _paymentRequest; | 58 PaymentRequest* _paymentRequest; |
60 | 59 |
61 // The currently selected item. May be nil. | 60 // The currently selected item. May be nil. |
62 CollectionViewTextItem* _selectedItem; | 61 PaymentsTextItem* _selectedItem; |
63 } | 62 } |
64 | 63 |
65 @end | 64 @end |
66 | 65 |
67 @implementation ShippingOptionSelectionViewController | 66 @implementation ShippingOptionSelectionViewController |
68 | 67 |
69 @synthesize pending = _pending; | 68 @synthesize pending = _pending; |
70 @synthesize errorMessage = _errorMessage; | 69 @synthesize errorMessage = _errorMessage; |
71 @synthesize delegate = _delegate; | 70 @synthesize delegate = _delegate; |
72 | 71 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 if (_errorMessage) { | 112 if (_errorMessage) { |
114 PaymentsTextItem* messageItem = | 113 PaymentsTextItem* messageItem = |
115 [[PaymentsTextItem alloc] initWithType:ItemTypeMessage]; | 114 [[PaymentsTextItem alloc] initWithType:ItemTypeMessage]; |
116 messageItem.text = _errorMessage; | 115 messageItem.text = _errorMessage; |
117 messageItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | 116 messageItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); |
118 [model addItem:messageItem | 117 [model addItem:messageItem |
119 toSectionWithIdentifier:SectionIdentifierShippingOption]; | 118 toSectionWithIdentifier:SectionIdentifierShippingOption]; |
120 } | 119 } |
121 | 120 |
122 for (const auto* shippingOption : _paymentRequest->shipping_options()) { | 121 for (const auto* shippingOption : _paymentRequest->shipping_options()) { |
123 CollectionViewTextItem* item = | 122 PaymentsTextItem* item = |
124 [[CollectionViewTextItem alloc] initWithType:ItemTypeShippingOption]; | 123 [[PaymentsTextItem alloc] initWithType:ItemTypeShippingOption]; |
125 item.text = base::SysUTF16ToNSString(shippingOption->label); | 124 item.text = base::SysUTF16ToNSString(shippingOption->label); |
126 payments::CurrencyFormatter* currencyFormatter = | 125 payments::CurrencyFormatter* currencyFormatter = |
127 _paymentRequest->GetOrCreateCurrencyFormatter(); | 126 _paymentRequest->GetOrCreateCurrencyFormatter(); |
128 item.detailText = SysUTF16ToNSString(currencyFormatter->Format( | 127 item.detailText = SysUTF16ToNSString(currencyFormatter->Format( |
129 base::UTF16ToASCII(shippingOption->amount.value))); | 128 base::UTF16ToASCII(shippingOption->amount.value))); |
130 | 129 |
131 // Styling. | |
132 item.textFont = [MDCTypography body2Font]; | |
133 item.textColor = [[MDCPalette greyPalette] tint900]; | |
134 item.detailTextFont = [MDCTypography body1Font]; | |
135 item.detailTextColor = [[MDCPalette greyPalette] tint900]; | |
136 | |
137 if (_paymentRequest->selected_shipping_option() == shippingOption) { | 130 if (_paymentRequest->selected_shipping_option() == shippingOption) { |
138 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | 131 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
139 _selectedItem = item; | 132 _selectedItem = item; |
140 } | 133 } |
141 | 134 |
142 [model addItem:item | 135 [model addItem:item |
143 toSectionWithIdentifier:SectionIdentifierShippingOption]; | 136 toSectionWithIdentifier:SectionIdentifierShippingOption]; |
144 } | 137 } |
145 } | 138 } |
146 | 139 |
147 - (void)viewDidLoad { | 140 - (void)viewDidLoad { |
148 [super viewDidLoad]; | 141 [super viewDidLoad]; |
149 self.collectionView.accessibilityIdentifier = | 142 self.collectionView.accessibilityIdentifier = |
150 kShippingOptionSelectionCollectionViewID; | 143 kShippingOptionSelectionCollectionViewID; |
151 | 144 |
152 // Customize collection view settings. | 145 // Customize collection view settings. |
153 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | 146 self.styler.cellStyle = MDCCollectionViewCellStyleCard; |
154 self.styler.separatorInset = | 147 self.styler.separatorInset = |
155 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); | 148 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); |
156 } | 149 } |
157 | 150 |
158 #pragma mark UICollectionViewDataSource | |
159 | |
160 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView | |
161 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
162 UICollectionViewCell* cell = | |
163 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; | |
164 | |
165 NSInteger itemType = | |
166 [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
167 switch (itemType) { | |
168 case ItemTypeMessage: { | |
169 PaymentsTextCell* messageCell = | |
170 base::mac::ObjCCastStrict<PaymentsTextCell>(cell); | |
171 messageCell.textLabel.textColor = [[MDCPalette cr_redPalette] tint600]; | |
172 break; | |
173 } | |
174 default: | |
175 break; | |
176 } | |
177 return cell; | |
178 } | |
179 | |
180 #pragma mark UICollectionViewDelegate | 151 #pragma mark UICollectionViewDelegate |
181 | 152 |
182 - (void)collectionView:(UICollectionView*)collectionView | 153 - (void)collectionView:(UICollectionView*)collectionView |
183 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 154 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
184 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | 155 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
185 | 156 |
186 CollectionViewModel* model = self.collectionViewModel; | 157 CollectionViewModel* model = self.collectionViewModel; |
187 | 158 |
188 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | 159 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; |
189 if (item.type == ItemTypeShippingOption) { | 160 if (item.type == ItemTypeShippingOption) { |
190 // Update the currently selected cell, if any. | 161 // Update the currently selected cell, if any. |
191 if (_selectedItem) { | 162 if (_selectedItem) { |
192 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | 163 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
193 [self reconfigureCellsForItems:@[ _selectedItem ] | 164 [self reconfigureCellsForItems:@[ _selectedItem ] |
194 inSectionWithIdentifier:SectionIdentifierShippingOption]; | 165 inSectionWithIdentifier:SectionIdentifierShippingOption]; |
195 } | 166 } |
196 | 167 |
197 // Update the newly selected cell. | 168 // Update the newly selected cell. |
198 CollectionViewTextItem* newlySelectedItem = | 169 PaymentsTextItem* newlySelectedItem = |
199 base::mac::ObjCCastStrict<CollectionViewTextItem>(item); | 170 base::mac::ObjCCastStrict<PaymentsTextItem>(item); |
200 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | 171 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
201 [self reconfigureCellsForItems:@[ newlySelectedItem ] | 172 [self reconfigureCellsForItems:@[ newlySelectedItem ] |
202 inSectionWithIdentifier:SectionIdentifierShippingOption]; | 173 inSectionWithIdentifier:SectionIdentifierShippingOption]; |
203 | 174 |
204 // Update the reference to the selected item. | 175 // Update the reference to the selected item. |
205 _selectedItem = newlySelectedItem; | 176 _selectedItem = newlySelectedItem; |
206 | 177 |
207 // Notify the delegate of the selection. | 178 // Notify the delegate of the selection. |
208 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; | 179 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; |
209 DCHECK(index < (NSInteger)_paymentRequest->shipping_options().size()); | 180 DCHECK(index < (NSInteger)_paymentRequest->shipping_options().size()); |
(...skipping 28 matching lines...) Expand all Loading... |
238 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | 209 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
239 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | 210 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
240 if (type == ItemTypeMessage) { | 211 if (type == ItemTypeMessage) { |
241 return YES; | 212 return YES; |
242 } else { | 213 } else { |
243 return NO; | 214 return NO; |
244 } | 215 } |
245 } | 216 } |
246 | 217 |
247 @end | 218 @end |
OLD | NEW |