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_address_selection_mediator.h" | 7 #import "ios/chrome/browser/ui/payments/shipping_address_selection_mediator.h" |
8 | 8 |
9 #include "components/autofill/core/browser/autofill_profile.h" | 9 #include "components/autofill/core/browser/autofill_profile.h" |
10 #include "components/strings/grit/components_strings.h" | 10 #include "components/strings/grit/components_strings.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 } // namespace | 27 } // namespace |
28 | 28 |
29 @interface ShippingAddressSelectionMediator () | 29 @interface ShippingAddressSelectionMediator () |
30 | 30 |
31 // The PaymentRequest object owning an instance of web::PaymentRequest as | 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 | 32 // provided by the page invoking the Payment Request API. This is a weak |
33 // pointer and should outlive this class. | 33 // pointer and should outlive this class. |
34 @property(nonatomic, assign) PaymentRequest* paymentRequest; | 34 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
35 | 35 |
36 // The selectable items to display in the collection. | 36 // The selectable items to display in the collection. |
37 @property(nonatomic, strong) NSArray<AutofillProfileItem*>* items; | 37 @property(nonatomic, strong) NSMutableArray<AutofillProfileItem*>* items; |
38 | 38 |
39 @end | 39 @end |
40 | 40 |
41 @implementation ShippingAddressSelectionMediator | 41 @implementation ShippingAddressSelectionMediator |
42 | 42 |
43 @synthesize headerText = _headerText; | 43 @synthesize headerText = _headerText; |
44 @synthesize state = _state; | 44 @synthesize state = _state; |
45 @synthesize selectedItemIndex = _selectedItemIndex; | 45 @synthesize selectedItemIndex = _selectedItemIndex; |
46 @synthesize paymentRequest = _paymentRequest; | 46 @synthesize paymentRequest = _paymentRequest; |
47 @synthesize items = _items; | 47 @synthesize items = _items; |
48 | 48 |
49 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { | 49 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest { |
50 self = [super init]; | 50 self = [super init]; |
51 if (self) { | 51 if (self) { |
52 _paymentRequest = paymentRequest; | 52 _paymentRequest = paymentRequest; |
53 _selectedItemIndex = NSUIntegerMax; | 53 _selectedItemIndex = NSUIntegerMax; |
54 _items = [self createItems]; | 54 [self loadItems]; |
55 } | 55 } |
56 return self; | 56 return self; |
57 } | 57 } |
58 | 58 |
59 #pragma mark - PaymentRequestSelectorViewControllerDataSource | 59 #pragma mark - PaymentRequestSelectorViewControllerDataSource |
60 | 60 |
| 61 - (BOOL)allowsEditMode { |
| 62 return YES; |
| 63 } |
| 64 |
61 - (CollectionViewItem*)headerItem { | 65 - (CollectionViewItem*)headerItem { |
62 if (!self.headerText.length) | 66 if (!self.headerText.length) |
63 return nil; | 67 return nil; |
64 | 68 |
65 PaymentsTextItem* headerItem = [[PaymentsTextItem alloc] init]; | 69 PaymentsTextItem* headerItem = [[PaymentsTextItem alloc] init]; |
66 headerItem.text = self.headerText; | 70 headerItem.text = self.headerText; |
67 if (self.state == PaymentRequestSelectorStateError) | 71 if (self.state == PaymentRequestSelectorStateError) |
68 headerItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); | 72 headerItem.image = NativeImage(IDR_IOS_PAYMENTS_WARNING); |
69 return headerItem; | 73 return headerItem; |
70 } | 74 } |
71 | 75 |
72 - (NSArray<CollectionViewItem*>*)selectableItems { | 76 - (NSArray<CollectionViewItem*>*)selectableItems { |
73 return self.items; | 77 return self.items; |
74 } | 78 } |
75 | 79 |
76 - (CollectionViewItem*)addButtonItem { | 80 - (CollectionViewItem*)addButtonItem { |
77 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; | 81 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; |
78 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_ADDRESS); | 82 addButtonItem.text = l10n_util::GetNSString(IDS_PAYMENTS_ADD_ADDRESS); |
79 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | 83 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); |
80 return addButtonItem; | 84 return addButtonItem; |
81 } | 85 } |
82 | 86 |
83 #pragma mark - Helper methods | 87 #pragma mark - Public methods |
84 | 88 |
85 - (NSArray<AutofillProfileItem*>*)createItems { | 89 - (void)loadItems { |
86 const std::vector<autofill::AutofillProfile*>& shippingProfiles = | 90 const std::vector<autofill::AutofillProfile*>& shippingProfiles = |
87 _paymentRequest->shipping_profiles(); | 91 _paymentRequest->shipping_profiles(); |
88 NSMutableArray<AutofillProfileItem*>* items = | 92 _items = [NSMutableArray arrayWithCapacity:shippingProfiles.size()]; |
89 [NSMutableArray arrayWithCapacity:shippingProfiles.size()]; | |
90 for (size_t index = 0; index < shippingProfiles.size(); ++index) { | 93 for (size_t index = 0; index < shippingProfiles.size(); ++index) { |
91 autofill::AutofillProfile* shippingAddress = shippingProfiles[index]; | 94 autofill::AutofillProfile* shippingAddress = shippingProfiles[index]; |
92 DCHECK(shippingAddress); | 95 DCHECK(shippingAddress); |
93 AutofillProfileItem* item = [[AutofillProfileItem alloc] init]; | 96 AutofillProfileItem* item = [[AutofillProfileItem alloc] init]; |
94 item.name = GetNameLabelFromAutofillProfile(*shippingAddress); | 97 item.name = GetNameLabelFromAutofillProfile(*shippingAddress); |
95 item.address = GetShippingAddressLabelFromAutofillProfile(*shippingAddress); | 98 item.address = GetShippingAddressLabelFromAutofillProfile(*shippingAddress); |
96 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*shippingAddress); | 99 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*shippingAddress); |
97 if (_paymentRequest->selected_shipping_profile() == shippingAddress) | 100 if (_paymentRequest->selected_shipping_profile() == shippingAddress) |
98 _selectedItemIndex = index; | 101 _selectedItemIndex = index; |
99 | 102 |
100 [items addObject:item]; | 103 [_items addObject:item]; |
101 } | 104 } |
102 return items; | |
103 } | 105 } |
104 | 106 |
105 @end | 107 @end |
OLD | NEW |