Chromium Code Reviews| 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/ui/payments/payment_request_selector_view_controller .h" | 5 #import "ios/chrome/browser/ui/payments/payment_request_selector_view_controller .h" |
| 6 | 6 |
| 7 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
| 8 #include "components/strings/grit/components_strings.h" | 8 #include "components/strings/grit/components_strings.h" |
| 9 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" | 9 #import "ios/chrome/browser/ui/autofill/cells/status_item.h" |
| 10 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h" | 10 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h" |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 61 UIBarButtonItem* backButton = | 61 UIBarButtonItem* backButton = |
| 62 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | 62 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 63 target:nil | 63 target:nil |
| 64 action:@selector(onBack)]; | 64 action:@selector(onBack)]; |
| 65 backButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | 65 backButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); |
| 66 self.navigationItem.leftBarButtonItem = backButton; | 66 self.navigationItem.leftBarButtonItem = backButton; |
| 67 } | 67 } |
| 68 return self; | 68 return self; |
| 69 } | 69 } |
| 70 | 70 |
| 71 // Returns a custom edit bar button item. | |
| 72 - (UIBarButtonItem*)editButton { | |
| 73 UIBarButtonItem* button = [[UIBarButtonItem alloc] | |
| 74 initWithTitle:l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON) | |
| 75 style:UIBarButtonItemStyleDone | |
| 76 target:self | |
| 77 action:@selector(editButtonPressed)]; | |
| 78 return button; | |
| 79 } | |
| 80 | |
| 81 // Returns a custom done bar button item. | |
| 82 - (UIBarButtonItem*)doneButton { | |
| 83 return [[UIBarButtonItem alloc] | |
| 84 initWithTitle:l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON) | |
| 85 style:UIBarButtonItemStyleDone | |
| 86 target:self | |
| 87 action:@selector(editButtonPressed)]; | |
| 88 } | |
| 89 | |
| 90 - (void)editButtonPressed { | |
| 91 PaymentRequestSelectorState newState = | |
| 92 self.dataSource.state == PaymentRequestSelectorStateEdit | |
| 93 ? PaymentRequestSelectorStateNormal | |
| 94 : PaymentRequestSelectorStateEdit; | |
| 95 if ([self.delegate | |
| 96 respondsToSelector:@selector | |
| 97 (paymentRequestSelectorViewController:didChangeToState:)]) { | |
| 98 [self.delegate paymentRequestSelectorViewController:self | |
| 99 didChangeToState:newState]; | |
| 100 } | |
| 101 } | |
| 102 | |
| 71 #pragma mark - PaymentRequestSelectorViewControllerActions | 103 #pragma mark - PaymentRequestSelectorViewControllerActions |
| 72 | 104 |
| 73 - (void)onBack { | 105 - (void)onBack { |
| 74 [self.delegate paymentRequestSelectorViewControllerDidFinish:self]; | 106 [self.delegate paymentRequestSelectorViewControllerDidFinish:self]; |
| 75 } | 107 } |
| 76 | 108 |
| 77 #pragma mark - CollectionViewController methods | 109 #pragma mark - CollectionViewController methods |
| 78 | 110 |
| 79 - (void)loadModel { | 111 - (void)loadModel { |
| 80 [super loadModel]; | 112 [super loadModel]; |
| 81 CollectionViewModel* model = self.collectionViewModel; | 113 CollectionViewModel* model = self.collectionViewModel; |
| 82 | 114 |
| 115 // Set up trailing (edit or done) button. | |
| 116 if ([self.dataSource allowsEditMode]) { | |
|
lpromero
2017/06/14 09:10:12
If !allowsEditMode, should you clear any present b
Moe
2017/06/14 14:35:05
allowsEditMode shouldn't change during the lifetim
| |
| 117 self.navigationItem.rightBarButtonItem = | |
| 118 self.dataSource.state == PaymentRequestSelectorStateEdit | |
| 119 ? [self doneButton] | |
| 120 : [self editButton]; | |
| 121 } | |
| 122 | |
| 83 [model addSectionWithIdentifier:SectionIdentifierItems]; | 123 [model addSectionWithIdentifier:SectionIdentifierItems]; |
| 84 | 124 |
| 85 // If the view controller is in the pending state, only display a spinner and | 125 // If the view controller is in the pending state, only display a spinner and |
| 86 // a message indicating the pending state. | 126 // a message indicating the pending state. |
| 87 if (self.dataSource.state == PaymentRequestSelectorStatePending) { | 127 if (self.dataSource.state == PaymentRequestSelectorStatePending) { |
| 88 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; | 128 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; |
| 89 statusItem.state = StatusItemState::VERIFYING; | 129 statusItem.state = StatusItemState::VERIFYING; |
| 90 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); | 130 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); |
| 91 [model addItem:statusItem toSectionWithIdentifier:SectionIdentifierItems]; | 131 [model addItem:statusItem toSectionWithIdentifier:SectionIdentifierItems]; |
| 92 return; | 132 return; |
| 93 } | 133 } |
| 94 | 134 |
| 95 CollectionViewItem* headerItem = [self.dataSource headerItem]; | 135 if (self.dataSource.state != PaymentRequestSelectorStateEdit) { |
| 96 if (headerItem) { | 136 CollectionViewItem* headerItem = [self.dataSource headerItem]; |
| 97 headerItem.type = ItemTypeHeader; | 137 if (headerItem) { |
| 98 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems]; | 138 headerItem.type = ItemTypeHeader; |
| 139 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems]; | |
| 140 } | |
| 99 } | 141 } |
| 100 | 142 |
| 101 [[self.dataSource selectableItems] | 143 [[self.dataSource selectableItems] |
| 102 enumerateObjectsUsingBlock:^( | 144 enumerateObjectsUsingBlock:^( |
| 103 CollectionViewItem<PaymentsHasAccessoryType>* item, NSUInteger index, | 145 CollectionViewItem<PaymentsHasAccessoryType>* item, NSUInteger index, |
| 104 BOOL* stop) { | 146 BOOL* stop) { |
| 105 DCHECK([item respondsToSelector:@selector(accessoryType)]); | 147 DCHECK([item respondsToSelector:@selector(accessoryType)]); |
| 106 item.type = ItemTypeSelectableItem; | 148 item.type = ItemTypeSelectableItem; |
| 107 item.accessibilityTraits |= UIAccessibilityTraitButton; | 149 item.accessibilityTraits |= UIAccessibilityTraitButton; |
| 108 item.accessoryType = (index == self.dataSource.selectedItemIndex) | 150 if (self.dataSource.state == PaymentRequestSelectorStateEdit) { |
| 109 ? MDCCollectionViewCellAccessoryCheckmark | 151 item.accessoryType = |
| 110 : MDCCollectionViewCellAccessoryNone; | 152 MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 153 } else { | |
| 154 item.accessoryType = (index == self.dataSource.selectedItemIndex) | |
| 155 ? MDCCollectionViewCellAccessoryCheckmark | |
| 156 : MDCCollectionViewCellAccessoryNone; | |
| 157 } | |
| 111 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems]; | 158 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems]; |
| 112 }]; | 159 }]; |
| 113 | 160 |
| 114 CollectionViewItem* addButtonItem = [self.dataSource addButtonItem]; | 161 if (self.dataSource.state != PaymentRequestSelectorStateEdit) { |
| 115 if (addButtonItem) { | 162 CollectionViewItem* addButtonItem = [self.dataSource addButtonItem]; |
| 116 addButtonItem.type = ItemTypeAddItem; | 163 if (addButtonItem) { |
| 117 addButtonItem.accessibilityTraits |= UIAccessibilityTraitButton; | 164 addButtonItem.type = ItemTypeAddItem; |
| 118 [model addItem:addButtonItem | 165 addButtonItem.accessibilityTraits |= UIAccessibilityTraitButton; |
| 119 toSectionWithIdentifier:SectionIdentifierItems]; | 166 [model addItem:addButtonItem |
| 167 toSectionWithIdentifier:SectionIdentifierItems]; | |
| 168 } | |
| 120 } | 169 } |
| 121 } | 170 } |
| 122 | 171 |
| 123 - (void)viewDidLoad { | 172 - (void)viewDidLoad { |
| 124 [super viewDidLoad]; | 173 [super viewDidLoad]; |
| 125 self.collectionView.accessibilityIdentifier = | 174 self.collectionView.accessibilityIdentifier = |
| 126 kPaymentRequestSelectorCollectionViewAccessibilityID; | 175 kPaymentRequestSelectorCollectionViewAccessibilityID; |
| 127 | 176 |
| 128 // Customize collection view settings. | 177 // Customize collection view settings. |
| 129 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | 178 self.styler.cellStyle = MDCCollectionViewCellStyleCard; |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 | 231 |
| 183 #pragma mark UICollectionViewDelegate | 232 #pragma mark UICollectionViewDelegate |
| 184 | 233 |
| 185 - (void)collectionView:(UICollectionView*)collectionView | 234 - (void)collectionView:(UICollectionView*)collectionView |
| 186 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 235 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 187 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | 236 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 188 | 237 |
| 189 CollectionViewModel* model = self.collectionViewModel; | 238 CollectionViewModel* model = self.collectionViewModel; |
| 190 | 239 |
| 191 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | 240 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; |
| 241 | |
| 242 if (self.dataSource.state == PaymentRequestSelectorStateEdit) { | |
| 243 DCHECK(item.type == ItemTypeSelectableItem); | |
| 244 // Notify the delegate of the selection. | |
| 245 NSUInteger index = | |
| 246 [self.collectionViewModel indexInItemTypeForIndexPath:indexPath]; | |
| 247 DCHECK(index < [[self.dataSource selectableItems] count]); | |
| 248 if ([self.delegate | |
| 249 respondsToSelector:@selector | |
| 250 (paymentRequestSelectorViewController:didSelectItemAtIndexForEditing | |
| 251 :)]) { | |
|
lpromero
2017/06/14 09:10:13
No action needed: this line is awesome :p
Moe
2017/06/14 14:35:05
:)])
| |
| 252 [self.delegate paymentRequestSelectorViewController:self | |
| 253 didSelectItemAtIndexForEditing:index]; | |
| 254 } | |
| 255 return; | |
| 256 } | |
| 257 | |
| 192 switch (item.type) { | 258 switch (item.type) { |
| 193 case ItemTypeSelectableItem: { | 259 case ItemTypeSelectableItem: { |
| 194 // Update the currently selected cell, if any. | 260 // Update the currently selected cell, if any. |
| 195 if (self.dataSource.selectedItemIndex != NSUIntegerMax) { | 261 if (self.dataSource.selectedItemIndex != NSUIntegerMax) { |
| 196 DCHECK(self.dataSource.selectedItemIndex < | 262 DCHECK(self.dataSource.selectedItemIndex < |
| 197 [[self.dataSource selectableItems] count]); | 263 [[self.dataSource selectableItems] count]); |
| 198 CollectionViewItem<PaymentsHasAccessoryType>* oldSelectedItem = | 264 CollectionViewItem<PaymentsHasAccessoryType>* oldSelectedItem = |
| 199 [[self.dataSource selectableItems] | 265 [[self.dataSource selectableItems] |
| 200 objectAtIndex:self.dataSource.selectedItemIndex]; | 266 objectAtIndex:self.dataSource.selectedItemIndex]; |
| 201 oldSelectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | 267 oldSelectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 251 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | 317 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 252 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | 318 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 253 if (type == ItemTypeHeader) { | 319 if (type == ItemTypeHeader) { |
| 254 return YES; | 320 return YES; |
| 255 } else { | 321 } else { |
| 256 return NO; | 322 return NO; |
| 257 } | 323 } |
| 258 } | 324 } |
| 259 | 325 |
| 260 @end | 326 @end |
| OLD | NEW |