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_address_selection_mediator.h" | |
8 | |
9 #include "components/autofill/core/browser/autofill_profile.h" | |
10 #include "components/strings/grit/components_strings.h" | |
11 #import "ios/chrome/browser/payments/cells/autofill_profile_item.h" | |
12 #import "ios/chrome/browser/payments/cells/payments_text_item.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" | |
16 #include "ios/chrome/grit/ios_theme_resources.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 | |
19 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
20 #error "This file requires ARC support." | |
21 #endif | |
22 | |
23 namespace { | |
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 | |
31 // The PaymentRequest object owning an instance of web::PaymentRequest as | |
32 // provided by the page invoking the Payment Request API. This is a weak | |
33 // pointer and should outlive this class. | |
34 @property(nonatomic, assign) PaymentRequest* paymentRequest; | |
gambard
2017/04/18 14:34:46
s/assign/weak
Moe
2017/04/18 17:19:17
PaymentRequest isn't an Objective-C object.
| |
35 | |
36 // The selectable items to display in the collection. | |
37 @property(nonatomic, strong) NSArray<AutofillProfileItem*>* items; | |
38 | |
39 @end | |
40 | |
41 @implementation ShippingAddressSelectionMediator | |
42 | |
43 @synthesize state = _state; | |
44 @synthesize selectedItemIndex = _selectedItemIndex; | |
45 @synthesize paymentRequest = _paymentRequest; | |
46 @synthesize items = _items; | |
47 @synthesize message = _message; | |
48 | |
49 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | |
50 self = [super init]; | |
51 if (self) { | |
52 _paymentRequest = paymentRequest; | |
53 _selectedItemIndex = NSUIntegerMax; | |
54 _items = [self createItems]; | |
55 } | |
56 return self; | |
57 } | |
58 | |
59 #pragma mark - PaymentRequestSelectorViewControllerDataSource | |
60 | |
61 - (CollectionViewItem*)headerItem { | |
62 if (!self.message.length) | |
63 return nil; | |
64 | |
65 PaymentsTextItem* headerItem = [[PaymentsTextItem alloc] init]; | |
66 headerItem.text = self.message; | |
67 if (self.state == PaymentRequestSelectorStateError) | |
68 headerItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | |
69 return headerItem; | |
70 } | |
71 | |
72 - (NSArray<CollectionViewItem*>*)selectableItems { | |
73 return self.items; | |
74 } | |
75 | |
76 - (CollectionViewItem*)selectableItemAtIndex:(NSUInteger)index { | |
77 DCHECK(index < self.items.count); | |
gambard
2017/04/18 14:34:46
You did not specify in the protocol that |index| s
Moe
2017/04/18 17:19:16
Done.
| |
78 return [self.items objectAtIndex:index]; | |
79 } | |
80 | |
81 - (CollectionViewItem*)addButtonItem { | |
82 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; | |
83 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_ADDRESS); | |
84 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | |
85 return addButtonItem; | |
86 } | |
87 | |
88 #pragma mark - Helper methods | |
89 | |
90 - (NSArray<AutofillProfileItem*>*)createItems { | |
91 const std::vector<autofill::AutofillProfile*>& shippingProfiles = | |
92 _paymentRequest->shipping_profiles(); | |
93 NSMutableArray<AutofillProfileItem*>* items = | |
94 [NSMutableArray arrayWithCapacity:shippingProfiles.size()]; | |
95 for (size_t index = 0; index < shippingProfiles.size(); ++index) { | |
96 autofill::AutofillProfile* shippingAddress = shippingProfiles[index]; | |
97 DCHECK(shippingAddress); | |
98 AutofillProfileItem* item = [[AutofillProfileItem alloc] init]; | |
99 item.name = GetNameLabelFromAutofillProfile(*shippingAddress); | |
100 item.address = GetShippingAddressLabelFromAutofillProfile(*shippingAddress); | |
101 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*shippingAddress); | |
102 if (_paymentRequest->selected_shipping_profile() == shippingAddress) | |
103 _selectedItemIndex = index; | |
104 | |
105 [items addObject:item]; | |
106 } | |
107 return items; | |
108 } | |
109 | |
110 @end | |
OLD | NEW |