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/payment_request_selector_view_controller.h" |
| 6 |
| 7 #include "base/mac/foundation_util.h" |
| 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
| 10 #import "ios/chrome/browser/payments/payment_request_selector_view_controller_ac
tions.h" |
| 11 #import "ios/chrome/browser/payments/payment_request_selector_view_controller_da
ta_source.h" |
| 12 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" |
| 13 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 14 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item+collec
tion_view_controller.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 17 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
| 18 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
| 19 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 20 #include "ios/chrome/grit/ios_strings.h" |
| 21 #include "ios/chrome/grit/ios_theme_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
| 28 namespace { |
| 29 |
| 30 NSString* const kPaymentRequestSelectorCollectionViewAccessibilityID = |
| 31 @"kPaymentRequestSelectorCollectionViewAccessibilityID"; |
| 32 |
| 33 const CGFloat kSeparatorEdgeInset = 14; |
| 34 |
| 35 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 36 SectionIdentifierItems = kSectionIdentifierEnumZero, |
| 37 }; |
| 38 |
| 39 typedef NS_ENUM(NSInteger, ItemType) { |
| 40 ItemTypeHeader = kItemTypeEnumZero, |
| 41 ItemTypeSelectableItem, // This is a repeated item type. |
| 42 ItemTypeSpinner, |
| 43 ItemTypeAddItem, |
| 44 }; |
| 45 |
| 46 } // namespace |
| 47 |
| 48 @interface PaymentRequestSelectorViewController ()< |
| 49 PaymentRequestSelectorViewControllerActions> |
| 50 |
| 51 @end |
| 52 |
| 53 @implementation PaymentRequestSelectorViewController |
| 54 |
| 55 @synthesize delegate = _delegate; |
| 56 @synthesize dataSource = _dataSource; |
| 57 |
| 58 - (instancetype)init { |
| 59 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 60 // Set up leading (back) button. |
| 61 UIBarButtonItem* backButton = |
| 62 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 63 target:nil |
| 64 action:@selector(onBack)]; |
| 65 backButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); |
| 66 self.navigationItem.leftBarButtonItem = backButton; |
| 67 } |
| 68 return self; |
| 69 } |
| 70 |
| 71 #pragma mark - PaymentRequestSelectorViewControllerActions |
| 72 |
| 73 - (void)onBack { |
| 74 [self.delegate paymentRequestSelectorViewControllerDidFinish:self]; |
| 75 } |
| 76 |
| 77 #pragma mark - CollectionViewController methods |
| 78 |
| 79 - (void)loadModel { |
| 80 [super loadModel]; |
| 81 CollectionViewModel* model = self.collectionViewModel; |
| 82 |
| 83 [model addSectionWithIdentifier:SectionIdentifierItems]; |
| 84 |
| 85 // If the view controller is in the pending state, only display a spinner and |
| 86 // a message indicating the pending state. |
| 87 if (self.dataSource.state == PaymentRequestSelectorStatePending) { |
| 88 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; |
| 89 statusItem.state = StatusItemState::VERIFYING; |
| 90 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); |
| 91 [model addItem:statusItem toSectionWithIdentifier:SectionIdentifierItems]; |
| 92 return; |
| 93 } |
| 94 |
| 95 CollectionViewItem* headerItem = [self.dataSource headerItem]; |
| 96 if (headerItem) { |
| 97 headerItem.type = ItemTypeHeader; |
| 98 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems]; |
| 99 } |
| 100 |
| 101 [self.dataSource.selectableItems |
| 102 enumerateObjectsUsingBlock:^( |
| 103 CollectionViewItem<PaymentsHasAccessoryType>* item, NSUInteger index, |
| 104 BOOL* stop) { |
| 105 item.type = ItemTypeSelectableItem; |
| 106 item.accessibilityTraits |= UIAccessibilityTraitButton; |
| 107 item.accessoryType = (index == self.dataSource.selectedItemIndex) |
| 108 ? MDCCollectionViewCellAccessoryCheckmark |
| 109 : MDCCollectionViewCellAccessoryNone; |
| 110 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems]; |
| 111 }]; |
| 112 |
| 113 CollectionViewItem* addButtonItem = [self.dataSource addButtonItem]; |
| 114 if (addButtonItem) { |
| 115 addButtonItem.type = ItemTypeAddItem; |
| 116 addButtonItem.accessibilityTraits |= UIAccessibilityTraitButton; |
| 117 [model addItem:addButtonItem |
| 118 toSectionWithIdentifier:SectionIdentifierItems]; |
| 119 } |
| 120 } |
| 121 |
| 122 - (void)viewDidLoad { |
| 123 [super viewDidLoad]; |
| 124 self.collectionView.accessibilityIdentifier = |
| 125 kPaymentRequestSelectorCollectionViewAccessibilityID; |
| 126 |
| 127 // Customize collection view settings. |
| 128 self.styler.cellStyle = MDCCollectionViewCellStyleCard; |
| 129 self.styler.separatorInset = |
| 130 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); |
| 131 } |
| 132 |
| 133 #pragma mark UICollectionViewDelegate |
| 134 |
| 135 - (void)collectionView:(UICollectionView*)collectionView |
| 136 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 137 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 138 |
| 139 CollectionViewModel* model = self.collectionViewModel; |
| 140 |
| 141 CollectionViewItem<PaymentsHasAccessoryType>* item = |
| 142 [model itemAtIndexPath:indexPath]; |
| 143 switch (item.type) { |
| 144 case ItemTypeSelectableItem: { |
| 145 // Update the currently selected cell, if any. |
| 146 if (self.dataSource.selectedItemIndex != NSUIntegerMax) { |
| 147 CollectionViewItem<PaymentsHasAccessoryType>* selectedItem = |
| 148 [self.dataSource |
| 149 selectableItemAtIndex:self.dataSource.selectedItemIndex]; |
| 150 selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
| 151 [self reconfigureCellsForItems:@[ selectedItem ] |
| 152 inSectionWithIdentifier:SectionIdentifierItems]; |
| 153 } |
| 154 |
| 155 // Update the newly selected cell. |
| 156 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 157 [self reconfigureCellsForItems:@[ item ] |
| 158 inSectionWithIdentifier:SectionIdentifierItems]; |
| 159 |
| 160 // Notify the delegate of the selection. |
| 161 NSUInteger index = |
| 162 [self.collectionViewModel indexInItemTypeForIndexPath:indexPath]; |
| 163 DCHECK(index < [[self.dataSource selectableItems] count]); |
| 164 [self.delegate paymentRequestSelectorViewController:self |
| 165 didSelectItemAtIndex:index]; |
| 166 break; |
| 167 } |
| 168 case ItemTypeAddItem: { |
| 169 [self.delegate paymentRequestSelectorViewControllerDidSelectAddItem:self]; |
| 170 break; |
| 171 } |
| 172 default: |
| 173 break; |
| 174 } |
| 175 } |
| 176 |
| 177 #pragma mark MDCCollectionViewStylingDelegate |
| 178 |
| 179 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 180 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 181 CollectionViewItem* item = |
| 182 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 183 |
| 184 UIEdgeInsets inset = [self collectionView:collectionView |
| 185 layout:collectionView.collectionViewLayout |
| 186 insetForSectionAtIndex:indexPath.section]; |
| 187 |
| 188 return [MDCCollectionViewCell |
| 189 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) - |
| 190 inset.left - inset.right |
| 191 forItem:item]; |
| 192 } |
| 193 |
| 194 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 195 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 196 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 197 if (type == ItemTypeHeader) { |
| 198 return YES; |
| 199 } else { |
| 200 return NO; |
| 201 } |
| 202 } |
| 203 |
| 204 @end |
OLD | NEW |