| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 #include <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #import "ios/chrome/browser/ui/payments/payment_method_selection_mediator.h" | 7 #import "ios/chrome/browser/ui/payments/payment_method_selection_mediator.h" |
| 8 | 8 |
| 9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" | 10 #include "components/autofill/core/browser/autofill_data_util.h" |
| (...skipping 18 matching lines...) Expand all Loading... |
| 29 } // namespace | 29 } // namespace |
| 30 | 30 |
| 31 @interface PaymentMethodSelectionMediator () | 31 @interface PaymentMethodSelectionMediator () |
| 32 | 32 |
| 33 // The PaymentRequest object owning an instance of web::PaymentRequest as | 33 // The PaymentRequest object owning an instance of web::PaymentRequest as |
| 34 // provided by the page invoking the Payment Request API. This is a weak | 34 // provided by the page invoking the Payment Request API. This is a weak |
| 35 // pointer and should outlive this class. | 35 // pointer and should outlive this class. |
| 36 @property(nonatomic, assign) PaymentRequest* paymentRequest; | 36 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
| 37 | 37 |
| 38 // The selectable items to display in the collection. | 38 // The selectable items to display in the collection. |
| 39 @property(nonatomic, strong) NSArray<PaymentMethodItem*>* items; | 39 @property(nonatomic, strong) NSMutableArray<PaymentMethodItem*>* items; |
| 40 | 40 |
| 41 @end | 41 @end |
| 42 | 42 |
| 43 @implementation PaymentMethodSelectionMediator | 43 @implementation PaymentMethodSelectionMediator |
| 44 | 44 |
| 45 @synthesize state = _state; | 45 @synthesize state = _state; |
| 46 @synthesize selectedItemIndex = _selectedItemIndex; | 46 @synthesize selectedItemIndex = _selectedItemIndex; |
| 47 @synthesize paymentRequest = _paymentRequest; | 47 @synthesize paymentRequest = _paymentRequest; |
| 48 @synthesize items = _items; | 48 @synthesize items = _items; |
| 49 | 49 |
| 50 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | 50 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
| 51 self = [super init]; | 51 self = [super init]; |
| 52 if (self) { | 52 if (self) { |
| 53 _paymentRequest = paymentRequest; | 53 _paymentRequest = paymentRequest; |
| 54 _selectedItemIndex = NSUIntegerMax; | 54 _selectedItemIndex = NSUIntegerMax; |
| 55 _items = [self createItems]; | 55 [self loadItems]; |
| 56 } | 56 } |
| 57 return self; | 57 return self; |
| 58 } | 58 } |
| 59 | 59 |
| 60 #pragma mark - PaymentRequestSelectorViewControllerDataSource | 60 #pragma mark - PaymentRequestSelectorViewControllerDataSource |
| 61 | 61 |
| 62 - (BOOL)allowsEditMode { |
| 63 return YES; |
| 64 } |
| 65 |
| 62 - (CollectionViewItem*)headerItem { | 66 - (CollectionViewItem*)headerItem { |
| 63 return nil; | 67 return nil; |
| 64 } | 68 } |
| 65 | 69 |
| 66 - (NSArray<CollectionViewItem*>*)selectableItems { | 70 - (NSArray<CollectionViewItem*>*)selectableItems { |
| 67 return self.items; | 71 return self.items; |
| 68 } | 72 } |
| 69 | 73 |
| 70 - (CollectionViewItem*)addButtonItem { | 74 - (CollectionViewItem*)addButtonItem { |
| 71 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; | 75 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; |
| 72 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_CARD); | 76 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_CARD); |
| 73 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | 77 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); |
| 74 return addButtonItem; | 78 return addButtonItem; |
| 75 } | 79 } |
| 76 | 80 |
| 77 #pragma mark - Helper methods | 81 #pragma mark - Public methods |
| 78 | 82 |
| 79 - (NSArray<PaymentMethodItem*>*)createItems { | 83 - (void)loadItems { |
| 80 const std::vector<autofill::CreditCard*>& paymentMethods = | 84 const std::vector<autofill::CreditCard*>& paymentMethods = |
| 81 _paymentRequest->credit_cards(); | 85 _paymentRequest->credit_cards(); |
| 82 NSMutableArray<PaymentMethodItem*>* items = | 86 _items = [NSMutableArray arrayWithCapacity:paymentMethods.size()]; |
| 83 [NSMutableArray arrayWithCapacity:paymentMethods.size()]; | |
| 84 for (size_t index = 0; index < paymentMethods.size(); ++index) { | 87 for (size_t index = 0; index < paymentMethods.size(); ++index) { |
| 85 autofill::CreditCard* paymentMethod = paymentMethods[index]; | 88 autofill::CreditCard* paymentMethod = paymentMethods[index]; |
| 86 DCHECK(paymentMethod); | 89 DCHECK(paymentMethod); |
| 87 PaymentMethodItem* item = [[PaymentMethodItem alloc] init]; | 90 PaymentMethodItem* item = [[PaymentMethodItem alloc] init]; |
| 88 item.methodID = | 91 item.methodID = |
| 89 base::SysUTF16ToNSString(paymentMethod->NetworkAndLastFourDigits()); | 92 base::SysUTF16ToNSString(paymentMethod->NetworkAndLastFourDigits()); |
| 90 item.methodDetail = base::SysUTF16ToNSString( | 93 item.methodDetail = base::SysUTF16ToNSString( |
| 91 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); | 94 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); |
| 92 | 95 |
| 93 autofill::AutofillProfile* billingAddress = | 96 autofill::AutofillProfile* billingAddress = |
| 94 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( | 97 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( |
| 95 paymentMethod->billing_address_id(), | 98 paymentMethod->billing_address_id(), |
| 96 _paymentRequest->billing_profiles()); | 99 _paymentRequest->billing_profiles()); |
| 97 if (billingAddress) { | 100 if (billingAddress) { |
| 98 item.methodAddress = | 101 item.methodAddress = |
| 99 GetBillingAddressLabelFromAutofillProfile(*billingAddress); | 102 GetBillingAddressLabelFromAutofillProfile(*billingAddress); |
| 100 } | 103 } |
| 101 | 104 |
| 102 int methodTypeIconID = | 105 int methodTypeIconID = |
| 103 autofill::data_util::GetPaymentRequestData(paymentMethod->network()) | 106 autofill::data_util::GetPaymentRequestData(paymentMethod->network()) |
| 104 .icon_resource_id; | 107 .icon_resource_id; |
| 105 item.methodTypeIcon = NativeImage(methodTypeIconID); | 108 item.methodTypeIcon = NativeImage(methodTypeIconID); |
| 106 | 109 |
| 107 item.reserveRoomForAccessoryType = YES; | 110 item.reserveRoomForAccessoryType = YES; |
| 108 if (_paymentRequest->selected_credit_card() == paymentMethod) | 111 if (_paymentRequest->selected_credit_card() == paymentMethod) |
| 109 _selectedItemIndex = index; | 112 _selectedItemIndex = index; |
| 110 | 113 |
| 111 [items addObject:item]; | 114 [_items addObject:item]; |
| 112 } | 115 } |
| 113 return items; | |
| 114 } | 116 } |
| 115 | 117 |
| 116 @end | 118 @end |
| OLD | NEW |