Commit b269a4b2 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Add delete in SettingsTableView's toolbar

This CL adds a toolbar with a "delete" item in the
SettingsRootTableViewController, displayed when the item selection is
not nil.

Bug: 894791
Change-Id: Ifffc2b774ab96bad97814cdf80829f2f0b74d77a
Reviewed-on: https://chromium-review.googlesource.com/c/1331469
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607534}
parent a7854310
...@@ -1391,6 +1391,9 @@ Handoff must also be enabled in the General section of Settings, and your device ...@@ -1391,6 +1391,9 @@ Handoff must also be enabled in the General section of Settings, and your device
<message name="IDS_IOS_SETTING_ON" desc="Generic status label displayed in Settings to show that a setting is currently turned on. [Length: 5em] [iOS only]"> <message name="IDS_IOS_SETTING_ON" desc="Generic status label displayed in Settings to show that a setting is currently turned on. [Length: 5em] [iOS only]">
On On
</message> </message>
<message name="IDS_IOS_SETTINGS_TOOLBAR_DELETE" desc="Label of the button displayed on the bottom toolbar of the settings' collections when they are in edit mode. Pressing this button deletes the currently selected items.">
Delete
</message>
<message name="IDS_IOS_TOGGLE_SETTING_SWITCH_ACCESSIBILITY_HINT" desc="Action hint for any switch in settings. This is spoken by VoiceOver. [iOS only]"> <message name="IDS_IOS_TOGGLE_SETTING_SWITCH_ACCESSIBILITY_HINT" desc="Action hint for any switch in settings. This is spoken by VoiceOver. [iOS only]">
Double tap to toggle setting Double tap to toggle setting
</message> </message>
......
...@@ -50,6 +50,11 @@ ...@@ -50,6 +50,11 @@
// editing mode of this controller. // editing mode of this controller.
- (void)editButtonPressed; - (void)editButtonPressed;
// Called when this ViewController toolbar's delete item has been tapped.
// |indexPaths| is the index paths of the currently selected item to be deleted.
// Default implementation removes the items.
- (void)deleteItems:(NSArray<NSIndexPath*>*)indexPaths;
@end @end
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SETTINGS_ROOT_TABLE_VIEW_CONTROLLER_H_ #endif // IOS_CHROME_BROWSER_UI_SETTINGS_SETTINGS_ROOT_TABLE_VIEW_CONTROLLER_H_
...@@ -19,8 +19,16 @@ ...@@ -19,8 +19,16 @@
#error "This file requires ARC support." #error "This file requires ARC support."
#endif #endif
@interface SettingsRootTableViewController ()
// Delete button for the toolbar.
@property(nonatomic, strong) UIBarButtonItem* deleteButton;
@end
@implementation SettingsRootTableViewController @implementation SettingsRootTableViewController
@synthesize deleteButton = _deleteButton;
@synthesize dispatcher = _dispatcher; @synthesize dispatcher = _dispatcher;
#pragma mark - Public #pragma mark - Public
...@@ -35,8 +43,30 @@ ...@@ -35,8 +43,30 @@
} }
} }
#pragma mark - Property
- (UIBarButtonItem*)deleteButton {
if (!_deleteButton) {
_deleteButton = [[UIBarButtonItem alloc]
initWithTitle:l10n_util::GetNSString(IDS_IOS_SETTINGS_TOOLBAR_DELETE)
style:UIBarButtonItemStylePlain
target:self
action:@selector(deleteButtonCallback)];
_deleteButton.tintColor = [UIColor redColor];
}
return _deleteButton;
}
#pragma mark - UIViewController #pragma mark - UIViewController
- (NSArray<UIBarButtonItem*>*)toolbarItems {
UIBarButtonItem* flexibleSpace = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
return @[ flexibleSpace, self.deleteButton, flexibleSpace ];
}
- (void)viewDidLoad { - (void)viewDidLoad {
self.styler.tableViewBackgroundColor = self.styler.tableViewBackgroundColor =
[UIColor groupTableViewBackgroundColor]; [UIColor groupTableViewBackgroundColor];
...@@ -57,6 +87,31 @@ ...@@ -57,6 +87,31 @@
} }
} }
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[self.navigationController setToolbarHidden:YES animated:YES];
}
#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView*)tableView
didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
if (!self.tableView.editing)
return;
if (self.navigationController.toolbarHidden)
[self.navigationController setToolbarHidden:NO animated:YES];
}
- (void)tableView:(UITableView*)tableView
didDeselectRowAtIndexPath:(NSIndexPath*)indexPath {
if (!self.tableView.editing)
return;
if (self.tableView.indexPathsForSelectedRows.count == 0)
[self.navigationController setToolbarHidden:YES animated:YES];
}
#pragma mark - TableViewLinkHeaderFooterItemDelegate #pragma mark - TableViewLinkHeaderFooterItemDelegate
- (void)view:(TableViewLinkHeaderFooterView*)view didTapLinkURL:(GURL)URL { - (void)view:(TableViewLinkHeaderFooterView*)view didTapLinkURL:(GURL)URL {
...@@ -68,6 +123,10 @@ ...@@ -68,6 +123,10 @@
#pragma mark - Private #pragma mark - Private
- (void)deleteButtonCallback {
[self deleteItems:self.tableView.indexPathsForSelectedRows];
}
- (UIBarButtonItem*)doneButtonIfNeeded { - (UIBarButtonItem*)doneButtonIfNeeded {
if (self.shouldHideDoneButton) { if (self.shouldHideDoneButton) {
return nil; return nil;
...@@ -115,4 +174,13 @@ ...@@ -115,4 +174,13 @@
[self updateEditButton]; [self updateEditButton];
} }
- (void)deleteItems:(NSArray<NSIndexPath*>*)indexPaths {
[self.tableView performBatchUpdates:^{
[self removeFromModelItemAtIndexPaths:indexPaths];
[self.tableView deleteRowsAtIndexPaths:indexPaths
withRowAnimation:UITableViewRowAnimationAutomatic];
}
completion:nil];
}
@end @end
...@@ -74,3 +74,50 @@ TEST_F(SettingsRootTableViewControllerTest, TestUpdateEditButton) { ...@@ -74,3 +74,50 @@ TEST_F(SettingsRootTableViewControllerTest, TestUpdateEditButton) {
EXPECT_NSEQ(l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON), EXPECT_NSEQ(l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON),
controller.navigationItem.rightBarButtonItem.title); controller.navigationItem.rightBarButtonItem.title);
} }
// Tests that the delete button in the bottom toolbar is displayed only when the
// collection is being edited.
TEST_F(SettingsRootTableViewControllerTest, TestDeleteToolbar) {
SettingsRootTableViewController* controller = Controller();
id mockController = OCMPartialMock(controller);
id mockTableView = OCMPartialMock(controller.tableView);
SettingsNavigationController* navigationController = NavigationController();
OCMStub([mockController navigationController])
.andReturn(navigationController);
NSIndexPath* testIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
NSMutableArray* testSelectedItems = [NSMutableArray array];
[testSelectedItems addObject:testIndexPath];
ASSERT_TRUE(navigationController.toolbarHidden);
// Test that if the table view isn't being edited, the toolbar is still
// hidden.
controller.tableView.editing = NO;
[controller tableView:controller.tableView
didSelectRowAtIndexPath:testIndexPath];
EXPECT_TRUE(navigationController.toolbarHidden);
// Test that if the table view is being edited, the toolbar is displayed when
// the element is selected.
controller.tableView.editing = YES;
EXPECT_TRUE(navigationController.toolbarHidden);
[controller tableView:controller.tableView
didSelectRowAtIndexPath:testIndexPath];
EXPECT_FALSE(navigationController.toolbarHidden);
// Test that if the table view is being edited, the toolbar is not hidden when
// an element is deselected but some elements are still selected.
OCMStub([mockTableView indexPathsForSelectedRows])
.andReturn(testSelectedItems);
[controller tableView:controller.tableView
didDeselectRowAtIndexPath:testIndexPath];
EXPECT_FALSE(navigationController.toolbarHidden);
// Test that if the table view is being edited, the toolbar is hidden when the
// last selected element is deselected.
[testSelectedItems removeAllObjects];
[controller tableView:controller.tableView
didDeselectRowAtIndexPath:testIndexPath];
EXPECT_TRUE(navigationController.toolbarHidden);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment