| 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/contact_info_selection_mediator.h" | 7 #import "ios/chrome/browser/ui/payments/contact_info_selection_mediator.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "components/autofill/core/browser/autofill_profile.h" | 10 #include "components/autofill/core/browser/autofill_profile.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 } // namespace | 28 } // namespace |
| 29 | 29 |
| 30 @interface ContactInfoSelectionMediator () | 30 @interface ContactInfoSelectionMediator () |
| 31 | 31 |
| 32 // The PaymentRequest object owning an instance of web::PaymentRequest as | 32 // The PaymentRequest object owning an instance of web::PaymentRequest as |
| 33 // provided by the page invoking the Payment Request API. This is a weak | 33 // provided by the page invoking the Payment Request API. This is a weak |
| 34 // pointer and should outlive this class. | 34 // pointer and should outlive this class. |
| 35 @property(nonatomic, assign) PaymentRequest* paymentRequest; | 35 @property(nonatomic, assign) PaymentRequest* paymentRequest; |
| 36 | 36 |
| 37 // The selectable items to display in the collection. | 37 // The selectable items to display in the collection. |
| 38 @property(nonatomic, strong) NSArray<AutofillProfileItem*>* items; | 38 @property(nonatomic, strong) NSMutableArray<AutofillProfileItem*>* items; |
| 39 | 39 |
| 40 @end | 40 @end |
| 41 | 41 |
| 42 @implementation ContactInfoSelectionMediator | 42 @implementation ContactInfoSelectionMediator |
| 43 | 43 |
| 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 return nil; | 66 return nil; |
| 63 } | 67 } |
| 64 | 68 |
| 65 - (NSArray<CollectionViewItem*>*)selectableItems { | 69 - (NSArray<CollectionViewItem*>*)selectableItems { |
| 66 return self.items; | 70 return self.items; |
| 67 } | 71 } |
| 68 | 72 |
| 69 - (CollectionViewItem*)addButtonItem { | 73 - (CollectionViewItem*)addButtonItem { |
| 70 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; | 74 PaymentsTextItem* addButtonItem = [[PaymentsTextItem alloc] init]; |
| 71 addButtonItem.text = | 75 addButtonItem.text = |
| 72 l10n_util::GetNSString(IDS_PAYMENTS_ADD_CONTACT_DETAILS_LABEL); | 76 l10n_util::GetNSString(IDS_PAYMENTS_ADD_CONTACT_DETAILS_LABEL); |
| 73 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); | 77 addButtonItem.image = NativeImage(IDR_IOS_PAYMENTS_ADD); |
| 74 return addButtonItem; | 78 return addButtonItem; |
| 75 } | 79 } |
| 76 | 80 |
| 77 #pragma mark - Helper methods | 81 #pragma mark - Public methods |
| 78 | 82 |
| 79 - (NSArray<AutofillProfileItem*>*)createItems { | 83 - (void)loadItems { |
| 80 const std::vector<autofill::AutofillProfile*>& contactProfiles = | 84 const std::vector<autofill::AutofillProfile*>& contactProfiles = |
| 81 _paymentRequest->contact_profiles(); | 85 _paymentRequest->contact_profiles(); |
| 82 | 86 |
| 83 NSMutableArray<AutofillProfileItem*>* items = | 87 _items = [NSMutableArray arrayWithCapacity:contactProfiles.size()]; |
| 84 [NSMutableArray arrayWithCapacity:contactProfiles.size()]; | |
| 85 for (size_t index = 0; index < contactProfiles.size(); ++index) { | 88 for (size_t index = 0; index < contactProfiles.size(); ++index) { |
| 86 autofill::AutofillProfile* contactProfile = contactProfiles[index]; | 89 autofill::AutofillProfile* contactProfile = contactProfiles[index]; |
| 87 DCHECK(contactProfile); | 90 DCHECK(contactProfile); |
| 88 AutofillProfileItem* item = [[AutofillProfileItem alloc] init]; | 91 AutofillProfileItem* item = [[AutofillProfileItem alloc] init]; |
| 89 item.name = GetNameLabelFromAutofillProfile(*contactProfile); | 92 item.name = GetNameLabelFromAutofillProfile(*contactProfile); |
| 90 item.email = GetEmailLabelFromAutofillProfile(*contactProfile); | 93 item.email = GetEmailLabelFromAutofillProfile(*contactProfile); |
| 91 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*contactProfile); | 94 item.phoneNumber = GetPhoneNumberLabelFromAutofillProfile(*contactProfile); |
| 92 if (_paymentRequest->selected_contact_profile() == contactProfile) | 95 if (_paymentRequest->selected_contact_profile() == contactProfile) |
| 93 _selectedItemIndex = index; | 96 _selectedItemIndex = index; |
| 94 | 97 |
| 95 [items addObject:item]; | 98 [_items addObject:item]; |
| 96 } | 99 } |
| 97 return items; | |
| 98 } | 100 } |
| 99 | 101 |
| 100 @end | 102 @end |
| OLD | NEW |