OLD | NEW |
(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 #import "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 #include "components/autofill/core/browser/credit_card.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/ui/collection_view/cells/collection_view_detail_item
.h" |
| 15 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_item.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 17 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 18 #import "ios/chrome/browser/ui/icons/chrome_icon.h" |
| 19 #include "ios/chrome/grit/ios_strings.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" |
| 22 |
| 23 NSString* const kPaymentMethodSelectionCollectionViewId = |
| 24 @"kPaymentMethodSelectionCollectionViewId"; |
| 25 |
| 26 namespace { |
| 27 |
| 28 const CGFloat kSeparatorEdgeInset = 14; |
| 29 |
| 30 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 31 SectionIdentifierPayment = kSectionIdentifierEnumZero, |
| 32 }; |
| 33 |
| 34 typedef NS_ENUM(NSInteger, ItemType) { |
| 35 ItemTypePaymentMethod = kItemTypeEnumZero, // This is a repeated item type. |
| 36 ItemTypeAddMethod, |
| 37 }; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 @interface PaymentMethodSelectionViewController () { |
| 42 base::WeakNSProtocol<id<PaymentMethodSelectionViewControllerDelegate>> |
| 43 _delegate; |
| 44 } |
| 45 |
| 46 // Called when the user presses the return button. |
| 47 - (void)onReturn; |
| 48 |
| 49 @end |
| 50 |
| 51 @implementation PaymentMethodSelectionViewController |
| 52 |
| 53 @synthesize selectedPaymentMethod = _selectedPaymentMethod; |
| 54 @synthesize paymentMethods = _paymentMethods; |
| 55 |
| 56 - (instancetype)init { |
| 57 if ((self = [super initWithStyle:CollectionViewControllerStyleAppBar])) { |
| 58 [self setTitle:l10n_util::GetNSString( |
| 59 IDS_IOS_PAYMENT_REQUEST_METHOD_SELECTION_TITLE)]; |
| 60 |
| 61 UIBarButtonItem* returnButton = |
| 62 [ChromeIcon templateBarButtonItemWithImage:[ChromeIcon backIcon] |
| 63 target:nil |
| 64 action:@selector(onReturn)]; |
| 65 [self navigationItem].leftBarButtonItem = returnButton; |
| 66 } |
| 67 return self; |
| 68 } |
| 69 |
| 70 - (id<PaymentMethodSelectionViewControllerDelegate>)delegate { |
| 71 return _delegate.get(); |
| 72 } |
| 73 |
| 74 - (void)setDelegate:(id<PaymentMethodSelectionViewControllerDelegate>)delegate { |
| 75 _delegate.reset(delegate); |
| 76 } |
| 77 |
| 78 - (void)onReturn { |
| 79 [_delegate paymentMethodSelectionViewControllerDidReturn:self]; |
| 80 } |
| 81 |
| 82 #pragma mark - CollectionViewController methods |
| 83 |
| 84 - (void)loadModel { |
| 85 [super loadModel]; |
| 86 CollectionViewModel* model = self.collectionViewModel; |
| 87 |
| 88 [model addSectionWithIdentifier:SectionIdentifierPayment]; |
| 89 |
| 90 for (size_t i = 0; i < _paymentMethods.size(); ++i) { |
| 91 autofill::CreditCard* paymentMethod = _paymentMethods[i]; |
| 92 PaymentMethodItem* paymentMethodItem = [[[PaymentMethodItem alloc] |
| 93 initWithType:ItemTypePaymentMethod] autorelease]; |
| 94 paymentMethodItem.methodID = |
| 95 base::SysUTF16ToNSString(paymentMethod->TypeAndLastFourDigits()); |
| 96 paymentMethodItem.methodDetail = base::SysUTF16ToNSString( |
| 97 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); |
| 98 |
| 99 int methodTypeIconID = |
| 100 autofill::data_util::GetPaymentRequestData(paymentMethod->type()) |
| 101 .icon_resource_id; |
| 102 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 103 paymentMethodItem.methodTypeIcon = |
| 104 rb.GetNativeImageNamed(methodTypeIconID).ToUIImage(); |
| 105 |
| 106 if (paymentMethod == _selectedPaymentMethod) |
| 107 paymentMethodItem.accessoryType = MDCCollectionViewCellAccessoryCheckmark; |
| 108 [model addItem:paymentMethodItem |
| 109 toSectionWithIdentifier:SectionIdentifierPayment]; |
| 110 } |
| 111 |
| 112 CollectionViewTextItem* addPaymentMethod = [[[CollectionViewTextItem alloc] |
| 113 initWithType:ItemTypeAddMethod] autorelease]; |
| 114 addPaymentMethod.text = |
| 115 l10n_util::GetNSString(IDS_IOS_PAYMENT_REQUEST_ADD_METHOD_BUTTON); |
| 116 addPaymentMethod.accessibilityTraits |= UIAccessibilityTraitButton; |
| 117 [model addItem:addPaymentMethod |
| 118 toSectionWithIdentifier:SectionIdentifierPayment]; |
| 119 } |
| 120 |
| 121 - (void)viewDidLoad { |
| 122 [super viewDidLoad]; |
| 123 self.collectionView.accessibilityIdentifier = |
| 124 kPaymentMethodSelectionCollectionViewId; |
| 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 NSInteger itemType = |
| 139 [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 140 if (itemType == ItemTypePaymentMethod) { |
| 141 DCHECK(indexPath.item < (NSInteger)_paymentMethods.size()); |
| 142 [_delegate |
| 143 paymentMethodSelectionViewController:self |
| 144 selectedPaymentMethod:_paymentMethods[indexPath.item]]; |
| 145 } |
| 146 // TODO(crbug.com/602666): Present a credit card addition UI when |
| 147 // itemType == ItemAddMethod. |
| 148 } |
| 149 |
| 150 #pragma mark MDCCollectionViewStylingDelegate |
| 151 |
| 152 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 153 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 154 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 155 if (type == ItemTypeAddMethod) |
| 156 return MDCCellDefaultOneLineHeight; |
| 157 else |
| 158 return MDCCellDefaultTwoLineHeight; |
| 159 } |
| 160 |
| 161 @end |
OLD | NEW |