OLD | NEW |
(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 #include <vector> |
| 6 |
| 7 #import "ios/chrome/browser/payments/payment_method_selection_mediator.h" |
| 8 |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_data_util.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/credit_card.h" |
| 13 #include "components/autofill/core/browser/personal_data_manager.h" |
| 14 #include "components/strings/grit/components_strings.h" |
| 15 #import "ios/chrome/browser/payments/cells/payment_method_item.h" |
| 16 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
| 17 #include "ios/chrome/browser/payments/payment_request.h" |
| 18 #include "ios/chrome/browser/payments/payment_request_util.h" |
| 19 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
| 20 #include "ios/chrome/grit/ios_theme_resources.h" |
| 21 #include "ui/base/l10n/l10n_util.h" |
| 22 |
| 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 24 #error "This file requires ARC support." |
| 25 #endif |
| 26 |
| 27 namespace { |
| 28 using ::payment_request_util::GetBillingAddressLabelFromAutofillProfile; |
| 29 } // namespace |
| 30 |
| 31 @interface PaymentMethodSelectionMediator () |
| 32 |
| 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 |
| 35 // pointer and should outlive this class. |
| 36 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
| 37 |
| 38 // The selectable items to display in the collection. |
| 39 @property(nonatomic, strong) NSArray<PaymentMethodItem*>* items; |
| 40 |
| 41 @end |
| 42 |
| 43 @implementation PaymentMethodSelectionMediator |
| 44 |
| 45 @synthesize state = _state; |
| 46 @synthesize selectedItemIndex = _selectedItemIndex; |
| 47 @synthesize paymentRequest = _paymentRequest; |
| 48 @synthesize items = _items; |
| 49 |
| 50 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
| 51 self = [super init]; |
| 52 if (self) { |
| 53 _paymentRequest = paymentRequest; |
| 54 _selectedItemIndex = NSUIntegerMax; |
| 55 _items = [self createItems]; |
| 56 } |
| 57 return self; |
| 58 } |
| 59 |
| 60 #pragma mark - PaymentRequestSelectorViewControllerDataSource |
| 61 |
| 62 - (CollectionViewItem*)headerItem { |
| 63 return nil; |
| 64 } |
| 65 |
| 66 - (NSArray<CollectionViewItem*>*)selectableItems { |
| 67 return self.items; |
| 68 } |
| 69 |
| 70 - (CollectionViewItem*)selectableItemAtIndex:(NSUInteger)index { |
| 71 DCHECK(index < self.items.count); |
| 72 return [self.items objectAtIndex:index]; |
| 73 } |
| 74 |
| 75 - (CollectionViewItem*)addButtonItem { |
| 76 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; |
| 77 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_CARD); |
| 78 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); |
| 79 return addButtonItem; |
| 80 } |
| 81 |
| 82 #pragma mark - Helper methods |
| 83 |
| 84 - (NSArray<PaymentMethodItem*>*)createItems { |
| 85 const std::vector<autofill::CreditCard*>& paymentMethods = |
| 86 _paymentRequest->credit_cards(); |
| 87 NSMutableArray<PaymentMethodItem*>* items = |
| 88 [NSMutableArray arrayWithCapacity:paymentMethods.size()]; |
| 89 for (size_t index = 0; index < paymentMethods.size(); ++index) { |
| 90 autofill::CreditCard* paymentMethod = paymentMethods[index]; |
| 91 DCHECK(paymentMethod); |
| 92 PaymentMethodItem* item = [[PaymentMethodItem alloc] init]; |
| 93 item.methodID = |
| 94 base::SysUTF16ToNSString(paymentMethod->TypeAndLastFourDigits()); |
| 95 item.methodDetail = base::SysUTF16ToNSString( |
| 96 paymentMethod->GetRawInfo(autofill::CREDIT_CARD_NAME_FULL)); |
| 97 |
| 98 autofill::AutofillProfile* billingAddress = |
| 99 autofill::PersonalDataManager::GetProfileFromProfilesByGUID( |
| 100 paymentMethod->billing_address_id(), |
| 101 _paymentRequest->billing_profiles()); |
| 102 if (billingAddress) { |
| 103 item.methodAddress = |
| 104 GetBillingAddressLabelFromAutofillProfile(*billingAddress); |
| 105 } |
| 106 |
| 107 int methodTypeIconID = |
| 108 autofill::data_util::GetPaymentRequestData(paymentMethod->type()) |
| 109 .icon_resource_id; |
| 110 item.methodTypeIcon = NativeImage(methodTypeIconID); |
| 111 |
| 112 item.reserveRoomForAccessoryType = YES; |
| 113 if (_paymentRequest->selected_credit_card() == paymentMethod) |
| 114 _selectedItemIndex = index; |
| 115 |
| 116 [items addObject:item]; |
| 117 } |
| 118 return items; |
| 119 } |
| 120 |
| 121 @end |
OLD | NEW |