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