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_collection_view_controller.h" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/foundation_util.h" |
| 9 #include "base/mac/objc_property_releaser.h" |
| 10 #include "base/mac/scoped_nsobject.h" |
| 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "components/autofill/core/browser/personal_data_manager.h" |
| 13 #include "components/autofill/core/common/autofill_pref_names.h" |
| 14 #include "components/autofill/ios/browser/credit_card_util.h" |
| 15 #import "components/autofill/ios/browser/personal_data_manager_observer_bridge.h
" |
| 16 #include "components/prefs/pref_service.h" |
| 17 #include "ios/chrome/browser/application_context.h" |
| 18 #include "ios/chrome/browser/autofill/personal_data_manager_factory.h" |
| 19 #include "ios/chrome/browser/browser_state/chrome_browser_state.h" |
| 20 #import "ios/chrome/browser/ui/collection_view/cells/MDCCollectionViewCell+Chrom
e.h" |
| 21 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_switch_item
.h" |
| 22 #import "ios/chrome/browser/ui/collection_view/cells/collection_view_text_item.h
" |
| 23 #import "ios/chrome/browser/ui/collection_view/collection_view_model.h" |
| 24 #import "ios/chrome/browser/ui/settings/autofill_credit_card_edit_collection_vie
w_controller.h" |
| 25 #import "ios/chrome/browser/ui/settings/autofill_profile_edit_collection_view_co
ntroller.h" |
| 26 #import "ios/chrome/browser/ui/settings/cells/autofill_data_item.h" |
| 27 #include "ios/chrome/grit/ios_strings.h" |
| 28 #import "ios/third_party/material_components_ios/src/components/Palettes/src/Mat
erialPalettes.h" |
| 29 #include "ui/base/l10n/l10n_util.h" |
| 30 |
| 31 namespace { |
| 32 |
| 33 typedef NS_ENUM(NSInteger, SectionIdentifier) { |
| 34 SectionIdentifierSwitches = kSectionIdentifierEnumZero, |
| 35 SectionIdentifierProfiles, |
| 36 SectionIdentifierCards, |
| 37 }; |
| 38 |
| 39 typedef NS_ENUM(NSInteger, ItemType) { |
| 40 ItemTypeAutofillSwitch = kItemTypeEnumZero, |
| 41 ItemTypeWalletSwitch, |
| 42 ItemTypeAddress, |
| 43 ItemTypeCard, |
| 44 ItemTypeHeader, |
| 45 }; |
| 46 |
| 47 } // namespace |
| 48 |
| 49 #pragma mark - AutofillCollectionViewController |
| 50 |
| 51 @interface AutofillCollectionViewController ()< |
| 52 PersonalDataManagerObserverBridgeDelegate> { |
| 53 std::string _locale; // User locale. |
| 54 autofill::PersonalDataManager* _personalDataManager; |
| 55 base::mac::ObjCPropertyReleaser |
| 56 _propertyReleaser_AutofillCollectionViewController; |
| 57 ios::ChromeBrowserState* _browserState; |
| 58 std::unique_ptr<autofill::PersonalDataManagerObserverBridge> _observer; |
| 59 BOOL _deletionInProgress; |
| 60 |
| 61 // Writing user-initiated switch state changes to the pref service results in |
| 62 // an observer callback, which handles general data updates with a reloadData. |
| 63 // It is better to handle user-initiated changes with more specific actions |
| 64 // such as inserting or removing items/sections. This boolean is used to |
| 65 // stop the observer callback from acting on user-initiated changes. |
| 66 BOOL _userInteractionInProgress; |
| 67 } |
| 68 |
| 69 @end |
| 70 |
| 71 @implementation AutofillCollectionViewController |
| 72 |
| 73 - (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState { |
| 74 DCHECK(browserState); |
| 75 self = [super initWithStyle:CollectionViewControllerStyleAppBar]; |
| 76 if (self) { |
| 77 self.collectionViewAccessibilityIdentifier = @"kAutofillCollectionViewId"; |
| 78 self.title = l10n_util::GetNSString(IDS_IOS_AUTOFILL); |
| 79 self.shouldHideDoneButton = YES; |
| 80 _browserState = browserState; |
| 81 _locale = GetApplicationContext()->GetApplicationLocale(); |
| 82 _personalDataManager = |
| 83 autofill::PersonalDataManagerFactory::GetForBrowserState(_browserState); |
| 84 _observer.reset(new autofill::PersonalDataManagerObserverBridge(self)); |
| 85 _personalDataManager->AddObserver(_observer.get()); |
| 86 |
| 87 [self updateEditButton]; |
| 88 [self loadModel]; |
| 89 |
| 90 _propertyReleaser_AutofillCollectionViewController.Init( |
| 91 self, [AutofillCollectionViewController class]); |
| 92 } |
| 93 return self; |
| 94 } |
| 95 |
| 96 - (void)dealloc { |
| 97 _personalDataManager->RemoveObserver(_observer.get()); |
| 98 [super dealloc]; |
| 99 } |
| 100 |
| 101 #pragma mark - CollectionViewController |
| 102 |
| 103 - (void)loadModel { |
| 104 [super loadModel]; |
| 105 CollectionViewModel* model = self.collectionViewModel; |
| 106 |
| 107 [model addSectionWithIdentifier:SectionIdentifierSwitches]; |
| 108 [model addItem:[self autofillSwitchItem] |
| 109 toSectionWithIdentifier:SectionIdentifierSwitches]; |
| 110 |
| 111 if ([self isAutofillEnabled]) { |
| 112 [model addItem:[self walletSwitchItem] |
| 113 toSectionWithIdentifier:SectionIdentifierSwitches]; |
| 114 |
| 115 [self populateProfileSection]; |
| 116 [self populateCardSection]; |
| 117 } |
| 118 } |
| 119 |
| 120 #pragma mark - LoadModel Helpers |
| 121 |
| 122 // Populates profile section using personalDataManager. |
| 123 - (void)populateProfileSection { |
| 124 CollectionViewModel* model = self.collectionViewModel; |
| 125 const std::vector<autofill::AutofillProfile*> autofillProfiles = |
| 126 _personalDataManager->GetProfiles(); |
| 127 if (!autofillProfiles.empty()) { |
| 128 [model addSectionWithIdentifier:SectionIdentifierProfiles]; |
| 129 [model setHeader:[self profileSectionHeader] |
| 130 forSectionWithIdentifier:SectionIdentifierProfiles]; |
| 131 for (autofill::AutofillProfile* autofillProfile : autofillProfiles) { |
| 132 DCHECK(autofillProfile); |
| 133 [model addItem:[self itemForProfile:*autofillProfile] |
| 134 toSectionWithIdentifier:SectionIdentifierProfiles]; |
| 135 } |
| 136 } |
| 137 } |
| 138 |
| 139 // Populates card section using personalDataManager. |
| 140 - (void)populateCardSection { |
| 141 CollectionViewModel* model = self.collectionViewModel; |
| 142 const std::vector<autofill::CreditCard*>& creditCards = |
| 143 _personalDataManager->GetCreditCards(); |
| 144 if (!creditCards.empty()) { |
| 145 [model addSectionWithIdentifier:SectionIdentifierCards]; |
| 146 [model setHeader:[self cardSectionHeader] |
| 147 forSectionWithIdentifier:SectionIdentifierCards]; |
| 148 for (autofill::CreditCard* creditCard : creditCards) { |
| 149 DCHECK(creditCard); |
| 150 [model addItem:[self itemForCreditCard:*creditCard] |
| 151 toSectionWithIdentifier:SectionIdentifierCards]; |
| 152 } |
| 153 } |
| 154 } |
| 155 |
| 156 - (CollectionViewItem*)autofillSwitchItem { |
| 157 CollectionViewSwitchItem* switchItem = [[[CollectionViewSwitchItem alloc] |
| 158 initWithType:ItemTypeAutofillSwitch] autorelease]; |
| 159 switchItem.text = l10n_util::GetNSString(IDS_IOS_AUTOFILL); |
| 160 switchItem.on = [self isAutofillEnabled]; |
| 161 return switchItem; |
| 162 } |
| 163 |
| 164 - (CollectionViewItem*)walletSwitchItem { |
| 165 CollectionViewSwitchItem* switchItem = [[[CollectionViewSwitchItem alloc] |
| 166 initWithType:ItemTypeWalletSwitch] autorelease]; |
| 167 switchItem.text = l10n_util::GetNSString(IDS_IOS_AUTOFILL_USE_WALLET_DATA); |
| 168 switchItem.on = [self isWalletEnabled]; |
| 169 return switchItem; |
| 170 } |
| 171 |
| 172 - (CollectionViewItem*)profileSectionHeader { |
| 173 CollectionViewTextItem* header = [ |
| 174 [[CollectionViewTextItem alloc] initWithType:ItemTypeHeader] autorelease]; |
| 175 header.text = l10n_util::GetNSString(IDS_IOS_AUTOFILL_ADDRESSES_GROUP_NAME); |
| 176 return header; |
| 177 } |
| 178 |
| 179 - (CollectionViewItem*)cardSectionHeader { |
| 180 CollectionViewTextItem* header = [ |
| 181 [[CollectionViewTextItem alloc] initWithType:ItemTypeHeader] autorelease]; |
| 182 header.text = l10n_util::GetNSString(IDS_IOS_AUTOFILL_CREDITCARDS_GROUP_NAME); |
| 183 return header; |
| 184 } |
| 185 |
| 186 - (CollectionViewItem*)itemForProfile: |
| 187 (const autofill::AutofillProfile&)autofillProfile { |
| 188 std::string guid(autofillProfile.guid()); |
| 189 NSString* title = base::SysUTF16ToNSString(autofillProfile.GetInfo( |
| 190 autofill::AutofillType(autofill::NAME_FULL), _locale)); |
| 191 NSString* subTitle = base::SysUTF16ToNSString(autofillProfile.GetInfo( |
| 192 autofill::AutofillType(autofill::ADDRESS_HOME_LINE1), _locale)); |
| 193 bool isServerProfile = autofillProfile.record_type() == |
| 194 autofill::AutofillProfile::SERVER_PROFILE; |
| 195 |
| 196 AutofillDataItem* item = |
| 197 [[[AutofillDataItem alloc] initWithType:ItemTypeAddress] autorelease]; |
| 198 item.text = title; |
| 199 item.leadingDetailText = subTitle; |
| 200 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 201 item.accessibilityIdentifier = title; |
| 202 item.GUID = guid; |
| 203 item.deletable = !isServerProfile; |
| 204 if (isServerProfile) { |
| 205 item.trailingDetailText = |
| 206 l10n_util::GetNSString(IDS_IOS_AUTOFILL_WALLET_SERVER_NAME); |
| 207 } |
| 208 return item; |
| 209 } |
| 210 |
| 211 - (CollectionViewItem*)itemForCreditCard: |
| 212 (const autofill::CreditCard&)creditCard { |
| 213 std::string guid(creditCard.guid()); |
| 214 NSString* creditCardName = autofill::GetCreditCardName(creditCard, _locale); |
| 215 |
| 216 AutofillDataItem* item = |
| 217 [[[AutofillDataItem alloc] initWithType:ItemTypeCard] autorelease]; |
| 218 item.text = creditCardName; |
| 219 item.leadingDetailText = autofill::GetCreditCardObfuscatedNumber(creditCard); |
| 220 item.accessoryType = MDCCollectionViewCellAccessoryDisclosureIndicator; |
| 221 item.accessibilityIdentifier = creditCardName; |
| 222 item.deletable = autofill::IsCreditCardLocal(creditCard); |
| 223 item.GUID = guid; |
| 224 if (![item isDeletable]) { |
| 225 item.trailingDetailText = |
| 226 l10n_util::GetNSString(IDS_IOS_AUTOFILL_WALLET_SERVER_NAME); |
| 227 } |
| 228 return item; |
| 229 } |
| 230 |
| 231 - (BOOL)localProfilesOrCreditCardsExist { |
| 232 return !_personalDataManager->web_profiles().empty() || |
| 233 !_personalDataManager->GetLocalCreditCards().empty(); |
| 234 } |
| 235 |
| 236 #pragma mark - SettingsRootCollectionViewController |
| 237 |
| 238 - (BOOL)shouldShowEditButton { |
| 239 return [self isAutofillEnabled]; |
| 240 } |
| 241 |
| 242 - (BOOL)editButtonEnabled { |
| 243 return [self localProfilesOrCreditCardsExist]; |
| 244 } |
| 245 |
| 246 #pragma mark - UICollectionViewDataSource |
| 247 |
| 248 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
| 249 cellForItemAtIndexPath:(NSIndexPath*)indexPath { |
| 250 UICollectionViewCell* cell = |
| 251 [super collectionView:collectionView cellForItemAtIndexPath:indexPath]; |
| 252 |
| 253 ItemType itemType = static_cast<ItemType>( |
| 254 [self.collectionViewModel itemTypeForIndexPath:indexPath]); |
| 255 |
| 256 if (itemType == ItemTypeAutofillSwitch) { |
| 257 CollectionViewSwitchCell* switchCell = |
| 258 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 259 [switchCell.switchView addTarget:self |
| 260 action:@selector(autofillSwitchChanged:) |
| 261 forControlEvents:UIControlEventValueChanged]; |
| 262 } else if (itemType == ItemTypeWalletSwitch) { |
| 263 CollectionViewSwitchCell* switchCell = |
| 264 base::mac::ObjCCastStrict<CollectionViewSwitchCell>(cell); |
| 265 [switchCell.switchView addTarget:self |
| 266 action:@selector(walletSwitchChanged:) |
| 267 forControlEvents:UIControlEventValueChanged]; |
| 268 } |
| 269 return cell; |
| 270 } |
| 271 |
| 272 - (UICollectionReusableView*)collectionView:(UICollectionView*)collectionView |
| 273 viewForSupplementaryElementOfKind:(NSString*)kind |
| 274 atIndexPath:(NSIndexPath*)indexPath { |
| 275 UICollectionReusableView* view = [super collectionView:collectionView |
| 276 viewForSupplementaryElementOfKind:kind |
| 277 atIndexPath:indexPath]; |
| 278 MDCCollectionViewTextCell* textCell = |
| 279 base::mac::ObjCCast<MDCCollectionViewTextCell>(view); |
| 280 if (textCell) { |
| 281 textCell.textLabel.textColor = [[MDCPalette greyPalette] tint500]; |
| 282 } |
| 283 return view; |
| 284 } |
| 285 |
| 286 #pragma mark - Switch Callbacks |
| 287 |
| 288 - (void)autofillSwitchChanged:(UISwitch*)switchView { |
| 289 [self setSwitchItemOn:[switchView isOn] itemType:ItemTypeAutofillSwitch]; |
| 290 _userInteractionInProgress = YES; |
| 291 [self setAutofillEnabled:[switchView isOn]]; |
| 292 _userInteractionInProgress = NO; |
| 293 [self updateEditButton]; |
| 294 |
| 295 // Avoid reference cycle in block. |
| 296 base::WeakNSObject<AutofillCollectionViewController> weakSelf(self); |
| 297 [self.collectionView performBatchUpdates:^{ |
| 298 // Obtain strong reference again. |
| 299 base::scoped_nsobject<AutofillCollectionViewController> strongSelf( |
| 300 [weakSelf retain]); |
| 301 if (!strongSelf) { |
| 302 return; |
| 303 } |
| 304 |
| 305 if ([switchView isOn]) { |
| 306 [strongSelf insertWalletSwitchItem]; |
| 307 [strongSelf insertProfileAndCardSections]; |
| 308 } else { |
| 309 [strongSelf removeWalletSwitchItem]; |
| 310 [strongSelf removeProfileAndCardSections]; |
| 311 } |
| 312 } |
| 313 completion:nil]; |
| 314 } |
| 315 |
| 316 - (void)walletSwitchChanged:(UISwitch*)switchView { |
| 317 [self setSwitchItemOn:[switchView isOn] itemType:ItemTypeWalletSwitch]; |
| 318 _userInteractionInProgress = YES; |
| 319 [self setWalletEnabled:[switchView isOn]]; |
| 320 _userInteractionInProgress = NO; |
| 321 if ([switchView isOn]) { |
| 322 [self insertCardSection]; |
| 323 } else { |
| 324 [self removeCardSection]; |
| 325 } |
| 326 } |
| 327 |
| 328 #pragma mark - Switch Helpers |
| 329 |
| 330 // Sets switchItem's state to |on|. It is important that there is only one item |
| 331 // of |switchItemType| in SectionIdentifierSwitches. |
| 332 - (void)setSwitchItemOn:(BOOL)on itemType:(ItemType)switchItemType { |
| 333 NSIndexPath* switchPath = |
| 334 [self.collectionViewModel indexPathForItemType:switchItemType |
| 335 sectionIdentifier:SectionIdentifierSwitches]; |
| 336 CollectionViewSwitchItem* switchItem = |
| 337 base::mac::ObjCCastStrict<CollectionViewSwitchItem>( |
| 338 [self.collectionViewModel itemAtIndexPath:switchPath]); |
| 339 switchItem.on = on; |
| 340 } |
| 341 |
| 342 #pragma mark - Insert or Delete Items and Sections |
| 343 |
| 344 - (void)insertWalletSwitchItem { |
| 345 CollectionViewModel* model = self.collectionViewModel; |
| 346 [model addItem:[self walletSwitchItem] |
| 347 toSectionWithIdentifier:SectionIdentifierSwitches]; |
| 348 NSIndexPath* indexPath = |
| 349 [self.collectionViewModel indexPathForItemType:ItemTypeWalletSwitch |
| 350 sectionIdentifier:SectionIdentifierSwitches]; |
| 351 [self.collectionView insertItemsAtIndexPaths:@[ indexPath ]]; |
| 352 } |
| 353 |
| 354 - (void)removeWalletSwitchItem { |
| 355 if (![self.collectionViewModel |
| 356 hasItemForItemType:ItemTypeWalletSwitch |
| 357 sectionIdentifier:SectionIdentifierSwitches]) { |
| 358 return; |
| 359 } |
| 360 NSIndexPath* indexPath = |
| 361 [self.collectionViewModel indexPathForItemType:ItemTypeWalletSwitch |
| 362 sectionIdentifier:SectionIdentifierSwitches]; |
| 363 [self.collectionViewModel removeItemWithType:ItemTypeWalletSwitch |
| 364 fromSectionWithIdentifier:SectionIdentifierSwitches]; |
| 365 [self.collectionView deleteItemsAtIndexPaths:@[ indexPath ]]; |
| 366 } |
| 367 |
| 368 - (void)insertProfileAndCardSections { |
| 369 [self populateProfileSection]; |
| 370 [self populateCardSection]; |
| 371 NSIndexSet* sections = [self indexSetForExistingProfileAndCardSections]; |
| 372 [self.collectionView insertSections:sections]; |
| 373 } |
| 374 |
| 375 - (void)removeProfileAndCardSections { |
| 376 // It is important to build the section indexSet before removing sections. |
| 377 NSIndexSet* sections = [self indexSetForExistingProfileAndCardSections]; |
| 378 if ([self.collectionViewModel |
| 379 hasSectionForSectionIdentifier:SectionIdentifierProfiles]) { |
| 380 [self.collectionViewModel |
| 381 removeSectionWithIdentifier:SectionIdentifierProfiles]; |
| 382 } |
| 383 if ([self.collectionViewModel |
| 384 hasSectionForSectionIdentifier:SectionIdentifierCards]) { |
| 385 [self.collectionViewModel |
| 386 removeSectionWithIdentifier:SectionIdentifierCards]; |
| 387 } |
| 388 [self.collectionView deleteSections:sections]; |
| 389 } |
| 390 |
| 391 - (NSIndexSet*)indexSetForExistingProfileAndCardSections { |
| 392 NSMutableIndexSet* sections = [[[NSMutableIndexSet alloc] init] autorelease]; |
| 393 if ([self.collectionViewModel |
| 394 hasSectionForSectionIdentifier:SectionIdentifierProfiles]) { |
| 395 [sections |
| 396 addIndex:[self.collectionViewModel |
| 397 sectionForSectionIdentifier:SectionIdentifierProfiles]]; |
| 398 } |
| 399 if ([self.collectionViewModel |
| 400 hasSectionForSectionIdentifier:SectionIdentifierCards]) { |
| 401 [sections addIndex:[self.collectionViewModel |
| 402 sectionForSectionIdentifier:SectionIdentifierCards]]; |
| 403 } |
| 404 return sections; |
| 405 } |
| 406 |
| 407 - (void)insertCardSection { |
| 408 [self populateCardSection]; |
| 409 if ([self.collectionViewModel |
| 410 hasSectionForSectionIdentifier:SectionIdentifierCards]) { |
| 411 NSInteger section = [self.collectionViewModel |
| 412 sectionForSectionIdentifier:SectionIdentifierCards]; |
| 413 [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:section]]; |
| 414 } |
| 415 } |
| 416 |
| 417 - (void)removeCardSection { |
| 418 if (![self.collectionViewModel |
| 419 hasSectionForSectionIdentifier:SectionIdentifierCards]) { |
| 420 return; |
| 421 } |
| 422 NSInteger section = [self.collectionViewModel |
| 423 sectionForSectionIdentifier:SectionIdentifierCards]; |
| 424 [self.collectionViewModel removeSectionWithIdentifier:SectionIdentifierCards]; |
| 425 [self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:section]]; |
| 426 } |
| 427 |
| 428 #pragma mark - MDCCollectionViewStylingDelegate |
| 429 |
| 430 - (CGFloat)collectionView:(UICollectionView*)collectionView |
| 431 cellHeightAtIndexPath:(NSIndexPath*)indexPath { |
| 432 CollectionViewItem* item = |
| 433 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 434 if (item.type == ItemTypeAddress || item.type == ItemTypeCard || |
| 435 item.type == ItemTypeWalletSwitch) { |
| 436 return [MDCCollectionViewCell |
| 437 cr_preferredHeightForWidth:CGRectGetWidth(collectionView.bounds) |
| 438 forItem:item]; |
| 439 } |
| 440 return MDCCellDefaultOneLineHeight; |
| 441 } |
| 442 |
| 443 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 444 hidesInkViewAtIndexPath:(NSIndexPath*)indexPath { |
| 445 NSInteger type = [self.collectionViewModel itemTypeForIndexPath:indexPath]; |
| 446 switch (type) { |
| 447 case ItemTypeAutofillSwitch: |
| 448 case ItemTypeWalletSwitch: |
| 449 return YES; |
| 450 default: |
| 451 return NO; |
| 452 } |
| 453 } |
| 454 |
| 455 #pragma mark - UICollectionViewDelegate |
| 456 |
| 457 - (void)collectionView:(UICollectionView*)collectionView |
| 458 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 459 [super collectionView:collectionView didSelectItemAtIndexPath:indexPath]; |
| 460 |
| 461 // Edit mode is the state where the user can select and delete entries. In |
| 462 // edit mode, selection is handled by the superclass. When not in edit mode |
| 463 // selection presents the editing controller for the selected entry. |
| 464 if ([self.editor isEditing]) { |
| 465 return; |
| 466 } |
| 467 |
| 468 CollectionViewModel* model = self.collectionViewModel; |
| 469 base::scoped_nsobject<UIViewController> controller; |
| 470 switch ([model itemTypeForIndexPath:indexPath]) { |
| 471 case ItemTypeAddress: { |
| 472 const std::vector<autofill::AutofillProfile*> autofillProfiles = |
| 473 _personalDataManager->GetProfiles(); |
| 474 controller.reset([[AutofillProfileEditCollectionViewController |
| 475 controllerWithProfile:*autofillProfiles[indexPath.item] |
| 476 personalDataManager:_personalDataManager] retain]); |
| 477 break; |
| 478 } |
| 479 case ItemTypeCard: { |
| 480 const std::vector<autofill::CreditCard*>& creditCards = |
| 481 _personalDataManager->GetCreditCards(); |
| 482 controller.reset([[AutofillCreditCardEditCollectionViewController alloc] |
| 483 initWithCreditCard:*creditCards[indexPath.item] |
| 484 personalDataManager:_personalDataManager]); |
| 485 break; |
| 486 } |
| 487 default: |
| 488 break; |
| 489 } |
| 490 |
| 491 if (controller.get()) { |
| 492 [self.navigationController pushViewController:controller animated:YES]; |
| 493 } |
| 494 } |
| 495 |
| 496 #pragma mark - MDCCollectionViewEditingDelegate |
| 497 |
| 498 - (BOOL)collectionViewAllowsEditing:(UICollectionView*)collectionView { |
| 499 return YES; |
| 500 } |
| 501 |
| 502 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 503 canEditItemAtIndexPath:(NSIndexPath*)indexPath { |
| 504 // Only autofill data cells are editable. |
| 505 CollectionViewItem* item = |
| 506 [self.collectionViewModel itemAtIndexPath:indexPath]; |
| 507 if ([item isKindOfClass:[AutofillDataItem class]]) { |
| 508 AutofillDataItem* autofillItem = |
| 509 base::mac::ObjCCastStrict<AutofillDataItem>(item); |
| 510 return [autofillItem isDeletable]; |
| 511 } |
| 512 return NO; |
| 513 } |
| 514 |
| 515 - (void)collectionView:(UICollectionView*)collectionView |
| 516 willDeleteItemsAtIndexPaths:(NSArray*)indexPaths { |
| 517 _deletionInProgress = YES; |
| 518 for (NSIndexPath* indexPath in indexPaths) { |
| 519 AutofillDataItem* item = base::mac::ObjCCastStrict<AutofillDataItem>( |
| 520 [self.collectionViewModel itemAtIndexPath:indexPath]); |
| 521 _personalDataManager->RemoveByGUID([item GUID]); |
| 522 } |
| 523 // Must call super at the end of the child implementation. |
| 524 [super collectionView:collectionView willDeleteItemsAtIndexPaths:indexPaths]; |
| 525 } |
| 526 |
| 527 - (void)collectionView:(UICollectionView*)collectionView |
| 528 didDeleteItemsAtIndexPaths:(NSArray*)indexPaths { |
| 529 // If there are no index paths, return early. This can happen if the user |
| 530 // presses the Delete button twice in quick succession. |
| 531 if (![indexPaths count]) |
| 532 return; |
| 533 |
| 534 // TODO(crbug.com/650390) Generalize removing empty sections |
| 535 [self removeSectionIfEmptyForSectionWithIdentifier:SectionIdentifierProfiles]; |
| 536 [self removeSectionIfEmptyForSectionWithIdentifier:SectionIdentifierCards]; |
| 537 } |
| 538 |
| 539 // Remove the section from the model and collectionView if there are no more |
| 540 // items in the section. |
| 541 - (void)removeSectionIfEmptyForSectionWithIdentifier: |
| 542 (SectionIdentifier)sectionIdentifier { |
| 543 if (![self.collectionViewModel |
| 544 hasSectionForSectionIdentifier:sectionIdentifier]) { |
| 545 return; |
| 546 } |
| 547 NSInteger section = |
| 548 [self.collectionViewModel sectionForSectionIdentifier:sectionIdentifier]; |
| 549 if ([self.collectionView numberOfItemsInSection:section] == 0) { |
| 550 // Avoid reference cycle in block. |
| 551 base::WeakNSObject<AutofillCollectionViewController> weakSelf(self); |
| 552 [self.collectionView performBatchUpdates:^{ |
| 553 // Obtain strong reference again. |
| 554 base::scoped_nsobject<AutofillCollectionViewController> strongSelf( |
| 555 [weakSelf retain]); |
| 556 if (!strongSelf) { |
| 557 return; |
| 558 } |
| 559 |
| 560 // Remove section from model and collectionView. |
| 561 [[strongSelf collectionViewModel] |
| 562 removeSectionWithIdentifier:sectionIdentifier]; |
| 563 [[strongSelf collectionView] |
| 564 deleteSections:[NSIndexSet indexSetWithIndex:section]]; |
| 565 } |
| 566 completion:^(BOOL finished) { |
| 567 // Obtain strong reference again. |
| 568 base::scoped_nsobject<AutofillCollectionViewController> strongSelf( |
| 569 [weakSelf retain]); |
| 570 if (!strongSelf) { |
| 571 return; |
| 572 } |
| 573 |
| 574 // Turn off edit mode if there is nothing to edit. |
| 575 if (![strongSelf localProfilesOrCreditCardsExist]) { |
| 576 [[strongSelf editor] setEditing:NO]; |
| 577 } |
| 578 [strongSelf updateEditButton]; |
| 579 strongSelf.get()->_deletionInProgress = NO; |
| 580 }]; |
| 581 } |
| 582 } |
| 583 |
| 584 #pragma mark PersonalDataManagerObserverBridgeDelegate |
| 585 |
| 586 - (void)onPersonalDataChanged { |
| 587 // If the change is due to local editing, or if local editing is happening |
| 588 // concurrently, updates are handled by collection view editing callbacks. |
| 589 // Data is reloaded at the end of deletion to make sure entries are in sync. |
| 590 if (_deletionInProgress) |
| 591 return; |
| 592 |
| 593 // If the change is due to user-initiated switch state changes, updates |
| 594 // are handled by the switch callbacks. |
| 595 if (_userInteractionInProgress) |
| 596 return; |
| 597 |
| 598 if (![self localProfilesOrCreditCardsExist]) { |
| 599 // Turn off edit mode if there exists nothing to edit. |
| 600 [self.editor setEditing:NO]; |
| 601 } |
| 602 |
| 603 [self updateEditButton]; |
| 604 [self reloadData]; |
| 605 } |
| 606 |
| 607 #pragma mark - Pref Helpers |
| 608 |
| 609 - (BOOL)isAutofillEnabled { |
| 610 return _browserState->GetPrefs()->GetBoolean( |
| 611 autofill::prefs::kAutofillEnabled); |
| 612 } |
| 613 |
| 614 - (void)setAutofillEnabled:(BOOL)isEnabled { |
| 615 _browserState->GetPrefs()->SetBoolean(autofill::prefs::kAutofillEnabled, |
| 616 isEnabled); |
| 617 } |
| 618 |
| 619 - (BOOL)isWalletEnabled { |
| 620 return _browserState->GetPrefs()->GetBoolean( |
| 621 autofill::prefs::kAutofillWalletImportEnabled); |
| 622 } |
| 623 |
| 624 - (void)setWalletEnabled:(BOOL)isEnabled { |
| 625 _browserState->GetPrefs()->SetBoolean( |
| 626 autofill::prefs::kAutofillWalletImportEnabled, isEnabled); |
| 627 } |
| 628 |
| 629 @end |
OLD | NEW |