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/shipping_option_selection_mediator.h" | 7 #import "ios/chrome/browser/ui/payments/shipping_option_selection_mediator.h" |
8 | 8 |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
11 #include "components/payments/core/currency_formatter.h" | 11 #include "components/payments/core/currency_formatter.h" |
12 #include "ios/chrome/browser/payments/payment_request.h" | 12 #include "ios/chrome/browser/payments/payment_request.h" |
13 #import "ios/chrome/browser/ui/payments/cells/payments_text_item.h" | 13 #import "ios/chrome/browser/ui/payments/cells/payments_text_item.h" |
14 #include "ios/chrome/browser/ui/uikit_ui_util.h" | 14 #include "ios/chrome/browser/ui/uikit_ui_util.h" |
15 #include "ios/chrome/grit/ios_theme_resources.h" | 15 #include "ios/chrome/grit/ios_theme_resources.h" |
16 #include "ios/web/public/payments/payment_request.h" | 16 #include "ios/web/public/payments/payment_request.h" |
17 | 17 |
18 #if !defined(__has_feature) || !__has_feature(objc_arc) | 18 #if !defined(__has_feature) || !__has_feature(objc_arc) |
19 #error "This file requires ARC support." | 19 #error "This file requires ARC support." |
20 #endif | 20 #endif |
21 | 21 |
22 @interface ShippingOptionSelectionMediator () | 22 @interface ShippingOptionSelectionMediator () |
23 | 23 |
24 // The PaymentRequest object owning an instance of web::PaymentRequest as | 24 // The PaymentRequest object owning an instance of web::PaymentRequest as |
25 // 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 |
26 // pointer and should outlive this class. | 26 // pointer and should outlive this class. |
27 @property(nonatomic, assign) PaymentRequest* paymentRequest; | 27 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
28 | 28 |
29 // The selectable items to display in the collection. | 29 // The selectable items to display in the collection. |
30 @property(nonatomic, strong) NSArray<PaymentsTextItem*>* items; | 30 @property(nonatomic, strong) NSMutableArray<PaymentsTextItem*>* items; |
| 31 |
| 32 // Creates and stores the selectable items to display in the collection. |
| 33 - (void)loadItems; |
31 | 34 |
32 @end | 35 @end |
33 | 36 |
34 @implementation ShippingOptionSelectionMediator | 37 @implementation ShippingOptionSelectionMediator |
35 | 38 |
36 @synthesize headerText = _headerText; | 39 @synthesize headerText = _headerText; |
37 @synthesize state = _state; | 40 @synthesize state = _state; |
38 @synthesize selectedItemIndex = _selectedItemIndex; | 41 @synthesize selectedItemIndex = _selectedItemIndex; |
39 @synthesize paymentRequest = _paymentRequest; | 42 @synthesize paymentRequest = _paymentRequest; |
40 @synthesize items = _items; | 43 @synthesize items = _items; |
41 | 44 |
42 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | 45 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
43 self = [super init]; | 46 self = [super init]; |
44 if (self) { | 47 if (self) { |
45 _paymentRequest = paymentRequest; | 48 _paymentRequest = paymentRequest; |
46 _selectedItemIndex = NSUIntegerMax; | 49 _selectedItemIndex = NSUIntegerMax; |
47 _items = [self createItems]; | 50 [self loadItems]; |
48 } | 51 } |
49 return self; | 52 return self; |
50 } | 53 } |
51 | 54 |
52 #pragma mark - PaymentRequestSelectorViewControllerDataSource | 55 #pragma mark - PaymentRequestSelectorViewControllerDataSource |
53 | 56 |
| 57 - (BOOL)allowsEditMode { |
| 58 return NO; |
| 59 } |
| 60 |
54 - (CollectionViewItem*)headerItem { | 61 - (CollectionViewItem*)headerItem { |
55 if (!self.headerText.length) | 62 if (!self.headerText.length) |
56 return nil; | 63 return nil; |
57 | 64 |
58 PaymentsTextItem* headerItem = [[PaymentsTextItem alloc] init]; | 65 PaymentsTextItem* headerItem = [[PaymentsTextItem alloc] init]; |
59 headerItem.text = self.headerText; | 66 headerItem.text = self.headerText; |
60 if (self.state == PaymentRequestSelectorStateError) | 67 if (self.state == PaymentRequestSelectorStateError) |
61 headerItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | 68 headerItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); |
62 return headerItem; | 69 return headerItem; |
63 } | 70 } |
64 | 71 |
65 - (NSArray<CollectionViewItem*>*)selectableItems { | 72 - (NSArray<CollectionViewItem*>*)selectableItems { |
66 return self.items; | 73 return self.items; |
67 } | 74 } |
68 | 75 |
69 - (CollectionViewItem*)addButtonItem { | 76 - (CollectionViewItem*)addButtonItem { |
70 return nil; | 77 return nil; |
71 } | 78 } |
72 | 79 |
73 #pragma mark - Helper methods | 80 #pragma mark - Helper methods |
74 | 81 |
75 - (NSArray<PaymentsTextItem*>*)createItems { | 82 - (void)loadItems { |
76 const std::vector<web::PaymentShippingOption*>& shippingOptions = | 83 const std::vector<web::PaymentShippingOption*>& shippingOptions = |
77 _paymentRequest->shipping_options(); | 84 _paymentRequest->shipping_options(); |
78 NSMutableArray<PaymentsTextItem*>* items = | 85 _items = [NSMutableArray arrayWithCapacity:shippingOptions.size()]; |
79 [NSMutableArray arrayWithCapacity:shippingOptions.size()]; | |
80 for (size_t index = 0; index < shippingOptions.size(); ++index) { | 86 for (size_t index = 0; index < shippingOptions.size(); ++index) { |
81 web::PaymentShippingOption* shippingOption = shippingOptions[index]; | 87 web::PaymentShippingOption* shippingOption = shippingOptions[index]; |
82 DCHECK(shippingOption); | 88 DCHECK(shippingOption); |
83 PaymentsTextItem* item = [[PaymentsTextItem alloc] init]; | 89 PaymentsTextItem* item = [[PaymentsTextItem alloc] init]; |
84 item.text = base::SysUTF16ToNSString(shippingOption->label); | 90 item.text = base::SysUTF16ToNSString(shippingOption->label); |
85 payments::CurrencyFormatter* currencyFormatter = | 91 payments::CurrencyFormatter* currencyFormatter = |
86 _paymentRequest->GetOrCreateCurrencyFormatter(); | 92 _paymentRequest->GetOrCreateCurrencyFormatter(); |
87 item.detailText = SysUTF16ToNSString(currencyFormatter->Format( | 93 item.detailText = SysUTF16ToNSString(currencyFormatter->Format( |
88 base::UTF16ToASCII(shippingOption->amount.value))); | 94 base::UTF16ToASCII(shippingOption->amount.value))); |
89 if (_paymentRequest->selected_shipping_option() == shippingOption) | 95 if (_paymentRequest->selected_shipping_option() == shippingOption) |
90 _selectedItemIndex = index; | 96 _selectedItemIndex = index; |
91 | 97 |
92 [items addObject:item]; | 98 [_items addObject:item]; |
93 } | 99 } |
94 return items; | |
95 } | 100 } |
96 | 101 |
97 @end | 102 @end |
OLD | NEW |