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 #import "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/strings/grit/components_strings.h" |
| 11 #import "ios/chrome/browser/payments/payment_request_utils.h" |
| 12 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 14 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 15 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
| 16 #include "ios/chrome/grit/ios_strings.h" |
| 17 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 18 #import "ios/third_party/material_components_ios/src/components/Typography/src/M
aterialTypography.h" |
| 19 #include "ui/base/l10n/l10n_util.h" |
| 20 |
| 21 NSString* const kShippingOptionSelectionCollectionViewId = |
| 22 @"kShippingOptionSelectionCollectionViewId"; |
| 23 |
| 24 namespace { |
| 25 |
| 26 const CGFloat kSeparatorEdgeInset = 14; |
| 27 |
| 28 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 29 SectionIdentifierShippingOption = kSectionIdentifierEnumZero, |
| 30 }; |
| 31 |
| 32 typedef NS_ENUM(NSInteger, ItemType) { |
| 33 ItemTypeShippingOption = kItemTypeEnumZero, // This is a repeated item type. |
| 34 }; |
| 35 |
| 36 } // namespace |
| 37 |
| 38 @interface ShippingOptionSelectionViewController () { |
| 39 base::WeakNSProtocol<id<ShippingOptionSelectionViewControllerDelegate>> |
| 40 _delegate; |
| 41 |
| 42 CollectionViewTextItem* _selectedItem; |
| 43 } |
| 44 |
| 45 // Called when the user presses the return button. |
| 46 - (void)onReturn; |
| 47 |
| 48 @end |
| 49 |
| 50 @implementation ShippingOptionSelectionViewController |
| 51 |
| 52 @synthesize shippingOptions = _shippingOptions; |
| 53 @synthesize selectedShippingOption = _selectedShippingOption; |
| 54 |
| 55 - (instancetype)init { |
| 56 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 57 self.title = l10n_util::GetNSString( |
| 58 IDS_IOS_PAYMENT_REQUEST_SHIPPING_OPTION_SELECTION_TITLE); |
| 59 |
| 60 UIBarButtonItem* returnButton = |
| 61 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 62 target:nil |
| 63 action:@selector(onReturn)]; |
| 64 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); |
| 65 self.navigationItem.leftBarButtonItem = returnButton; |
| 66 } |
| 67 return self; |
| 68 } |
| 69 |
| 70 - (id<ShippingOptionSelectionViewControllerDelegate>)delegate { |
| 71 return _delegate.get(); |
| 72 } |
| 73 |
| 74 - (void)setDelegate: |
| 75 (id<ShippingOptionSelectionViewControllerDelegate>)delegate { |
| 76 _delegate.reset(delegate); |
| 77 } |
| 78 |
| 79 - (void)onReturn { |
| 80 [_delegate shippingOptionSelectionViewControllerDidReturn:self]; |
| 81 } |
| 82 |
| 83 #pragma mark - CollectionViewController methods |
| 84 |
| 85 - (void)loadModel { |
| 86 [super loadModel]; |
| 87 CollectionViewModel* model = self.collectionViewModel; |
| 88 |
| 89 [model addSectionWithIdentifier:SectionIdentifierShippingOption]; |
| 90 |
| 91 for (size_t i = 0; i < _shippingOptions.size(); ++i) { |
| 92 web::PaymentShippingOption* shippingOption = _shippingOptions[i]; |
| 93 CollectionViewTextItem* item = [[[CollectionViewTextItem alloc] |
| 94 initWithType:ItemTypeShippingOption] autorelease]; |
| 95 item.text = base::SysUTF16ToNSString(shippingOption->label); |
| 96 NSString* currencyCode = |
| 97 base::SysUTF16ToNSString(shippingOption->amount.currency); |
| 98 NSDecimalNumber* value = [NSDecimalNumber |
| 99 decimalNumberWithString:SysUTF16ToNSString( |
| 100 shippingOption->amount.value)]; |
| 101 item.detailText = |
| 102 payment_request_utils::FormattedCurrencyString(value, currencyCode); |
| 103 |
| 104 if (_selectedShippingOption == shippingOption) { |
| 105 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 106 _selectedItem = item; |
| 107 } |
| 108 |
| 109 [model addItem:item |
| 110 toSectionWithIdentifier:SectionIdentifierShippingOption]; |
| 111 } |
| 112 } |
| 113 |
| 114 - (void)viewDidLoad { |
| 115 [super viewDidLoad]; |
| 116 self.collectionView.accessibilityIdentifier = |
| 117 kShippingOptionSelectionCollectionViewId; |
| 118 |
| 119 // Customize collection view settings. |
| 120 self.styler.cellStyle = MDCCollectionViewCellStyleCard; |
| 121 self.styler.separatorInset = |
| 122 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); |
| 123 } |
| 124 |
| 125 #pragma mark UICollectionViewDataSource |
| 126 |
| 127 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 128 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { |
| 129 UICollectionViewCell* cell = |
| 130 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 131 |
| 132 NSInteger itemType = |
| 133 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 134 DCHECK(ItemTypeShippingOption == itemType); |
| 135 |
| 136 MDCCollectionViewTextCell* textCell = |
| 137 base::mac::ObjCCastStrict<MDCCollectionViewTextCell>(cell); |
| 138 textCell.textLabel.font = [MDCTypography body2Font]; |
| 139 textCell.textLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 140 textCell.detailTextLabel.font = [MDCTypography body1Font]; |
| 141 textCell.detailTextLabel.textColor = [[MDCPalette greyPalette] tint900]; |
| 142 |
| 143 return cell; |
| 144 } |
| 145 |
| 146 #pragma mark UICollectionViewDelegate |
| 147 |
| 148 - (void)collectionView:(UICollectionView*)collectionView |
| 149 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 150 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 151 |
| 152 CollectionViewModel* model = self.collectionViewModel; |
| 153 |
| 154 NSInteger itemType = |
| 155 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 156 DCHECK(ItemTypeShippingOption == itemType); |
| 157 |
| 158 NSIndexPath* currentlySelectedIndexPath = [self.collectionViewModel |
| 159 indexPathForItem:_selectedItem |
| 160 inSectionWithIdentifier:SectionIdentifierShippingOption]; |
| 161 if (currentlySelectedIndexPath != indexPath) { |
| 162 // Update the cells. |
| 163 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; |
| 164 CollectionViewTextItem* newlySelectedItem = |
| 165 base::mac::ObjCCastStrict<CollectionViewTextItem>(item); |
| 166 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 167 |
| 168 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
| 169 |
| 170 [self reconfigureCellsForItems:@[ _selectedItem, newlySelectedItem ] |
| 171 inSectionWithIdentifier:SectionIdentifierShippingOption]; |
| 172 |
| 173 // Update the selected shipping option and its respective item. |
| 174 NSInteger index = [model indexInItemTypeForIndexPath:indexPath]; |
| 175 DCHECK(index < (NSInteger)_shippingOptions.size()); |
| 176 self.selectedShippingOption = _shippingOptions[index]; |
| 177 _selectedItem = newlySelectedItem; |
| 178 } |
| 179 [_delegate shippingOptionSelectionViewController:self |
| 180 selectedShippingOption:self.selectedShippingOption]; |
| 181 } |
| 182 |
| 183 #pragma mark MDCCollectionViewStylingDelegate |
| 184 |
| 185 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 186 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 187 return MDCCellDefaultTwoLineHeight; |
| 188 } |
| 189 |
| 190 @end |
OLD | NEW |