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

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

Issue 2826713002: [Payment Request] Refactors the remaining selector view controllers (Closed)
Patch Set: Addressed comment 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 2016 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_method_selection_view_controller.h"
6
7 #include "base/mac/foundation_util.h"
8 #include "base/strings/sys_string_conversions.h"
9 #include "components/autofill/core/browser/autofill_data_util.h"
10 #include "components/autofill/core/browser/credit_card.h"
11 #include "components/autofill/core/browser/personal_data_manager.h"
12 #include "components/strings/grit/components_strings.h"
13 #import "ios/chrome/browser/payments/cells/payment_method_item.h"
14 #import "ios/chrome/browser/payments/cells/payments_text_item.h"
15 #import "ios/chrome/browser/payments/payment_method_selection_view_controller_ac tions.h"
16 #include "ios/chrome/browser/payments/payment_request.h"
17 #include "ios/chrome/browser/payments/payment_request_util.h"
18 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom e.h"
19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item .h"
20 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h"
21 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h"
22 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h"
23 #import "ios/chrome/browser/ui/icons/chrome_icon.h"
24 #include "ios/chrome/browser/ui/uikit_ui_util.h"
25 #include "ios/chrome/grit/ios_strings.h"
26 #include "ios/chrome/grit/ios_theme_resources.h"
27 #include "ios/web/public/payments/payment_request.h"
28 #include "ui/base/l10n/l10n_util.h"
29 #include "ui/base/resource/resource_bundle.h"
30
31 #if !defined(__has_feature) || !__has_feature(objc_arc)
32 #error "This file requires ARC support."
33 #endif
34
35 NSString* const kPaymentMethodSelectionCollectionViewID =
36 @"kPaymentMethodSelectionCollectionViewID";
37
38 namespace {
39 using ::payment_request_util::GetBillingAddressLabelFromAutofillProfile;
40
41 const CGFloat kSeparatorEdgeInset = 14;
42
43 typedef NS_ENUM(NSInteger, SectionIdentifier) {
44 SectionIdentifierPayment = kSectionIdentifierEnumZero,
45 };
46
47 typedef NS_ENUM(NSInteger, ItemType) {
48 ItemTypePaymentMethod = kItemTypeEnumZero, // This is a repeated item type.
49 ItemTypeAddMethod,
50 };
51
52 } // namespace
53
54 @interface PaymentMethodSelectionViewController ()<
55 PaymentMethodSelectionViewControllerActions> {
56 // The PaymentRequest object having a copy of web::PaymentRequest as provided
57 // by the page invoking the Payment Request API. This is a weak pointer and
58 // should outlive this class.
59 PaymentRequest* _paymentRequest;
60
61 // The currently selected item. May be nil.
62 __weak PaymentMethodItem* _selectedItem;
63 }
64
65 @end
66
67 @implementation PaymentMethodSelectionViewController
68 @synthesize delegate = _delegate;
69
70 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest {
71 DCHECK(paymentRequest);
72 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) {
73 [self
74 setTitle:l10n_util::GetNSString(IDS_PAYMENTS_METHOD_OF_PAYMENT_LABEL)];
75
76 // Set up leading (return) button.
77 UIBarButtonItem* returnButton =
78 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon]
79 target:nil
80 action:@selector(onReturn)];
81 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK);
82 [self navigationItem].leftBarButtonItem = returnButton;
83
84 _paymentRequest = paymentRequest;
85 }
86 return self;
87 }
88
89 - (void)onReturn {
90 [_delegate paymentMethodSelectionViewControllerDidReturn:self];
91 }
92
93 #pragma mark - CollectionViewController methods
94
95 - (void)loadModel {
96 [super loadModel];
97 CollectionViewModel* model = self.collectionViewModel;
98 _selectedItem = nil;
99
100 [model addSectionWithIdentifier:SectionIdentifierPayment];
101
102 for (const auto* paymentMethod : _paymentRequest->credit_cards()) {
103 PaymentMethodItem* paymentMethodItem =
104 [[PaymentMethodItem alloc] initWithType:ItemTypePaymentMethod];
105
106 paymentMethodItem.accessibilityTraits |= UIAccessibilityTraitButton;
107 paymentMethodItem.methodID =
108 base::SysUTF16ToNSString(paymentMethod->TypeAndLastFourDigits());
109 paymentMethodItem.methodDetail = base::SysUTF16ToNSString(
110 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL));
111
112 autofill::AutofillProfile* billingAddress =
113 autofill::PersonalDataManager::GetProfileFromProfilesByGUID(
114 paymentMethod->billing_address_id(),
115 _paymentRequest->billing_profiles());
116 if (billingAddress) {
117 paymentMethodItem.methodAddress =
118 GetBillingAddressLabelFromAutofillProfile(*billingAddress);
119 }
120
121 int methodTypeIconID =
122 autofill::data_util::GetPaymentRequestData(paymentMethod->type())
123 .icon_resource_id;
124 paymentMethodItem.methodTypeIcon = NativeImage(methodTypeIconID);
125
126 paymentMethodItem.reserveRoomForAccessoryType = YES;
127
128 if (_paymentRequest->selected_credit_card() == paymentMethod) {
129 paymentMethodItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark;
130 _selectedItem = paymentMethodItem;
131 }
132 [model addItem:paymentMethodItem
133 toSectionWithIdentifier:SectionIdentifierPayment];
134 }
135
136 PaymentsTextItem* addPaymentMethod =
137 [[PaymentsTextItem alloc] initWithType:ItemTypeAddMethod];
138 addPaymentMethod.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_CARD);
139 addPaymentMethod.image = NativeImage(IDR_IOS_PAYMENTS_ADD);
140 addPaymentMethod.accessibilityTraits |= UIAccessibilityTraitButton;
141 [model addItem:addPaymentMethod
142 toSectionWithIdentifier:SectionIdentifierPayment];
143 }
144
145 - (void)viewDidLoad {
146 [super viewDidLoad];
147 self.collectionView.accessibilityIdentifier =
148 kPaymentMethodSelectionCollectionViewID;
149
150 // Customize collection view settings.
151 self.styler.cellStyle = MDCCollectionViewCellStyleCard;
152 self.styler.separatorInset =
153 UIEdgeInsetsMake(0, kSeparatorEdgeInset, 0, kSeparatorEdgeInset);
154 }
155
156 #pragma mark UICollectionViewDelegate
157
158 - (void)collectionView:(UICollectionView*)collectionView
159 didSelectItemAtIndexPath:(NSIndexPath*)indexPath {
160 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath];
161
162 CollectionViewModel* model = self.collectionViewModel;
163
164 CollectionViewItem* item = [model itemAtIndexPath:indexPath];
165 if (item.type == ItemTypePaymentMethod) {
166 // Update the currently selected cell, if any.
167 if (_selectedItem) {
168 _selectedItem.accessoryType = MDCCollectionViewCellAccessoryNone;
169 [self reconfigureCellsForItems:@[ _selectedItem ]
170 inSectionWithIdentifier:SectionIdentifierPayment];
171 }
172
173 // Update the newly selected cell.
174 PaymentMethodItem* newlySelectedItem =
175 base::mac::ObjCCastStrict<PaymentMethodItem>(item);
176 newlySelectedItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark;
177 [self reconfigureCellsForItems:@[ newlySelectedItem ]
178 inSectionWithIdentifier:SectionIdentifierPayment];
179
180 // Update the reference to the selected item.
181 _selectedItem = newlySelectedItem;
182
183 // Notify the delegate of the selection.
184 NSInteger index = [model indexInItemTypeForIndexPath:indexPath];
185 DCHECK(index < (NSInteger)_paymentRequest->credit_cards().size());
186 [_delegate
187 paymentMethodSelectionViewController:self
188 didSelectPaymentMethod:_paymentRequest
189 ->credit_cards()[index]];
190 } else if (item.type == ItemTypeAddMethod) {
191 [_delegate paymentMethodSelectionViewControllerDidSelectAddCard:self];
192 }
193 }
194
195 #pragma mark MDCCollectionViewStylingDelegate
196
197 - (CGFloat)collectionView:(UICollectionView*)collectionView
198 cellHeightAtIndexPath:(NSIndexPath*)indexPath {
199 CollectionViewItem* item =
200 [self.collectionViewModel itemAtIndexPath:indexPath];
201 switch (item.type) {
202 case ItemTypePaymentMethod:
203 case ItemTypeAddMethod:
204 return [MDCCollectionViewCell
205 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds)
206 forItem:item];
207 default:
208 NOTREACHED();
209 return MDCCellDefaultOneLineHeight;
210 }
211 }
212
213 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698