Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(804)

Side by Side Diff: ios/chrome/browser/payments/payment_request_selector_view_controller.mm

Issue 2805273002: [Payment Request] Selector view controller (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
gambard 2017/04/10 09:47:35 Is this item type the same as the one in the data
Moe 2017/04/10 17:53:49 Correct. Nothing should be added between the heade
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;
gambard 2017/04/10 09:47:34 I think the coordinator should set the navigation
Moe 2017/04/10 17:53:49 All the view controllers in ios/chrome/browser/pay
gambard 2017/04/18 08:06:37 Acknowledged.
67 }
68 return self;
69 }
70
gambard 2017/04/10 09:47:34 Add #pragma mark - PaymentRequestSelectorViewContr
Moe 2017/04/10 17:53:49 Done.
71 - (void)onReturn {
72 [_delegate paymentRequestSelectorViewControllerDidReturn:self];
gambard 2017/04/10 09:47:35 s/_delegate/self.delegate in the whole file (excep
Moe 2017/04/10 17:53:49 Done.
73 }
74
75 #pragma mark - CollectionViewController methods
76
77 - (void)loadModel {
78 [super loadModel];
79 CollectionViewModel* model = self.collectionViewModel;
80
81 [model addSectionWithIdentifier:SectionIdentifierItems];
82
83 // If the view controller is in the pending state, only display a spinner and
84 // a message indicating the pending state.
85 if (_dataSource.state == PaymentRequestSelectorViewControllerStatePending) {
gambard 2017/04/10 09:47:35 s/_dataSource/self.dataSource in the whole file (e
Moe 2017/04/10 17:53:49 Done.
86 StatusItem* statusItem = [[StatusItem alloc] initWithType:ItemTypeSpinner];
87 statusItem.text = l10n_util::GetNSString(IDS_PAYMENTS_CHECKING_OPTION);
gambard 2017/04/10 09:47:35 Don't you need to set the status?
Moe 2017/04/10 17:53:49 Done.
88 [model addItem:statusItem toSectionWithIdentifier:SectionIdentifierItems];
89 return;
90 }
91
92 CollectionViewItem* headerItem = [_dataSource headerItem];
gambard 2017/04/10 09:47:35 I think it is better to pass the value to be displ
Moe 2017/04/10 17:53:49 I discussed this with lpromero@ previously and he
gambard 2017/04/18 08:06:37 I am writing the ContentSuggestions classes. My me
93 if (headerItem) {
94 [model addItem:headerItem toSectionWithIdentifier:SectionIdentifierItems];
gambard 2017/04/10 09:47:35 Use |setHeader:forSectionWithIdentifier:|?
Moe 2017/04/10 17:53:49 Unfortunately we don't have support for arbitrary
gambard 2017/04/18 08:06:37 Acknowledged.
95 }
96
97 NSArray<CollectionViewItem*>* selectableItems = [_dataSource selectableItems];
98 for (NSUInteger index = 0; index < selectableItems.count; ++index) {
99 CollectionViewItem* item = [selectableItems objectAtIndex:index];
gambard 2017/04/10 09:47:34 Nit: you can use: selectableItems[index]
Moe 2017/04/10 17:53:49 Done.
100 DCHECK(item);
gambard 2017/04/10 09:47:34 You cannot have nil items in an array. You should
Moe 2017/04/10 17:53:49 Done.
101 item.accessibilityTraits |= UIAccessibilityTraitButton;
102 item.accessoryType = (index == _dataSource.selectedItemIndex)
103 ? MDCCollectionViewCellAccessoryCheckmark
104 : MDCCollectionViewCellAccessoryNone;
105 [model addItem:item toSectionWithIdentifier:SectionIdentifierItems];
106 }
107
108 if ([[_dataSource addButtonTitle] length]) {
gambard 2017/04/10 09:47:35 Why are you returning items for all the others met
Moe 2017/04/10 17:53:49 Because the add button, if any, will be the same k
109 PaymentsTextItem* addButton =
110 [[PaymentsTextItem alloc] initWithType:ItemTypeAddItem];
111 addButton.text = [_dataSource addButtonTitle];
112 addButton.image = NativeImage(IDR_IOS_PAYMENTS_ADD);
113 addButton.accessibilityTraits |= UIAccessibilityTraitButton;
114 [model addItem:addButton toSectionWithIdentifier:SectionIdentifierItems];
115 }
116 }
117
118 - (void)viewDidLoad {
119 [super viewDidLoad];
120 self.collectionView.accessibilityIdentifier =
121 kPaymentRequestSelectorCollectionViewAccessibilityID;
122
123 // Customize collection view settings.
124 self.styler.cellStyle = MDCCollectionViewCellStyleCard;
125 self.styler.separatorInset =
126 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset);
127 }
128
129 #pragma mark UICollectionViewDataSource
130
131 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
gambard 2017/04/10 09:47:34 I don't think this is how the items should be used
Moe 2017/04/10 17:53:49 I agree. Done.
132 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath {
133 CollectionViewModel* model = self.collectionViewModel;
134
135 UICollectionViewCell* cell =
136 [super collectionView:collectionView cellForItemAtIndexPath:indexPath];
137
138 CollectionViewItem* item = [model itemAtIndexPath:indexPath];
139 switch (item.type) {
140 case ItemTypeSelectableItem: {
lpromero 2017/04/11 15:59:06 Move this right above the default case to bundle t
141 break;
142 }
143 case ItemTypeHeader: {
144 if ([cell isKindOfClass:[PaymentsTextCell class]]) {
gambard 2017/04/10 09:47:35 I think this is a discrepancy between the idea you
Moe 2017/04/10 17:53:49 I see your point of view. Again this was another p
lpromero 2017/04/11 15:59:06 It's the idea of an agnostic view controller that
Moe 2017/04/14 06:05:49 Brought this back as we decided the mediators shou
145 PaymentsTextCell* headerCell =
146 base::mac::ObjCCastStrict<PaymentsTextCell>(cell);
147 headerCell.textLabel.textColor =
148 _dataSource.state == PaymentRequestSelectorViewControllerStateError
gambard 2017/04/10 09:47:35 Does the data source state is supposed to change o
Moe 2017/04/10 17:53:49 because the state changes always involve going to/
gambard 2017/04/18 08:06:37 Acknowledged.
149 ? [[MDCPalette cr_redPalette] tint600]
150 : [[MDCPalette greyPalette] tint600];
151 }
152 break;
153 }
154 case ItemTypeAddItem: {
155 PaymentsTextCell* addItemCell =
156 base::mac::ObjCCastStrict<PaymentsTextCell>(cell);
157 addItemCell.textLabel.textColor = [[MDCPalette greyPalette] tint900];
158 break;
159 }
160 default:
161 break;
162 }
163 return cell;
164 }
165
166 #pragma mark UICollectionViewDelegate
167
168 - (void)collectionView:(UICollectionView*)collectionView
169 didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
170 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
171
172 CollectionViewModel* model = self.collectionViewModel;
173
174 CollectionViewItem* item = [model itemAtIndexPath:indexPath];
175 switch (item.type) {
176 case ItemTypeSelectableItem: {
177 // Update the currently selected cell, if any.
178 if (_dataSource.selectedItemIndex != NSUIntegerMax) {
179 CollectionViewItem* selectedItem =
180 [_dataSource selectableItemAtIndex:_dataSource.selectedItemIndex];
181 DCHECK(selectedItem);
gambard 2017/04/10 09:47:35 Same, I don't think you need this DCHECK.
Moe 2017/04/10 17:53:49 Done.
182 selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone;
183 [self reconfigureCellsForItems:@[ selectedItem ]
184 inSectionWithIdentifier:SectionIdentifierItems];
185 }
186
187 // Update the data source with the selection.
188 NSUInteger index =
189 [self.collectionViewModel indexInItemTypeForIndexPath:indexPath];
190 DCHECK(index < [[_dataSource selectableItems] count]);
191 [_dataSource setSelectedItemIndex:index];
gambard 2017/04/10 09:47:34 I don't think the ViewController should set things
Moe 2017/04/10 17:53:49 Good point. Done.
192
193 // Update the newly selected cell.
194 item.accessoryType = MDCCollectionViewCellAccessoryCheckmark;
195 [self reconfigureCellsForItems:@[ item ]
196 inSectionWithIdentifier:SectionIdentifierItems];
197
198 // Notify the delegate of the selection.
199 [_delegate paymentRequestSelectorViewController:self
200 didSelectItemAtIndex:index];
201 break;
202 }
203 case ItemTypeAddItem: {
204 [_delegate paymentRequestSelectorViewControllerDidSelectAddItem:self];
205 break;
206 }
207 default:
208 break;
209 }
210 }
211
212 #pragma mark MDCCollectionViewStylingDelegate
213
214 - (CGFloat)collectionView:(UICollectionView*)collectionView
215 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
216 CollectionViewItem* item =
217 [self.collectionViewModel itemAtIndexPath:indexPath];
218 switch (item.type) {
gambard 2017/04/10 09:47:35 Nit: I would remove the the switch. Is there a par
Moe 2017/04/10 17:53:48 Done.
219 case ItemTypeSpinner:
220 case ItemTypeHeader:
221 case ItemTypeSelectableItem:
222 case ItemTypeAddItem:
223 return [MDCCollectionViewCell
224 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
gambard 2017/04/10 09:47:35 I don't know if this is relevant in your case, but
Moe 2017/04/10 17:53:49 Definitely relevant. Thank you.
225 forItem:item];
226 default:
227 NOTREACHED();
228 return MDCCellDefaultOneLineHeight;
229 }
230 }
231
232 - (BOOL)collectionView:(UICollectionView*)collectionView
233 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath {
234 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath];
235 if (type == ItemTypeHeader) {
gambard 2017/04/10 09:47:34 I would have say the ink does not appear on header
Moe 2017/04/10 17:53:49 please see above.
236 return YES;
237 } else {
238 return NO;
239 }
240 }
241
242 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698