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.h" | |
15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_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 = kItemTypeHeaderItem, // This is a repeated item type. | |
41 ItemTypeSelectableItem, | |
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 (return) button. | |
61 UIBarButtonItem* returnButton = | |
62 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | |
63 target:nil | |
64 action:@selector(onReturn)]; | |
65 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | |
66 self.navigationItem.leftBarButtonItem = returnButton; | |
67 } | |
68 return self; | |
69 } | |
70 | |
71 #pragma mark - PaymentRequestSelectorViewControllerActions | |
72 | |
73 - (void)onReturn { | |
74 [self.delegate paymentRequestSelectorViewControllerDidReturn:self]; | |
75 } | |
76 | |
77 #pragma mark - CollectionViewController methods | |
78 | |
79 - (void)loadModel { | |
lpromero
2017/04/11 17:30:19
I would have seen the model being entirely created
Moe
2017/04/14 06:05:49
Acknowledged. I think we're now on the same page a
| |
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 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems]; | |
98 } | |
99 | |
100 NSArray<CollectionViewItem*>* selectableItems = | |
101 [self.dataSource selectableItems]; | |
102 for (NSUInteger index = 0; index < selectableItems.count; ++index) { | |
103 CollectionViewItem* item = selectableItems[index]; | |
104 item.accessibilityTraits |= UIAccessibilityTraitButton; | |
105 item.accessoryType = (index == self.dataSource.selectedItemIndex) | |
106 ? MDCCollectionViewCellAccessoryCheckmark | |
107 : MDCCollectionViewCellAccessoryNone; | |
108 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems]; | |
109 } | |
110 | |
111 if ([[self.dataSource addButtonTitle] length]) { | |
112 PaymentsTextItem* addButton = | |
113 [[PaymentsTextItem alloc] initWithType:ItemTypeAddItem]; | |
114 addButton.text = [self.dataSource addButtonTitle]; | |
115 addButton.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | |
116 addButton.accessibilityTraits |= UIAccessibilityTraitButton; | |
117 [model addItem:addButton toSectionWithIdentifier:SectionIdentifierItems]; | |
118 } | |
119 } | |
120 | |
121 - (void)viewDidLoad { | |
122 [super viewDidLoad]; | |
123 self.collectionView.accessibilityIdentifier = | |
124 kPaymentRequestSelectorCollectionViewAccessibilityID; | |
125 | |
126 // Customize collection view settings. | |
127 self.styler.cellStyle = MDCCollectionViewCellStyleCard; | |
128 self.styler.separatorInset = | |
129 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset); | |
130 } | |
131 | |
132 #pragma mark UICollectionViewDelegate | |
133 | |
134 - (void)collectionView:(UICollectionView*)collectionView | |
135 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
136 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; | |
137 | |
138 CollectionViewModel* model = self.collectionViewModel; | |
139 | |
140 CollectionViewItem* item = [model itemAtIndexPath:indexPath]; | |
141 switch (item.type) { | |
142 case ItemTypeSelectableItem: { | |
143 // Update the currently selected cell, if any. | |
144 if (self.dataSource.selectedItemIndex != NSUIntegerMax) { | |
145 CollectionViewItem* selectedItem = [self.dataSource | |
lpromero
2017/04/11 17:30:19
Are all your items PaymentsTextItem? If so, it sho
Moe
2017/04/14 06:05:49
Unfortunately not. This also can't remain blocked
| |
146 selectableItemAtIndex:self.dataSource.selectedItemIndex]; | |
147 selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone; | |
148 [self reconfigureCellsForItems:@[ selectedItem ] | |
149 inSectionWithIdentifier:SectionIdentifierItems]; | |
150 } | |
151 | |
152 // Update the newly selected cell. | |
153 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | |
154 [self reconfigureCellsForItems:@[ item ] | |
155 inSectionWithIdentifier:SectionIdentifierItems]; | |
156 | |
157 // Notify the delegate of the selection. | |
158 NSUInteger index = | |
159 [self.collectionViewModel indexInItemTypeForIndexPath:indexPath]; | |
160 DCHECK(index < [[self.dataSource selectableItems] count]); | |
161 [self.delegate paymentRequestSelectorViewController:self | |
162 didSelectItemAtIndex:index]; | |
163 break; | |
164 } | |
165 case ItemTypeAddItem: { | |
166 [self.delegate paymentRequestSelectorViewControllerDidSelectAddItem:self]; | |
167 break; | |
168 } | |
169 default: | |
170 break; | |
171 } | |
172 } | |
173 | |
174 #pragma mark MDCCollectionViewStylingDelegate | |
175 | |
176 - (CGFloat)collectionView:(UICollectionView*)collectionView | |
177 cellHeightAtIndexPath:(NSIndexPath*)indexPath { | |
178 CollectionViewItem* item = | |
179 [self.collectionViewModel itemAtIndexPath:indexPath]; | |
180 | |
181 UIEdgeInsets inset = [self collectionView:collectionView | |
182 layout:collectionView.collectionViewLayout | |
183 insetForSectionAtIndex:indexPath.section]; | |
184 | |
185 return [MDCCollectionViewCell | |
186 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) - | |
187 inset.left - inset.right | |
188 forItem:item]; | |
189 } | |
190 | |
191 - (BOOL)collectionView:(UICollectionView*)collectionView | |
192 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { | |
193 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; | |
194 if (type == ItemTypeHeader) { | |
195 return YES; | |
196 } else { | |
197 return NO; | |
198 } | |
199 } | |
200 | |
201 @end | |
OLD | NEW |