OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/settings/autofill_profile_edit_collection_view_co
ntroller.h" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/mac/scoped_nsobject.h" |
| 10 #include "base/strings/sys_string_conversions.h" |
| 11 #include "components/autofill/core/browser/autofill_profile.h" |
| 12 #include "components/autofill/core/browser/field_types.h" |
| 13 #include "components/autofill/core/browser/payments/payments_service_url.h" |
| 14 #include "components/autofill/core/browser/personal_data_manager.h" |
| 15 #include "ios/chrome/browser/application_context.h" |
| 16 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 17 #import "ios/chrome/browser/ui/commands/UIKit+ChromeExecuteCommand.h" |
| 18 #include "ios/chrome/browser/ui/commands/ios_command_ids.h" |
| 19 #import "ios/chrome/browser/ui/commands/open_url_command.h" |
| 20 #import "ios/chrome/browser/ui/settings/cells/autofill_edit_item.h" |
| 21 #include "ios/chrome/grit/ios_strings.h" |
| 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "url/gurl.h" |
| 24 |
| 25 NSString* const kAutofillProfileEditCollectionViewId = |
| 26 @"kAutofillProfileEditCollectionViewId"; |
| 27 |
| 28 namespace { |
| 29 |
| 30 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 31 SectionIdentifierFields = kSectionIdentifierEnumZero, |
| 32 }; |
| 33 |
| 34 typedef NS_ENUM(NSInteger, ItemType) { |
| 35 ItemTypeField = kItemTypeEnumZero, |
| 36 }; |
| 37 |
| 38 struct AutofillFieldDisplayInfo { |
| 39 autofill::ServerFieldType autofillType; |
| 40 int displayStringID; |
| 41 UIReturnKeyType returnKeyType; |
| 42 UIKeyboardType keyboardType; |
| 43 UITextAutocapitalizationType autoCapitalizationType; |
| 44 }; |
| 45 |
| 46 static const AutofillFieldDisplayInfo kFieldsToDisplay[] = { |
| 47 {autofill::NAME_FULL, IDS_IOS_AUTOFILL_FULLNAME, UIReturnKeyNext, |
| 48 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 49 {autofill::COMPANY_NAME, IDS_IOS_AUTOFILL_COMPANY_NAME, UIReturnKeyNext, |
| 50 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 51 {autofill::ADDRESS_HOME_LINE1, IDS_IOS_AUTOFILL_ADDRESS1, UIReturnKeyNext, |
| 52 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 53 {autofill::ADDRESS_HOME_LINE2, IDS_IOS_AUTOFILL_ADDRESS2, UIReturnKeyNext, |
| 54 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 55 {autofill::ADDRESS_HOME_CITY, IDS_IOS_AUTOFILL_CITY, UIReturnKeyNext, |
| 56 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 57 {autofill::ADDRESS_HOME_STATE, IDS_IOS_AUTOFILL_STATE, UIReturnKeyNext, |
| 58 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 59 {autofill::ADDRESS_HOME_ZIP, IDS_IOS_AUTOFILL_ZIP, UIReturnKeyNext, |
| 60 UIKeyboardTypeNumberPad, UITextAutocapitalizationTypeSentences}, |
| 61 {autofill::ADDRESS_HOME_COUNTRY, IDS_IOS_AUTOFILL_COUNTRY, UIReturnKeyNext, |
| 62 UIKeyboardTypeDefault, UITextAutocapitalizationTypeSentences}, |
| 63 {autofill::PHONE_HOME_WHOLE_NUMBER, IDS_IOS_AUTOFILL_PHONE, UIReturnKeyNext, |
| 64 UIKeyboardTypePhonePad, UITextAutocapitalizationTypeSentences}, |
| 65 {autofill::EMAIL_ADDRESS, IDS_IOS_AUTOFILL_EMAIL, UIReturnKeyDone, |
| 66 UIKeyboardTypeEmailAddress, UITextAutocapitalizationTypeNone}}; |
| 67 |
| 68 } // namespace |
| 69 |
| 70 @interface AutofillProfileEditCollectionViewController () |
| 71 |
| 72 // Initializes a AutofillProfileEditCollectionViewController with |profile| and |
| 73 // |dataManager|. |
| 74 - (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile |
| 75 personalDataManager:(autofill::PersonalDataManager*)dataManager |
| 76 NS_DESIGNATED_INITIALIZER; |
| 77 |
| 78 @end |
| 79 |
| 80 @implementation AutofillProfileEditCollectionViewController { |
| 81 autofill::PersonalDataManager* _personalDataManager; // weak |
| 82 autofill::AutofillProfile _autofillProfile; |
| 83 } |
| 84 |
| 85 #pragma mark - Initialization |
| 86 |
| 87 - (instancetype)initWithProfile:(const autofill::AutofillProfile&)profile |
| 88 personalDataManager:(autofill::PersonalDataManager*)dataManager { |
| 89 DCHECK(dataManager); |
| 90 |
| 91 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 92 if (self) { |
| 93 _personalDataManager = dataManager; |
| 94 _autofillProfile = profile; |
| 95 |
| 96 [self setCollectionViewAccessibilityIdentifier: |
| 97 kAutofillProfileEditCollectionViewId]; |
| 98 [self setTitle:l10n_util::GetNSString(IDS_IOS_AUTOFILL_EDIT_ADDRESS)]; |
| 99 [self loadModel]; |
| 100 } |
| 101 |
| 102 return self; |
| 103 } |
| 104 |
| 105 + (instancetype)controllerWithProfile:(const autofill::AutofillProfile&)profile |
| 106 personalDataManager: |
| 107 (autofill::PersonalDataManager*)dataManager { |
| 108 return [[[self alloc] initWithProfile:profile personalDataManager:dataManager] |
| 109 autorelease]; |
| 110 } |
| 111 |
| 112 #pragma mark - SettingsRootCollectionViewController |
| 113 |
| 114 - (void)editButtonPressed { |
| 115 // In the case of server profiles, open the Payments editing page instead. |
| 116 if (_autofillProfile.record_type() == |
| 117 autofill::AutofillProfile::SERVER_PROFILE) { |
| 118 GURL paymentsURL = autofill::payments::GetManageAddressesUrl(0); |
| 119 base::scoped_nsobject<OpenUrlCommand> command( |
| 120 [[OpenUrlCommand alloc] initWithURLFromChrome:paymentsURL]); |
| 121 [command setTag:IDC_CLOSE_SETTINGS_AND_OPEN_URL]; |
| 122 [self chromeExecuteCommand:command]; |
| 123 |
| 124 // Don't call [super editButtonPressed] because edit mode is not actually |
| 125 // entered in this case. |
| 126 return; |
| 127 } |
| 128 |
| 129 [super editButtonPressed]; |
| 130 |
| 131 if (!self.editor.editing) { |
| 132 CollectionViewModel* model = self.collectionViewModel; |
| 133 NSInteger itemCount = |
| 134 [model numberOfItemsInSection: |
| 135 [model sectionForSectionIdentifier:SectionIdentifierFields]]; |
| 136 |
| 137 // Reads the values from the fields and updates the local copy of the |
| 138 // profile accordingly. |
| 139 NSInteger section = |
| 140 [model sectionForSectionIdentifier:SectionIdentifierFields]; |
| 141 for (NSInteger itemIndex = 0; itemIndex < itemCount; ++itemIndex) { |
| 142 NSIndexPath* path = |
| 143 [NSIndexPath indexPathForItem:itemIndex inSection:section]; |
| 144 AutofillEditItem* item = base::mac::ObjCCastStrict<AutofillEditItem>( |
| 145 [model itemAtIndexPath:path]); |
| 146 autofill::ServerFieldType serverFieldType = item.autofillType; |
| 147 if (serverFieldType == autofill::ADDRESS_HOME_COUNTRY) { |
| 148 _autofillProfile.SetInfo( |
| 149 autofill::AutofillType(serverFieldType), |
| 150 base::SysNSStringToUTF16(item.textFieldValue), |
| 151 GetApplicationContext()->GetApplicationLocale()); |
| 152 } else { |
| 153 _autofillProfile.SetRawInfo( |
| 154 serverFieldType, base::SysNSStringToUTF16(item.textFieldValue)); |
| 155 } |
| 156 } |
| 157 |
| 158 _personalDataManager->UpdateProfile(_autofillProfile); |
| 159 } |
| 160 |
| 161 // Reload the model. |
| 162 [self loadModel]; |
| 163 // Update the cells. |
| 164 [self reconfigureCellsForItems: |
| 165 [self.collectionViewModel |
| 166 itemsInSectionWithIdentifier:SectionIdentifierFields] |
| 167 inSectionWithIdentifier:SectionIdentifierFields]; |
| 168 } |
| 169 |
| 170 - (void)loadModel { |
| 171 [super loadModel]; |
| 172 CollectionViewModel* model = self.collectionViewModel; |
| 173 |
| 174 std::string locale = GetApplicationContext()->GetApplicationLocale(); |
| 175 [model addSectionWithIdentifier:SectionIdentifierFields]; |
| 176 for (size_t i = 0; i < arraysize(kFieldsToDisplay); ++i) { |
| 177 const AutofillFieldDisplayInfo& field = kFieldsToDisplay[i]; |
| 178 AutofillEditItem* item = |
| 179 [[[AutofillEditItem alloc] initWithType:ItemTypeField] autorelease]; |
| 180 item.textFieldName = l10n_util::GetNSString(field.displayStringID); |
| 181 item.textFieldValue = base::SysUTF16ToNSString(_autofillProfile.GetInfo( |
| 182 autofill::AutofillType(field.autofillType), locale)); |
| 183 item.autofillType = field.autofillType; |
| 184 item.textFieldEnabled = self.editor.editing; |
| 185 [model addItem:item toSectionWithIdentifier:SectionIdentifierFields]; |
| 186 } |
| 187 } |
| 188 |
| 189 #pragma mark - MDCCollectionViewEditingDelegate |
| 190 |
| 191 - (BOOL)collectionViewAllowsEditing:(UICollectionView*)collectionView { |
| 192 // The collection view needs to allow editing in order to respond to the Edit |
| 193 // button. |
| 194 return YES; |
| 195 } |
| 196 |
| 197 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 198 canEditItemAtIndexPath:(NSIndexPath*)indexPath { |
| 199 // Items in this collection view are not deletable, so should not be seen |
| 200 // as editable by the collection view. |
| 201 return NO; |
| 202 } |
| 203 |
| 204 #pragma mark - UICollectionViewDataSource |
| 205 |
| 206 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 207 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 208 UICollectionViewCell* cell = |
| 209 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 210 |
| 211 AutofillEditCell* textFieldCell = |
| 212 base::mac::ObjCCastStrict<AutofillEditCell>(cell); |
| 213 NSUInteger index = |
| 214 [self.collectionViewModel indexInItemTypeForIndexPath:indexPath]; |
| 215 const AutofillFieldDisplayInfo& field = kFieldsToDisplay[index]; |
| 216 textFieldCell.accessibilityIdentifier = textFieldCell.textLabel.text; |
| 217 textFieldCell.textField.autocapitalizationType = field.autoCapitalizationType; |
| 218 textFieldCell.textField.returnKeyType = field.returnKeyType; |
| 219 textFieldCell.textField.keyboardType = field.keyboardType; |
| 220 textFieldCell.textField.delegate = self; |
| 221 return textFieldCell; |
| 222 } |
| 223 |
| 224 #pragma mark - UICollectionViewDelegate |
| 225 |
| 226 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 227 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 228 if (self.editor.editing) { |
| 229 UICollectionViewCell* cell = |
| 230 [self.collectionView cellForItemAtIndexPath:indexPath]; |
| 231 AutofillEditCell* textFieldCell = |
| 232 base::mac::ObjCCastStrict<AutofillEditCell>(cell); |
| 233 [textFieldCell.textField becomeFirstResponder]; |
| 234 } |
| 235 return [super collectionView:collectionView |
| 236 shouldSelectItemAtIndexPath:indexPath]; |
| 237 } |
| 238 |
| 239 @end |
OLD | NEW |