| 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 #import "ios/chrome/browser/ui/payments/address_edit_mediator.h" | 5 #import "ios/chrome/browser/ui/payments/address_edit_mediator.h" |
| 6 | 6 |
| 7 #include <map> | 7 #include <map> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/logging.h" |
| 13 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/strings/sys_string_conversions.h" | 15 #include "base/strings/sys_string_conversions.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "components/autofill/core/browser/autofill_address_util.h" | 17 #include "components/autofill/core/browser/autofill_address_util.h" |
| 17 #include "components/autofill/core/browser/autofill_country.h" | 18 #include "components/autofill/core/browser/autofill_country.h" |
| 18 #include "components/autofill/core/browser/autofill_profile.h" | 19 #include "components/autofill/core/browser/autofill_profile.h" |
| 19 #include "components/autofill/core/browser/autofill_type.h" | 20 #include "components/autofill/core/browser/autofill_type.h" |
| 20 #include "components/autofill/core/browser/country_combobox_model.h" | 21 #include "components/autofill/core/browser/country_combobox_model.h" |
| 21 #include "components/autofill/core/browser/field_types.h" | 22 #include "components/autofill/core/browser/field_types.h" |
| 22 #include "components/autofill/core/browser/personal_data_manager.h" | 23 #include "components/autofill/core/browser/personal_data_manager.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 @property(nonatomic, strong) EditorField* regionField; | 62 @property(nonatomic, strong) EditorField* regionField; |
| 62 | 63 |
| 63 @end | 64 @end |
| 64 | 65 |
| 65 @implementation AddressEditMediator | 66 @implementation AddressEditMediator |
| 66 | 67 |
| 67 @synthesize state = _state; | 68 @synthesize state = _state; |
| 68 @synthesize consumer = _consumer; | 69 @synthesize consumer = _consumer; |
| 69 @synthesize countries = _countries; | 70 @synthesize countries = _countries; |
| 70 @synthesize selectedCountryCode = _selectedCountryCode; | 71 @synthesize selectedCountryCode = _selectedCountryCode; |
| 71 @synthesize regions = _regions; | |
| 72 @synthesize paymentRequest = _paymentRequest; | 72 @synthesize paymentRequest = _paymentRequest; |
| 73 @synthesize address = _address; | 73 @synthesize address = _address; |
| 74 @synthesize fieldsMap = _fieldsMap; | 74 @synthesize fieldsMap = _fieldsMap; |
| 75 @synthesize fields = _fields; | 75 @synthesize fields = _fields; |
| 76 @synthesize regionField = _regionField; | 76 @synthesize regionField = _regionField; |
| 77 | 77 |
| 78 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest | 78 - (instancetype)initWithPaymentRequest:(PaymentRequest*)paymentRequest |
| 79 address:(autofill::AutofillProfile*)address { | 79 address:(autofill::AutofillProfile*)address { |
| 80 self = [super init]; | 80 self = [super init]; |
| 81 if (self) { | 81 if (self) { |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 119 return NO; | 119 return NO; |
| 120 } | 120 } |
| 121 | 121 |
| 122 - (UIImage*)iconIdentifyingEditorField:(EditorField*)field { | 122 - (UIImage*)iconIdentifyingEditorField:(EditorField*)field { |
| 123 return nil; | 123 return nil; |
| 124 } | 124 } |
| 125 | 125 |
| 126 #pragma mark - RegionDataLoaderConsumer | 126 #pragma mark - RegionDataLoaderConsumer |
| 127 | 127 |
| 128 - (void)regionDataLoaderDidSucceedWithRegions: | 128 - (void)regionDataLoaderDidSucceedWithRegions: |
| 129 (NSMutableArray<NSString*>*)regions { | 129 (NSDictionary<NSString*, NSString*>*)regions { |
| 130 self.regions = regions; | |
| 131 // Enable the previously disabled field. | 130 // Enable the previously disabled field. |
| 132 self.regionField.enabled = YES; | 131 self.regionField.enabled = YES; |
| 133 | 132 |
| 134 // If an address is being edited and it has a valid region, the field value is | 133 // An autofill profile may have a region code or a region name stored as the |
| 135 // set to that region. Otherwise, set the field value to nil. If creating an | 134 // autofill::ADDRESS_HOME_STATE. If an address is being edited whose value for |
| 136 // address, set the first available region as the field value, if possible. | 135 // that field type is a valid region code or a valid region name, the editor |
| 136 // field value is set to the respective region code. Otherwise, it is set to |
| 137 // nil. |
| 138 self.regionField.value = nil; |
| 137 if (self.address) { | 139 if (self.address) { |
| 138 NSString* region = | 140 NSString* region = |
| 139 [self fieldValueFromProfile:self.address | 141 [self fieldValueFromProfile:self.address |
| 140 fieldType:autofill::ADDRESS_HOME_STATE]; | 142 fieldType:autofill::ADDRESS_HOME_STATE]; |
| 141 self.regionField.value = | 143 if ([regions objectForKey:region]) { |
| 142 [self.regions containsObject:region] ? region : nil; | 144 self.regionField.value = region; |
| 143 } else { | 145 } else if ([[regions allKeysForObject:region] count]) { |
| 144 self.regionField.value = regions.count ? regions[0] : nil; | 146 DCHECK(1 == [[regions allKeysForObject:region] count]); |
| 147 self.regionField.value = [regions allKeysForObject:region][0]; |
| 148 } |
| 145 } | 149 } |
| 146 | 150 |
| 147 // Notify the view controller asynchronously to allow for the view to update. | 151 // Notify the view controller asynchronously to allow for the view to update. |
| 148 __weak AddressEditMediator* weakSelf = self; | 152 __weak AddressEditMediator* weakSelf = self; |
| 149 dispatch_async(dispatch_get_main_queue(), ^{ | 153 dispatch_async(dispatch_get_main_queue(), ^{ |
| 150 [weakSelf.consumer setOptions:weakSelf.regions | 154 [weakSelf.consumer setOptions:[regions allKeys] |
| 151 forEditorField:weakSelf.regionField]; | 155 forEditorField:weakSelf.regionField]; |
| 152 }); | 156 }); |
| 153 } | 157 } |
| 154 | 158 |
| 155 #pragma mark - Helper methods | 159 #pragma mark - Helper methods |
| 156 | 160 |
| 157 // Loads the country codes and names and sets the default selected country code. | 161 // Loads the country codes and names and sets the default selected country code. |
| 158 - (void)loadCountries { | 162 - (void)loadCountries { |
| 159 autofill::CountryComboboxModel countryModel; | 163 autofill::CountryComboboxModel countryModel; |
| 160 countryModel.SetCountries(*_paymentRequest->GetPersonalDataManager(), | 164 countryModel.SetCountries(*_paymentRequest->GetPersonalDataManager(), |
| (...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 314 // corresponding field value. Returns nil if |profile| is nullptr. | 318 // corresponding field value. Returns nil if |profile| is nullptr. |
| 315 - (NSString*)fieldValueFromProfile:(autofill::AutofillProfile*)profile | 319 - (NSString*)fieldValueFromProfile:(autofill::AutofillProfile*)profile |
| 316 fieldType:(autofill::ServerFieldType)fieldType { | 320 fieldType:(autofill::ServerFieldType)fieldType { |
| 317 return profile ? base::SysUTF16ToNSString(profile->GetInfo( | 321 return profile ? base::SysUTF16ToNSString(profile->GetInfo( |
| 318 autofill::AutofillType(fieldType), | 322 autofill::AutofillType(fieldType), |
| 319 GetApplicationContext()->GetApplicationLocale())) | 323 GetApplicationContext()->GetApplicationLocale())) |
| 320 : nil; | 324 : nil; |
| 321 } | 325 } |
| 322 | 326 |
| 323 @end | 327 @end |
| OLD | NEW |