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/payments/shipping_address_selection_mediator.h" | 7 #import "ios/chrome/browser/payments/shipping_option_selection_mediator.h" |
8 | 8 |
9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "components/strings/grit/components_strings.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #import "ios/chrome/browser/payments/cells/autofill_profile_item.h" | 11 #include "components/payments/core/currency_formatter.h" |
12 #import "ios/chrome/browser/payments/cells/payments_text_item.h" | 12 #import "ios/chrome/browser/payments/cells/payments_text_item.h" |
13 #include "ios/chrome/browser/payments/payment_request.h" | 13 #include "ios/chrome/browser/payments/payment_request.h" |
14 #import "ios/chrome/browser/payments/payment_request_util.h" | |
15 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 14 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
16 #include "ios/chrome/grit/ios_theme_resources.h" | 15 #include "ios/chrome/grit/ios_theme_resources.h" |
17 #include "ui/base/l10n/l10n_util.h" | 16 #include "ios/web/public/payments/payment_request.h" |
18 | 17 |
19 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
20 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
21 #endif | 20 #endif |
22 | 21 |
23 namespace { | 22 @interface ShippingOptionSelectionMediator () |
24 using ::payment_request_util::GetNameLabelFromAutofillProfile; | |
25 using ::payment_request_util::GetShippingAddressLabelFromAutofillProfile; | |
26 using ::payment_request_util::GetPhoneNumberLabelFromAutofillProfile; | |
27 } // namespace | |
28 | |
29 @interface ShippingAddressSelectionMediator () | |
30 | 23 |
31 // The PaymentRequest object owning an instance of web::PaymentRequest as | 24 // The PaymentRequest object owning an instance of web::PaymentRequest as |
32 // provided by the page invoking the Payment Request API. This is a weak | 25 // provided by the page invoking the Payment Request API. This is a weak |
33 // pointer and should outlive this class. | 26 // pointer and should outlive this class. |
34 @property(nonatomic, assign) PaymentRequest* paymentRequest; | 27 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
35 | 28 |
36 // The selectable items to display in the collection. | 29 // The selectable items to display in the collection. |
37 @property(nonatomic, strong) NSArray<AutofillProfileItem*>* items; | 30 @property(nonatomic, strong) NSArray<PaymentsTextItem*>* items; |
38 | 31 |
39 @end | 32 @end |
40 | 33 |
41 @implementation ShippingAddressSelectionMediator | 34 @implementation ShippingOptionSelectionMediator |
42 | 35 |
| 36 @synthesize headerText = _headerText; |
43 @synthesize state = _state; | 37 @synthesize state = _state; |
44 @synthesize selectedItemIndex = _selectedItemIndex; | 38 @synthesize selectedItemIndex = _selectedItemIndex; |
45 @synthesize paymentRequest = _paymentRequest; | 39 @synthesize paymentRequest = _paymentRequest; |
46 @synthesize items = _items; | 40 @synthesize items = _items; |
47 @synthesize headerText = _headerText; | |
48 | 41 |
49 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | 42 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
50 self = [super init]; | 43 self = [super init]; |
51 if (self) { | 44 if (self) { |
52 _paymentRequest = paymentRequest; | 45 _paymentRequest = paymentRequest; |
53 _selectedItemIndex = NSUIntegerMax; | 46 _selectedItemIndex = NSUIntegerMax; |
54 _items = [self createItems]; | 47 _items = [self createItems]; |
55 } | 48 } |
56 return self; | 49 return self; |
57 } | 50 } |
(...skipping 14 matching lines...) Expand all Loading... |
72 - (NSArray<CollectionViewItem*>*)selectableItems { | 65 - (NSArray<CollectionViewItem*>*)selectableItems { |
73 return self.items; | 66 return self.items; |
74 } | 67 } |
75 | 68 |
76 - (CollectionViewItem*)selectableItemAtIndex:(NSUInteger)index { | 69 - (CollectionViewItem*)selectableItemAtIndex:(NSUInteger)index { |
77 DCHECK(index < self.items.count); | 70 DCHECK(index < self.items.count); |
78 return [self.items objectAtIndex:index]; | 71 return [self.items objectAtIndex:index]; |
79 } | 72 } |
80 | 73 |
81 - (CollectionViewItem*)addButtonItem { | 74 - (CollectionViewItem*)addButtonItem { |
82 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; | 75 return nil; |
83 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_ADDRESS); | |
84 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | |
85 return addButtonItem; | |
86 } | 76 } |
87 | 77 |
88 #pragma mark - Helper methods | 78 #pragma mark - Helper methods |
89 | 79 |
90 - (NSArray<AutofillProfileItem*>*)createItems { | 80 - (NSArray<PaymentsTextItem*>*)createItems { |
91 const std::vector<autofill::AutofillProfile*>& shippingProfiles = | 81 const std::vector<web::PaymentShippingOption*>& shippingOptions = |
92 _paymentRequest->shipping_profiles(); | 82 _paymentRequest->shipping_options(); |
93 NSMutableArray<AutofillProfileItem*>* items = | 83 NSMutableArray<PaymentsTextItem*>* items = |
94 [NSMutableArray arrayWithCapacity:shippingProfiles.size()]; | 84 [NSMutableArray arrayWithCapacity:shippingOptions.size()]; |
95 for (size_t index = 0; index < shippingProfiles.size(); ++index) { | 85 for (size_t index = 0; index < shippingOptions.size(); ++index) { |
96 autofill::AutofillProfile* shippingAddress = shippingProfiles[index]; | 86 web::PaymentShippingOption* shippingOption = shippingOptions[index]; |
97 DCHECK(shippingAddress); | 87 DCHECK(shippingOption); |
98 AutofillProfileItem* item = [[AutofillProfileItem alloc] init]; | 88 PaymentsTextItem* item = [[PaymentsTextItem alloc] init]; |
99 item.name = GetNameLabelFromAutofillProfile(*shippingAddress); | 89 item.text = base::SysUTF16ToNSString(shippingOption->label); |
100 item.address = GetShippingAddressLabelFromAutofillProfile(*shippingAddress); | 90 payments::CurrencyFormatter* currencyFormatter = |
101 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*shippingAddress); | 91 _paymentRequest->GetOrCreateCurrencyFormatter(); |
102 if (_paymentRequest->selected_shipping_profile() == shippingAddress) | 92 item.detailText = SysUTF16ToNSString(currencyFormatter->Format( |
| 93 base::UTF16ToASCII(shippingOption->amount.value))); |
| 94 if (_paymentRequest->selected_shipping_option() == shippingOption) |
103 _selectedItemIndex = index; | 95 _selectedItemIndex = index; |
104 | 96 |
105 [items addObject:item]; | 97 [items addObject:item]; |
106 } | 98 } |
107 return items; | 99 return items; |
108 } | 100 } |
109 | 101 |
110 @end | 102 @end |
OLD | NEW |