| 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/payments/shipping_option_selection_view_controller.h
" | |
| 6 | |
| 7 #include "base/mac/foundation_util.h" | |
| 8 #include "base/strings/sys_string_conversions.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "components/payments/core/currency_formatter.h" | |
| 11 #include "components/payments/core/strings_util.h" | |
| 12 #include "components/strings/grit/components_strings.h" | |
| 13 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | |
| 14 #include "ios/chrome/browser/payments/payment_request.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" | |
| 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" | |
| 19 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | |
| 20 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | |
| 21 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | |
| 22 #include "ios/chrome/browser/ui/uikit_ui_util.h" | |
| 23 #include "ios/chrome/grit/ios_strings.h" | |
| 24 #include "ios/chrome/grit/ios_theme_resources.h" | |
| 25 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" | |
| 26 #include "ios/web/public/payments/payment_request.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 | |
| 29 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 30 #error "This file requires ARC support." | |
| 31 #endif | |
| 32 | |
| 33 namespace { | |
| 34 using ::payments::GetShippingOptionSectionString; | |
| 35 | |
| 36 NSString* const kShippingOptionSelectionCollectionViewID = | |
| 37 @"kShippingOptionSelectionCollectionViewID"; | |
| 38 | |
| 39 const CGFloat kSeparatorEdgeInset = 14; | |
| 40 | |
| 41 typedef NS_ENUM(NSInteger, SectionIdentifier) { | |
| 42 SectionIdentifierShippingOption = kSectionIdentifierEnumZero, | |
| 43 }; | |
| 44 | |
| 45 typedef NS_ENUM(NSInteger, ItemType) { | |
| 46 ItemTypeSpinner = kItemTypeEnumZero, | |
| 47 ItemTypeMessage, | |
| 48 ItemTypeShippingOption, // This is a repeated item type. | |
| 49 }; | |
| 50 | |
| 51 } // namespace | |
| 52 | |
| 53 @interface ShippingOptionSelectionViewController ()< | |
| 54 ShippingOptionSelectionViewControllerActions> { | |
| 55 // The PaymentRequest object having a copy of web::PaymentRequest as provided | |
| 56 // by the page invoking the Payment Request API. This is a weak pointer and | |
| 57 // should outlive this class. | |
| 58 PaymentRequest* _paymentRequest; | |
| 59 | |
| 60 // The currently selected item. May be nil. | |
| 61 PaymentsTextItem* _selectedItem; | |
| 62 } | |
| 63 | |
| 64 @end | |
| 65 | |
| 66 @implementation ShippingOptionSelectionViewController | |
| 67 | |
| 68 @synthesize pending = _pending; | |
| 69 @synthesize errorMessage = _errorMessage; | |
| 70 @synthesize delegate = _delegate; | |
| 71 | |
| 72 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | |
| 73 DCHECK(paymentRequest); | |
| 74 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | |
| 75 self.title = base::SysUTF16ToNSString( | |
| 76 GetShippingOptionSectionString(paymentRequest->shipping_type())); | |
| 77 | |
| 78 // Set up leading (return) button. | |
| 79 UIBarButtonItem* returnButton = | |
| 80 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | |
| 81 target:nil | |
| 82 action:@selector(onReturn)]; | |
| 83 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | |
| 84 self.navigationItem.leftBarButtonItem = returnButton; | |
| 85 | |
| 86 _paymentRequest = paymentRequest; | |
| 87 } | |
| 88 return self; | |
| 89 } | |
| 90 | |
| 91 - (void)onReturn { | |
| 92 [_delegate shippingOptionSelectionViewControllerDidReturn:self]; | |
| 93 } | |
| 94 | |
| 95 #pragma mark - CollectionViewController methods | |
| 96 | |
| 97 - (void)loadModel { | |
| 98 [super loadModel]; | |
| 99 CollectionViewModel* model = self.collectionViewModel; | |
| 100 _selectedItem = nil; | |
| 101 | |
| 102 [model addSectionWithIdentifier:SectionIdentifierShippingOption]; | |
| 103 | |
| 104 if (self.pending) { | |
| 105 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; | |
| 106 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); | |
| 107 [model addItem:statusItem | |
| 108 toSectionWithIdentifier:SectionIdentifierShippingOption]; | |
| 109 return; | |
| 110 } | |
| 111 | |
| 112 if (_errorMessage) { | |
| 113 PaymentsTextItem* messageItem = | |
| 114 [[PaymentsTextItem alloc] initWithType:ItemTypeMessage]; | |
| 115 messageItem.text = _errorMessage; | |
| 116 messageItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | |
| 117 [model addItem:messageItem | |
| 118 toSectionWithIdentifier:SectionIdentifierShippingOption]; | |
| 119 } | |
| 120 | |
| 121 for (const auto* shippingOption : _paymentRequest->shipping_options()) { | |
| 122 PaymentsTextItem* item = | |
| 123 [[PaymentsTextItem alloc] initWithType:ItemTypeShippingOption]; | |
| 124 item.text = base::SysUTF16ToNSString(shippingOption->label); | |
| 125 payments::CurrencyFormatter* currencyFormatter = | |
| 126 _paymentRequest->GetOrCreateCurrencyFormatter(); | |
| 127 item.detailText = SysUTF16ToNSString(currencyFormatter->Format( | |
| 128 base::UTF16ToASCII(shippingOption->amount.value))); | |
| 129 | |
| 130 if (_paymentRequest->selected_shipping_option() == shippingOption) { | |
| 131 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
| 132 _selectedItem = item; | |
| 133 } | |
| 134 | |
| 135 [model addItem:item | |
| 136 toSectionWithIdentifier:SectionIdentifierShippingOption]; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 - (void)viewDidLoad { | |
| 141 [super viewDidLoad]; | |
| 142 self.collectionView.accessibilityIdentifier = | |
| 143 kShippingOptionSelectionCollectionViewID; | |
| 144 | |
| 145 // Customize collection view settings. | |
| 146 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | |
| 147 self.styler.separatorInset = | |
| 148 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); | |
| 149 } | |
| 150 | |
| 151 #pragma mark UICollectionViewDelegate | |
| 152 | |
| 153 - (void)collectionView:(UICollectionView*)collectionView | |
| 154 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
| 155 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | |
| 156 | |
| 157 CollectionViewModel* model = self.collectionViewModel; | |
| 158 | |
| 159 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | |
| 160 if (item.type == ItemTypeShippingOption) { | |
| 161 // Update the currently selected cell, if any. | |
| 162 if (_selectedItem) { | |
| 163 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | |
| 164 [self reconfigureCellsForItems:@[ _selectedItem ] | |
| 165 inSectionWithIdentifier:SectionIdentifierShippingOption]; | |
| 166 } | |
| 167 | |
| 168 // Update the newly selected cell. | |
| 169 PaymentsTextItem* newlySelectedItem = | |
| 170 base::mac::ObjCCastStrict<PaymentsTextItem>(item); | |
| 171 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
| 172 [self reconfigureCellsForItems:@[ newlySelectedItem ] | |
| 173 inSectionWithIdentifier:SectionIdentifierShippingOption]; | |
| 174 | |
| 175 // Update the reference to the selected item. | |
| 176 _selectedItem = newlySelectedItem; | |
| 177 | |
| 178 // Notify the delegate of the selection. | |
| 179 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; | |
| 180 DCHECK(index < (NSInteger)_paymentRequest->shipping_options().size()); | |
| 181 [_delegate | |
| 182 shippingOptionSelectionViewController:self | |
| 183 didSelectShippingOption:_paymentRequest | |
| 184 ->shipping_options()[index]]; | |
| 185 } | |
| 186 } | |
| 187 | |
| 188 #pragma mark MDCCollectionViewStylingDelegate | |
| 189 | |
| 190 - (CGFloat)collectionView:(UICollectionView*)collectionView | |
| 191 cellHeightAtIndexPath:(NSIndexPath*)indexPath { | |
| 192 CollectionViewItem* item = | |
| 193 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
| 194 switch (item.type) { | |
| 195 case ItemTypeMessage: | |
| 196 case ItemTypeSpinner: | |
| 197 return [MDCCollectionViewCell | |
| 198 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) | |
| 199 forItem:item]; | |
| 200 case ItemTypeShippingOption: | |
| 201 return MDCCellDefaultTwoLineHeight; | |
| 202 default: | |
| 203 NOTREACHED(); | |
| 204 return MDCCellDefaultOneLineHeight; | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 - (BOOL)collectionView:(UICollectionView*)collectionView | |
| 209 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | |
| 210 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
| 211 if (type == ItemTypeMessage) { | |
| 212 return YES; | |
| 213 } else { | |
| 214 return NO; | |
| 215 } | |
| 216 } | |
| 217 | |
| 218 @end | |
| OLD | NEW |