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_edit_collection_view_controller
.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "base/mac/foundation_util.h" |
| 9 #import "base/mac/scoped_nsobject.h" |
| 10 #import "ios/chrome/browser/ui/settings/autofill_edit_accessory_view.h" |
| 11 #import "ios/chrome/browser/ui/settings/cells/autofill_edit_item.h" |
| 12 #import "ios/third_party/material_components_ios/src/components/CollectionCells/
src/MaterialCollectionCells.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 AutofillEditCell* AutofillEditCellForTextField(UITextField* textField) { |
| 17 AutofillEditCell* settingsCell = nil; |
| 18 for (UIView* view = textField; view; view = [view superview]) { |
| 19 AutofillEditCell* cell = base::mac::ObjCCast<AutofillEditCell>(view); |
| 20 if (cell) { |
| 21 settingsCell = cell; |
| 22 break; |
| 23 } |
| 24 } |
| 25 |
| 26 // There should be a cell associated with this text field. |
| 27 DCHECK(settingsCell); |
| 28 return settingsCell; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 @interface AutofillEditCollectionViewController ()< |
| 34 AutofillEditAccessoryDelegate> { |
| 35 base::scoped_nsobject<AutofillEditCell> _currentEditingCell; |
| 36 base::scoped_nsobject<AutofillEditAccessoryView> _accessoryView; |
| 37 } |
| 38 @end |
| 39 |
| 40 @implementation AutofillEditCollectionViewController |
| 41 |
| 42 - (instancetype)initWithStyle:(CollectionViewControllerStyle)style { |
| 43 self = [super initWithStyle:style]; |
| 44 if (!self) { |
| 45 return nil; |
| 46 } |
| 47 |
| 48 _accessoryView.reset( |
| 49 [[AutofillEditAccessoryView alloc] initWithDelegate:self]); |
| 50 [self setShouldHideDoneButton:YES]; |
| 51 [self updateEditButton]; |
| 52 return self; |
| 53 } |
| 54 |
| 55 - (void)viewDidAppear:(BOOL)animated { |
| 56 [super viewDidAppear:animated]; |
| 57 [[NSNotificationCenter defaultCenter] |
| 58 addObserver:self |
| 59 selector:@selector(keyboardDidShow) |
| 60 name:UIKeyboardDidShowNotification |
| 61 object:nil]; |
| 62 } |
| 63 |
| 64 - (void)viewWillDisappear:(BOOL)animated { |
| 65 [super viewWillDisappear:animated]; |
| 66 [[NSNotificationCenter defaultCenter] |
| 67 removeObserver:self |
| 68 name:UIKeyboardDidShowNotification |
| 69 object:nil]; |
| 70 } |
| 71 |
| 72 - (void)dealloc { |
| 73 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 74 [super dealloc]; |
| 75 } |
| 76 |
| 77 #pragma mark - SettingsRootCollectionViewController |
| 78 |
| 79 - (BOOL)shouldShowEditButton { |
| 80 return YES; |
| 81 } |
| 82 |
| 83 - (BOOL)editButtonEnabled { |
| 84 return YES; |
| 85 } |
| 86 |
| 87 - (NSIndexPath*)indexForCellPathWithOffset:(NSInteger)offset |
| 88 fromPath:(NSIndexPath*)cellPath { |
| 89 if (!cellPath || !offset) |
| 90 return nil; |
| 91 |
| 92 NSInteger cellSection = [cellPath section]; |
| 93 NSInteger nextCellRow = [cellPath row] + offset; |
| 94 |
| 95 if (nextCellRow >= 0 && |
| 96 nextCellRow < [[self collectionView] numberOfItemsInSection:cellSection]) |
| 97 return [NSIndexPath indexPathForRow:nextCellRow inSection:cellSection]; |
| 98 |
| 99 NSInteger nextCellSection = cellSection + offset; |
| 100 if (nextCellSection >= 0 && |
| 101 nextCellSection < [[self collectionView] numberOfSections]) |
| 102 return [NSIndexPath indexPathForRow:0 inSection:nextCellSection]; |
| 103 |
| 104 return nil; |
| 105 } |
| 106 |
| 107 #pragma mark - UITextFieldDelegate |
| 108 |
| 109 - (void)textFieldDidBeginEditing:(UITextField*)textField { |
| 110 AutofillEditCell* cell = AutofillEditCellForTextField(textField); |
| 111 _currentEditingCell.reset([cell retain]); |
| 112 [textField setInputAccessoryView:_accessoryView]; |
| 113 [self updateAccessoryViewButtonState]; |
| 114 } |
| 115 |
| 116 - (void)textFieldDidEndEditing:(UITextField*)textField { |
| 117 AutofillEditCell* cell = AutofillEditCellForTextField(textField); |
| 118 DCHECK(_currentEditingCell == cell); |
| 119 [textField setInputAccessoryView:nil]; |
| 120 _currentEditingCell.reset(nil); |
| 121 } |
| 122 |
| 123 - (BOOL)textFieldShouldReturn:(UITextField*)textField { |
| 124 DCHECK([_currentEditingCell textField] == textField); |
| 125 [self nextPressed]; |
| 126 return NO; |
| 127 } |
| 128 |
| 129 #pragma mark - AutofillEditAccessoryDelegate |
| 130 |
| 131 - (NSIndexPath*)indexPathForCurrentTextField { |
| 132 DCHECK(_currentEditingCell); |
| 133 return [[self collectionView] indexPathForCell:_currentEditingCell]; |
| 134 } |
| 135 |
| 136 - (void)moveToAnotherCellWithOffset:(NSInteger)offset { |
| 137 UICollectionView* collectionView = [self collectionView]; |
| 138 NSIndexPath* cellPath = [self indexPathForCurrentTextField]; |
| 139 DCHECK(cellPath); |
| 140 NSIndexPath* nextCellPath = |
| 141 [self indexForCellPathWithOffset:offset fromPath:cellPath]; |
| 142 |
| 143 if (!nextCellPath) { |
| 144 [[_currentEditingCell textField] resignFirstResponder]; |
| 145 } else { |
| 146 AutofillEditCell* nextCell = base::mac::ObjCCastStrict<AutofillEditCell>( |
| 147 [collectionView cellForItemAtIndexPath:nextCellPath]); |
| 148 [nextCell.textField becomeFirstResponder]; |
| 149 } |
| 150 } |
| 151 |
| 152 - (void)nextPressed { |
| 153 [self moveToAnotherCellWithOffset:1]; |
| 154 } |
| 155 |
| 156 - (void)previousPressed { |
| 157 [self moveToAnotherCellWithOffset:-1]; |
| 158 } |
| 159 |
| 160 - (void)closePressed { |
| 161 [[_currentEditingCell textField] resignFirstResponder]; |
| 162 } |
| 163 |
| 164 - (void)updateAccessoryViewButtonState { |
| 165 NSIndexPath* currentPath = [self indexPathForCurrentTextField]; |
| 166 NSIndexPath* nextPath = |
| 167 [self indexForCellPathWithOffset:1 fromPath:currentPath]; |
| 168 NSIndexPath* previousPath = |
| 169 [self indexForCellPathWithOffset:-1 fromPath:currentPath]; |
| 170 |
| 171 [[_accessoryView previousButton] setEnabled:previousPath != nil]; |
| 172 [[_accessoryView nextButton] setEnabled:nextPath != nil]; |
| 173 } |
| 174 |
| 175 #pragma mark - Keyboard handling |
| 176 |
| 177 - (void)keyboardDidShow { |
| 178 [self.collectionView scrollRectToVisible:[_currentEditingCell frame] |
| 179 animated:YES]; |
| 180 } |
| 181 |
| 182 @end |
OLD | NEW |