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 30 matching lines...) Expand all Loading... |
41 ItemTypeSelectableItem, // This is a repeated item type. | 41 ItemTypeSelectableItem, // This is a repeated item type. |
42 ItemTypeSpinner, | 42 ItemTypeSpinner, |
43 ItemTypeAddItem, | 43 ItemTypeAddItem, |
44 }; | 44 }; |
45 | 45 |
46 } // namespace | 46 } // namespace |
47 | 47 |
48 @interface PaymentRequestSelectorViewController ()< | 48 @interface PaymentRequestSelectorViewController ()< |
49 PaymentRequestSelectorViewControllerActions> | 49 PaymentRequestSelectorViewControllerActions> |
50 | 50 |
| 51 // Whether or not the editor is in the editing mode. |
| 52 @property(nonatomic, assign, getter=isEditing) BOOL editing; |
| 53 |
51 @end | 54 @end |
52 | 55 |
53 @implementation PaymentRequestSelectorViewController | 56 @implementation PaymentRequestSelectorViewController |
54 | 57 |
55 @synthesize delegate = _delegate; | 58 @synthesize delegate = _delegate; |
56 @synthesize dataSource = _dataSource; | 59 @synthesize dataSource = _dataSource; |
| 60 @synthesize editing = _editing; |
57 | 61 |
58 - (instancetype)init { | 62 - (instancetype)init { |
59 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | 63 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 64 _editing = NO; |
| 65 |
60 // Set up leading (back) button. | 66 // Set up leading (back) button. |
61 UIBarButtonItem* backButton = | 67 UIBarButtonItem* backButton = |
62 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | 68 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
63 target:nil | 69 target:nil |
64 action:@selector(onBack)]; | 70 action:@selector(onBack)]; |
65 backButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | 71 backButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); |
66 self.navigationItem.leftBarButtonItem = backButton; | 72 self.navigationItem.leftBarButtonItem = backButton; |
67 } | 73 } |
68 return self; | 74 return self; |
69 } | 75 } |
70 | 76 |
| 77 // Returns a custom edit bar button item. |
| 78 - (UIBarButtonItem*)editButton { |
| 79 UIBarButtonItem* button = [[UIBarButtonItem alloc] |
| 80 initWithTitle:l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON) |
| 81 style:UIBarButtonItemStyleDone |
| 82 target:self |
| 83 action:@selector(editButtonPressed)]; |
| 84 return button; |
| 85 } |
| 86 |
| 87 // Returns a custom done bar button item. |
| 88 - (UIBarButtonItem*)doneButton { |
| 89 return [[UIBarButtonItem alloc] |
| 90 initWithTitle:l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON) |
| 91 style:UIBarButtonItemStyleDone |
| 92 target:self |
| 93 action:@selector(editButtonPressed)]; |
| 94 } |
| 95 |
| 96 - (void)editButtonPressed { |
| 97 // Toggle the editing mode and notify the delegate. |
| 98 self.editing = !self.editing; |
| 99 if ([self.delegate |
| 100 respondsToSelector:@selector |
| 101 (paymentRequestSelectorViewControllerDidToggleEditingMode:)]) { |
| 102 [self.delegate |
| 103 paymentRequestSelectorViewControllerDidToggleEditingMode:self]; |
| 104 } |
| 105 } |
| 106 |
71 #pragma mark - PaymentRequestSelectorViewControllerActions | 107 #pragma mark - PaymentRequestSelectorViewControllerActions |
72 | 108 |
73 - (void)onBack { | 109 - (void)onBack { |
74 [self.delegate paymentRequestSelectorViewControllerDidFinish:self]; | 110 [self.delegate paymentRequestSelectorViewControllerDidFinish:self]; |
75 } | 111 } |
76 | 112 |
77 #pragma mark - CollectionViewController methods | 113 #pragma mark - CollectionViewController methods |
78 | 114 |
79 - (void)loadModel { | 115 - (void)loadModel { |
80 [super loadModel]; | 116 [super loadModel]; |
81 CollectionViewModel* model = self.collectionViewModel; | 117 CollectionViewModel* model = self.collectionViewModel; |
82 | 118 |
| 119 // Set up trailing (edit or done) button. |
| 120 if (self.dataSource.state == PaymentRequestSelectorStateNormal && |
| 121 [self.dataSource allowsEditMode]) { |
| 122 self.navigationItem.rightBarButtonItem = |
| 123 self.editing ? [self doneButton] : [self editButton]; |
| 124 } else { |
| 125 self.navigationItem.rightBarButtonItem = nil; |
| 126 } |
| 127 |
83 [model addSectionWithIdentifier:SectionIdentifierItems]; | 128 [model addSectionWithIdentifier:SectionIdentifierItems]; |
84 | 129 |
85 // If the view controller is in the pending state, only display a spinner and | 130 // If the view controller is in the pending state, only display a spinner and |
86 // a message indicating the pending state. | 131 // a message indicating the pending state. |
87 if (self.dataSource.state == PaymentRequestSelectorStatePending) { | 132 if (self.dataSource.state == PaymentRequestSelectorStatePending) { |
88 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; | 133 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner]; |
89 statusItem.state = StatusItemState::VERIFYING; | 134 statusItem.state = StatusItemState::VERIFYING; |
90 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); | 135 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION); |
91 [model addItem:statusItem toSectionWithIdentifier:SectionIdentifierItems]; | 136 [model addItem:statusItem toSectionWithIdentifier:SectionIdentifierItems]; |
92 return; | 137 return; |
93 } | 138 } |
94 | 139 |
95 CollectionViewItem* headerItem = [self.dataSource headerItem]; | 140 if (!self.editing) { |
96 if (headerItem) { | 141 CollectionViewItem* headerItem = [self.dataSource headerItem]; |
97 headerItem.type = ItemTypeHeader; | 142 if (headerItem) { |
98 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems]; | 143 headerItem.type = ItemTypeHeader; |
| 144 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems]; |
| 145 } |
99 } | 146 } |
100 | 147 |
101 [[self.dataSource selectableItems] | 148 [[self.dataSource selectableItems] |
102 enumerateObjectsUsingBlock:^( | 149 enumerateObjectsUsingBlock:^( |
103 CollectionViewItem<PaymentsHasAccessoryType>* item, NSUInteger index, | 150 CollectionViewItem<PaymentsHasAccessoryType>* item, NSUInteger index, |
104 BOOL* stop) { | 151 BOOL* stop) { |
105 DCHECK([item respondsToSelector:@selector(accessoryType)]); | 152 DCHECK([item respondsToSelector:@selector(accessoryType)]); |
106 item.type = ItemTypeSelectableItem; | 153 item.type = ItemTypeSelectableItem; |
107 item.accessibilityTraits |= UIAccessibilityTraitButton; | 154 item.accessibilityTraits |= UIAccessibilityTraitButton; |
108 item.accessoryType = (index == self.dataSource.selectedItemIndex) | 155 if (self.editing) { |
109 ? MDCCollectionViewCellAccessoryCheckmark | 156 item.accessoryType = |
110 : MDCCollectionViewCellAccessoryNone; | 157 MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 158 } else { |
| 159 item.accessoryType = (index == self.dataSource.selectedItemIndex) |
| 160 ? MDCCollectionViewCellAccessoryCheckmark |
| 161 : MDCCollectionViewCellAccessoryNone; |
| 162 } |
111 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems]; | 163 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems]; |
112 }]; | 164 }]; |
113 | 165 |
114 CollectionViewItem* addButtonItem = [self.dataSource addButtonItem]; | 166 if (!self.editing) { |
115 if (addButtonItem) { | 167 CollectionViewItem* addButtonItem = [self.dataSource addButtonItem]; |
116 addButtonItem.type = ItemTypeAddItem; | 168 if (addButtonItem) { |
117 addButtonItem.accessibilityTraits |= UIAccessibilityTraitButton; | 169 addButtonItem.type = ItemTypeAddItem; |
118 [model addItem:addButtonItem | 170 addButtonItem.accessibilityTraits |= UIAccessibilityTraitButton; |
119 toSectionWithIdentifier:SectionIdentifierItems]; | 171 [model addItem:addButtonItem |
| 172 toSectionWithIdentifier:SectionIdentifierItems]; |
| 173 } |
120 } | 174 } |
121 } | 175 } |
122 | 176 |
123 - (void)viewDidLoad { | 177 - (void)viewDidLoad { |
124 [super viewDidLoad]; | 178 [super viewDidLoad]; |
125 self.collectionView.accessibilityIdentifier = | 179 self.collectionView.accessibilityIdentifier = |
126 kPaymentRequestSelectorCollectionViewAccessibilityID; | 180 kPaymentRequestSelectorCollectionViewAccessibilityID; |
127 | 181 |
128 // Customize collection view settings. | 182 // Customize collection view settings. |
129 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | 183 self.styler.cellStyle = MDCCollectionViewCellStyleCard; |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 | 236 |
183 #pragma mark UICollectionViewDelegate | 237 #pragma mark UICollectionViewDelegate |
184 | 238 |
185 - (void)collectionView:(UICollectionView*)collectionView | 239 - (void)collectionView:(UICollectionView*)collectionView |
186 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 240 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
187 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | 241 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
188 | 242 |
189 CollectionViewModel* model = self.collectionViewModel; | 243 CollectionViewModel* model = self.collectionViewModel; |
190 | 244 |
191 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | 245 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; |
| 246 |
| 247 if (self.editing) { |
| 248 DCHECK(item.type == ItemTypeSelectableItem); |
| 249 // Notify the delegate of the selection. |
| 250 NSUInteger index = |
| 251 [self.collectionViewModel indexInItemTypeForIndexPath:indexPath]; |
| 252 DCHECK(index < [[self.dataSource selectableItems] count]); |
| 253 if ([self.delegate |
| 254 respondsToSelector:@selector |
| 255 (paymentRequestSelectorViewController:didSelectItemAtIndexForEditing |
| 256 :)]) { |
| 257 [self.delegate paymentRequestSelectorViewController:self |
| 258 didSelectItemAtIndexForEditing:index]; |
| 259 } |
| 260 return; |
| 261 } |
| 262 |
192 switch (item.type) { | 263 switch (item.type) { |
193 case ItemTypeSelectableItem: { | 264 case ItemTypeSelectableItem: { |
194 // Update the currently selected cell, if any. | 265 // Update the currently selected cell, if any. |
195 if (self.dataSource.selectedItemIndex != NSUIntegerMax) { | 266 if (self.dataSource.selectedItemIndex != NSUIntegerMax) { |
196 DCHECK(self.dataSource.selectedItemIndex < | 267 DCHECK(self.dataSource.selectedItemIndex < |
197 [[self.dataSource selectableItems] count]); | 268 [[self.dataSource selectableItems] count]); |
198 CollectionViewItem<PaymentsHasAccessoryType>* oldSelectedItem = | 269 CollectionViewItem<PaymentsHasAccessoryType>* oldSelectedItem = |
199 [[self.dataSource selectableItems] | 270 [[self.dataSource selectableItems] |
200 objectAtIndex:self.dataSource.selectedItemIndex]; | 271 objectAtIndex:self.dataSource.selectedItemIndex]; |
201 oldSelectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | 272 oldSelectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
251 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | 322 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
252 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | 323 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
253 if (type == ItemTypeHeader) { | 324 if (type == ItemTypeHeader) { |
254 return YES; | 325 return YES; |
255 } else { | 326 } else { |
256 return NO; | 327 return NO; |
257 } | 328 } |
258 } | 329 } |
259 | 330 |
260 @end | 331 @end |
OLD | NEW |