OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/payments/payment_method_selection_view_controller.h" | 5 #import "ios/chrome/browser/payments/payment_method_selection_view_controller.h" |
6 | 6 |
7 #import "base/ios/weak_nsobject.h" | |
8 #include "base/mac/foundation_util.h" | 7 #include "base/mac/foundation_util.h" |
9 #include "base/mac/scoped_nsobject.h" | |
10 #include "base/strings/sys_string_conversions.h" | 8 #include "base/strings/sys_string_conversions.h" |
11 #include "components/autofill/core/browser/autofill_data_util.h" | 9 #include "components/autofill/core/browser/autofill_data_util.h" |
12 #include "components/autofill/core/browser/credit_card.h" | 10 #include "components/autofill/core/browser/credit_card.h" |
13 #include "components/strings/grit/components_strings.h" | 11 #include "components/strings/grit/components_strings.h" |
14 #import "ios/chrome/browser/payments/cells/payment_method_item.h" | 12 #import "ios/chrome/browser/payments/cells/payment_method_item.h" |
15 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | 13 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
16 #include "ios/chrome/browser/payments/payment_request.h" | 14 #include "ios/chrome/browser/payments/payment_request.h" |
17 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" | 15 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" | 16 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_detail_item
.h" |
19 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" | 17 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
20 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" | 18 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
21 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" | 19 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
22 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" | 20 #import "ios/chrome/browser/ui/colors/MDCPalette+CrAdditions.h" |
23 #import "ios/chrome/browser/ui/icons/chrome_icon.h" | 21 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
24 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 22 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
25 #include "ios/chrome/grit/ios_strings.h" | 23 #include "ios/chrome/grit/ios_strings.h" |
26 #include "ios/chrome/grit/ios_theme_resources.h" | 24 #include "ios/chrome/grit/ios_theme_resources.h" |
27 #include "ui/base/l10n/l10n_util.h" | 25 #include "ui/base/l10n/l10n_util.h" |
28 #include "ui/base/resource/resource_bundle.h" | 26 #include "ui/base/resource/resource_bundle.h" |
29 | 27 |
| 28 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 29 #error "This file requires ARC support." |
| 30 #endif |
| 31 |
30 NSString* const kPaymentMethodSelectionCollectionViewId = | 32 NSString* const kPaymentMethodSelectionCollectionViewId = |
31 @"kPaymentMethodSelectionCollectionViewId"; | 33 @"kPaymentMethodSelectionCollectionViewId"; |
32 | 34 |
33 namespace { | 35 namespace { |
34 | 36 |
35 const CGFloat kSeparatorEdgeInset = 14; | 37 const CGFloat kSeparatorEdgeInset = 14; |
36 | 38 |
37 typedef NS_ENUM(NSInteger, SectionIdentifier) { | 39 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
38 SectionIdentifierPayment = kSectionIdentifierEnumZero, | 40 SectionIdentifierPayment = kSectionIdentifierEnumZero, |
39 }; | 41 }; |
40 | 42 |
41 typedef NS_ENUM(NSInteger, ItemType) { | 43 typedef NS_ENUM(NSInteger, ItemType) { |
42 ItemTypePaymentMethod = kItemTypeEnumZero, // This is a repeated item type. | 44 ItemTypePaymentMethod = kItemTypeEnumZero, // This is a repeated item type. |
43 ItemTypeAddMethod, | 45 ItemTypeAddMethod, |
44 }; | 46 }; |
45 | 47 |
46 } // namespace | 48 } // namespace |
47 | 49 |
48 @interface PaymentMethodSelectionViewController () { | 50 @interface PaymentMethodSelectionViewController () { |
49 base::WeakNSProtocol<id<PaymentMethodSelectionViewControllerDelegate>> | |
50 _delegate; | |
51 | |
52 // The PaymentRequest object owning an instance of web::PaymentRequest as | 51 // The PaymentRequest object owning an instance of web::PaymentRequest as |
53 // provided by the page invoking the Payment Request API. This is a weak | 52 // provided by the page invoking the Payment Request API. This is a weak |
54 // pointer and should outlive this class. | 53 // pointer and should outlive this class. |
55 PaymentRequest* _paymentRequest; | 54 PaymentRequest* _paymentRequest; |
56 | 55 |
57 // The currently selected item. May be nil. | 56 // The currently selected item. May be nil. |
58 PaymentMethodItem* _selectedItem; | 57 __weak PaymentMethodItem* _selectedItem; |
59 } | 58 } |
60 | 59 |
61 // Called when the user presses the return button. | 60 // Called when the user presses the return button. |
62 - (void)onReturn; | 61 - (void)onReturn; |
63 | 62 |
64 @end | 63 @end |
65 | 64 |
66 @implementation PaymentMethodSelectionViewController | 65 @implementation PaymentMethodSelectionViewController |
| 66 @synthesize delegate = _delegate; |
67 | 67 |
68 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | 68 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
69 DCHECK(paymentRequest); | 69 DCHECK(paymentRequest); |
70 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { | 70 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
71 [self setTitle:l10n_util::GetNSString( | 71 [self setTitle:l10n_util::GetNSString( |
72 IDS_IOS_PAYMENT_REQUEST_METHOD_SELECTION_TITLE)]; | 72 IDS_IOS_PAYMENT_REQUEST_METHOD_SELECTION_TITLE)]; |
73 | 73 |
74 UIBarButtonItem* returnButton = | 74 UIBarButtonItem* returnButton = |
75 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] | 75 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
76 target:nil | 76 target:nil |
77 action:@selector(onReturn)]; | 77 action:@selector(onReturn)]; |
78 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); | 78 returnButton.accessibilityLabel = l10n_util::GetNSString(IDS_ACCNAME_BACK); |
79 [self navigationItem].leftBarButtonItem = returnButton; | 79 [self navigationItem].leftBarButtonItem = returnButton; |
80 | 80 |
81 _paymentRequest = paymentRequest; | 81 _paymentRequest = paymentRequest; |
82 } | 82 } |
83 return self; | 83 return self; |
84 } | 84 } |
85 | 85 |
86 - (id<PaymentMethodSelectionViewControllerDelegate>)delegate { | |
87 return _delegate.get(); | |
88 } | |
89 | |
90 - (void)setDelegate:(id<PaymentMethodSelectionViewControllerDelegate>)delegate { | |
91 _delegate.reset(delegate); | |
92 } | |
93 | |
94 - (void)onReturn { | 86 - (void)onReturn { |
95 [_delegate paymentMethodSelectionViewControllerDidReturn:self]; | 87 [_delegate paymentMethodSelectionViewControllerDidReturn:self]; |
96 } | 88 } |
97 | 89 |
98 #pragma mark - CollectionViewController methods | 90 #pragma mark - CollectionViewController methods |
99 | 91 |
100 - (void)loadModel { | 92 - (void)loadModel { |
101 [super loadModel]; | 93 [super loadModel]; |
102 CollectionViewModel* model = self.collectionViewModel; | 94 CollectionViewModel* model = self.collectionViewModel; |
103 _selectedItem = nil; | 95 _selectedItem = nil; |
104 | 96 |
105 [model addSectionWithIdentifier:SectionIdentifierPayment]; | 97 [model addSectionWithIdentifier:SectionIdentifierPayment]; |
106 | 98 |
107 for (const auto& paymentMethod : _paymentRequest->credit_cards()) { | 99 for (const auto& paymentMethod : _paymentRequest->credit_cards()) { |
108 PaymentMethodItem* paymentMethodItem = [[[PaymentMethodItem alloc] | 100 PaymentMethodItem* paymentMethodItem = |
109 initWithType:ItemTypePaymentMethod] autorelease]; | 101 [[PaymentMethodItem alloc] initWithType:ItemTypePaymentMethod]; |
110 paymentMethodItem.accessibilityTraits |= UIAccessibilityTraitButton; | 102 paymentMethodItem.accessibilityTraits |= UIAccessibilityTraitButton; |
111 paymentMethodItem.methodID = | 103 paymentMethodItem.methodID = |
112 base::SysUTF16ToNSString(paymentMethod->TypeAndLastFourDigits()); | 104 base::SysUTF16ToNSString(paymentMethod->TypeAndLastFourDigits()); |
113 paymentMethodItem.methodDetail = base::SysUTF16ToNSString( | 105 paymentMethodItem.methodDetail = base::SysUTF16ToNSString( |
114 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); | 106 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); |
115 int methodTypeIconID = | 107 int methodTypeIconID = |
116 autofill::data_util::GetPaymentRequestData(paymentMethod->type()) | 108 autofill::data_util::GetPaymentRequestData(paymentMethod->type()) |
117 .icon_resource_id; | 109 .icon_resource_id; |
118 paymentMethodItem.methodTypeIcon = NativeImage(methodTypeIconID); | 110 paymentMethodItem.methodTypeIcon = NativeImage(methodTypeIconID); |
119 | 111 |
120 if (_paymentRequest->selected_credit_card() == paymentMethod) { | 112 if (_paymentRequest->selected_credit_card() == paymentMethod) { |
121 paymentMethodItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; | 113 paymentMethodItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
122 _selectedItem = paymentMethodItem; | 114 _selectedItem = paymentMethodItem; |
123 } | 115 } |
124 [model addItem:paymentMethodItem | 116 [model addItem:paymentMethodItem |
125 toSectionWithIdentifier:SectionIdentifierPayment]; | 117 toSectionWithIdentifier:SectionIdentifierPayment]; |
126 } | 118 } |
127 | 119 |
128 PaymentsTextItem* addPaymentMethod = | 120 PaymentsTextItem* addPaymentMethod = |
129 [[[PaymentsTextItem alloc] initWithType:ItemTypeAddMethod] autorelease]; | 121 [[PaymentsTextItem alloc] initWithType:ItemTypeAddMethod]; |
130 addPaymentMethod.text = | 122 addPaymentMethod.text = |
131 l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_ADD_METHOD_BUTTON); | 123 l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_ADD_METHOD_BUTTON); |
132 addPaymentMethod.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | 124 addPaymentMethod.image = NativeImage(IDR_IOS_PAYMENTS_ADD); |
133 addPaymentMethod.accessibilityTraits |= UIAccessibilityTraitButton; | 125 addPaymentMethod.accessibilityTraits |= UIAccessibilityTraitButton; |
134 [model addItem:addPaymentMethod | 126 [model addItem:addPaymentMethod |
135 toSectionWithIdentifier:SectionIdentifierPayment]; | 127 toSectionWithIdentifier:SectionIdentifierPayment]; |
136 } | 128 } |
137 | 129 |
138 - (void)viewDidLoad { | 130 - (void)viewDidLoad { |
139 [super viewDidLoad]; | 131 [super viewDidLoad]; |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 return [MDCCollectionViewCell | 190 return [MDCCollectionViewCell |
199 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) | 191 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
200 forItem:item]; | 192 forItem:item]; |
201 default: | 193 default: |
202 NOTREACHED(); | 194 NOTREACHED(); |
203 return MDCCellDefaultOneLineHeight; | 195 return MDCCellDefaultOneLineHeight; |
204 } | 196 } |
205 } | 197 } |
206 | 198 |
207 @end | 199 @end |
OLD | NEW |