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 #import "ios/chrome/browser/ui/payments/contact_info_edit_coordinator.h" |
| 6 |
| 7 #include "base/guid.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #include "components/autofill/core/browser/autofill_country.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/personal_data_manager.h" |
| 13 #include "components/autofill/core/browser/validation.h" |
| 14 #include "components/autofill/core/common/autofill_constants.h" |
| 15 #include "components/payments/core/payments_profile_comparator.h" |
| 16 #include "components/strings/grit/components_strings.h" |
| 17 #include "ios/chrome/browser/application_context.h" |
| 18 #include "ios/chrome/browser/payments/payment_request.h" |
| 19 #import "ios/chrome/browser/ui/autofill/autofill_ui_type_util.h" |
| 20 #import "ios/chrome/browser/ui/payments/contact_info_edit_mediator.h" |
| 21 #import "ios/chrome/browser/ui/payments/payment_request_editor_field.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 |
| 24 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 25 #error "This file requires ARC support." |
| 26 #endif |
| 27 |
| 28 namespace { |
| 29 using ::AutofillTypeFromAutofillUIType; |
| 30 } // namespace |
| 31 |
| 32 @interface ContactInfoEditCoordinator () |
| 33 |
| 34 @property(nonatomic, strong) PaymentRequestEditViewController* viewController; |
| 35 |
| 36 @property(nonatomic, strong) ContactInfoEditMediator* mediator; |
| 37 |
| 38 @end |
| 39 |
| 40 @implementation ContactInfoEditCoordinator |
| 41 |
| 42 @synthesize profile = _profile; |
| 43 @synthesize paymentRequest = _paymentRequest; |
| 44 @synthesize delegate = _delegate; |
| 45 @synthesize viewController = _viewController; |
| 46 @synthesize mediator = _mediator; |
| 47 |
| 48 - (void)start { |
| 49 self.viewController = [[PaymentRequestEditViewController alloc] init]; |
| 50 // TODO(crbug.com/602666): Title varies depending on what field is missing. |
| 51 // e.g., Add Email vs. Add Phone Number. |
| 52 NSString* title = |
| 53 self.profile |
| 54 ? l10n_util::GetNSString(IDS_PAYMENTS_EDIT_CONTACT_DETAILS_LABEL) |
| 55 : l10n_util::GetNSString(IDS_PAYMENTS_ADD_CONTACT_DETAILS_LABEL); |
| 56 [self.viewController setTitle:title]; |
| 57 [self.viewController setDelegate:self]; |
| 58 [self.viewController setValidatorDelegate:self]; |
| 59 self.mediator = [[ContactInfoEditMediator alloc] |
| 60 initWithPaymentRequest:self.paymentRequest |
| 61 profile:self.profile]; |
| 62 [self.mediator setConsumer:self.viewController]; |
| 63 [self.viewController setDataSource:self.mediator]; |
| 64 [self.viewController loadModel]; |
| 65 |
| 66 DCHECK(self.baseViewController.navigationController); |
| 67 [[self baseViewController].navigationController |
| 68 pushViewController:self.viewController |
| 69 animated:YES]; |
| 70 } |
| 71 |
| 72 - (void)stop { |
| 73 [self.viewController.navigationController popViewControllerAnimated:YES]; |
| 74 self.viewController = nil; |
| 75 } |
| 76 |
| 77 #pragma mark - PaymentRequestEditViewControllerValidator |
| 78 |
| 79 - (NSString*)paymentRequestEditViewController: |
| 80 (PaymentRequestEditViewController*)controller |
| 81 validateField:(EditorField*)field { |
| 82 if (field.value.length) { |
| 83 switch (field.autofillUIType) { |
| 84 case AutofillUITypeProfileHomePhoneWholeNumber: { |
| 85 const std::string countryCode = |
| 86 autofill::AutofillCountry::CountryCodeForLocale( |
| 87 GetApplicationContext()->GetApplicationLocale()); |
| 88 if (!autofill::IsValidPhoneNumber(base::SysNSStringToUTF16(field.value), |
| 89 countryCode)) { |
| 90 return l10n_util::GetNSString( |
| 91 IDS_PAYMENTS_PHONE_INVALID_VALIDATION_MESSAGE); |
| 92 } |
| 93 break; |
| 94 } |
| 95 case AutofillUITypeProfileEmailAddress: { |
| 96 if (!autofill::IsValidEmailAddress( |
| 97 base::SysNSStringToUTF16(field.value))) { |
| 98 return l10n_util::GetNSString( |
| 99 IDS_PAYMENTS_EMAIL_INVALID_VALIDATION_MESSAGE); |
| 100 } |
| 101 break; |
| 102 } |
| 103 default: |
| 104 break; |
| 105 } |
| 106 } else if (field.isRequired) { |
| 107 return l10n_util::GetNSString( |
| 108 IDS_PAYMENTS_FIELD_REQUIRED_VALIDATION_MESSAGE); |
| 109 } |
| 110 return nil; |
| 111 } |
| 112 |
| 113 #pragma mark - PaymentRequestEditViewControllerDelegate |
| 114 |
| 115 - (void)paymentRequestEditViewController: |
| 116 (PaymentRequestEditViewController*)controller |
| 117 didFinishEditingFields:(NSArray<EditorField*>*)fields { |
| 118 // Create an empty autofill profile. If a profile is being edited, copy over |
| 119 // the information. |
| 120 autofill::AutofillProfile profile = |
| 121 self.profile ? *self.profile |
| 122 : autofill::AutofillProfile(base::GenerateGUID(), |
| 123 autofill::kSettingsOrigin); |
| 124 |
| 125 for (EditorField* field in fields) { |
| 126 profile.SetRawInfo(AutofillTypeFromAutofillUIType(field.autofillUIType), |
| 127 base::SysNSStringToUTF16(field.value)); |
| 128 } |
| 129 |
| 130 if (!self.profile) { |
| 131 self.paymentRequest->GetPersonalDataManager()->AddProfile(profile); |
| 132 |
| 133 // Add the profile to the list of profiles in |self.paymentRequest|. |
| 134 self.profile = self.paymentRequest->AddAutofillProfile(profile); |
| 135 } else { |
| 136 // Override the origin. |
| 137 profile.set_origin(autofill::kSettingsOrigin); |
| 138 self.paymentRequest->GetPersonalDataManager()->UpdateProfile(profile); |
| 139 |
| 140 // Cached profile must be invalidated once the profile is modified. |
| 141 _paymentRequest->profile_comparator()->Invalidate(profile); |
| 142 |
| 143 // Update the original profile instance that is being edited. |
| 144 *self.profile = profile; |
| 145 } |
| 146 |
| 147 [self.delegate contactInfoEditCoordinator:self |
| 148 didFinishEditingProfile:self.profile]; |
| 149 } |
| 150 |
| 151 - (void)paymentRequestEditViewControllerDidCancel: |
| 152 (PaymentRequestEditViewController*)controller { |
| 153 [self.delegate contactInfoEditCoordinatorDidCancel:self]; |
| 154 } |
| 155 |
| 156 @end |
OLD | NEW |