Commit 519df6dd authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Update SettingsRootTableViewController

This CL adds features available to the collection view root panel to
the table view root panel in the settings.
More specifically it adds support for the top right button.
It also adds a helper to remove items from the model to the
ChromeTableViewController.

Bug: 894791
Change-Id: I1d0f4836f160379a0d8ab71e91f3a0ad13a38e5f
Reviewed-on: https://chromium-review.googlesource.com/c/1304437
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#607192}
parent 3e300603
......@@ -288,6 +288,7 @@ source_set("unit_tests") {
"search_engine_settings_collection_view_controller_unittest.mm",
"settings_navigation_controller_unittest.mm",
"settings_root_collection_view_controller_unittest.mm",
"settings_root_table_view_controller_unittest.mm",
"sync_create_passphrase_collection_view_controller_unittest.mm",
"sync_encryption_collection_view_controller_unittest.mm",
"sync_encryption_passphrase_collection_view_controller_unittest.mm",
......
......@@ -19,6 +19,37 @@
: ChromeTableViewController<AppBarViewControllerPresenting,
SettingsRootViewControlling,
TableViewLinkHeaderFooterItemDelegate>
// Whether this table view controller should hide the "Done" button (the right
// navigation bar button). Default is NO.
@property(nonatomic, assign) BOOL shouldHideDoneButton;
// Updates the edit or done button to reflect editing state. If the
// tableView is not in edit mode (and thus showing the 'Done' button) it is
// using shouldHideDoneButton to know if it should display the edit button.
- (void)updateEditButton;
@end
// Subclasses of SettingsRootTableViewController should implement the
// following methods to customize the behavior.
@interface SettingsRootTableViewController (Subclassing)
// Returns NO. Subclasses should overload this if an edit button is required.
- (BOOL)shouldShowEditButton;
// Returns NO. Subclasses should overload this if the edit button should be
// enabled.
- (BOOL)editButtonEnabled;
// Notifies the view controller that the edit button has been tapped. If you
// override this method, you must call -[super editButtonPressed] at some point
// in your implementation.
//
// Note that this method calls -[self setEditing:] in order to change the
// editing mode of this controller.
- (void)editButtonPressed;
@end
#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SETTINGS_ROOT_TABLE_VIEW_CONTROLLER_H_
......@@ -12,6 +12,8 @@
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/chrome/browser/ui/table_view/chrome_table_view_styler.h"
#import "ios/chrome/browser/ui/util/uikit_ui_util.h"
#include "ios/chrome/grit/ios_strings.h"
#include "ui/base/l10n/l10n_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -21,6 +23,20 @@
@synthesize dispatcher = _dispatcher;
#pragma mark - Public
- (void)updateEditButton {
if (self.tableView.editing) {
self.navigationItem.rightBarButtonItem = [self createEditModeDoneButton];
} else if (self.shouldShowEditButton) {
self.navigationItem.rightBarButtonItem = [self createEditButton];
} else {
self.navigationItem.rightBarButtonItem = [self doneButtonIfNeeded];
}
}
#pragma mark - UIViewController
- (void)viewDidLoad {
self.styler.tableViewBackgroundColor =
[UIColor groupTableViewBackgroundColor];
......@@ -35,13 +51,10 @@
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
// Set up the "Done" button in the upper right of the nav bar.
SettingsNavigationController* navigationController =
base::mac::ObjCCast<SettingsNavigationController>(
self.navigationController);
UIBarButtonItem* doneButton = [navigationController doneButton];
UIBarButtonItem* doneButton = [self doneButtonIfNeeded];
if (!self.navigationItem.rightBarButtonItem && doneButton) {
self.navigationItem.rightBarButtonItem = doneButton;
}
}
#pragma mark - TableViewLinkHeaderFooterItemDelegate
......@@ -53,4 +66,53 @@
[self.dispatcher closeSettingsUIAndOpenURL:command];
}
#pragma mark - Private
- (UIBarButtonItem*)doneButtonIfNeeded {
if (self.shouldHideDoneButton) {
return nil;
}
SettingsNavigationController* navigationController =
base::mac::ObjCCast<SettingsNavigationController>(
self.navigationController);
return [navigationController doneButton];
}
- (UIBarButtonItem*)createEditButton {
// Create a custom Edit bar button item, as Material Navigation Bar does not
// handle a system UIBarButtonSystemItemEdit item.
UIBarButtonItem* button = [[UIBarButtonItem alloc]
initWithTitle:l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON)
style:UIBarButtonItemStyleDone
target:self
action:@selector(editButtonPressed)];
[button setEnabled:[self editButtonEnabled]];
return button;
}
- (UIBarButtonItem*)createEditModeDoneButton {
// Create a custom Done bar button item, as Material Navigation Bar does not
// handle a system UIBarButtonSystemItemDone item.
return [[UIBarButtonItem alloc]
initWithTitle:l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON)
style:UIBarButtonItemStyleDone
target:self
action:@selector(editButtonPressed)];
}
#pragma mark - Subclassing
- (BOOL)shouldShowEditButton {
return NO;
}
- (BOOL)editButtonEnabled {
return NO;
}
- (void)editButtonPressed {
self.tableView.editing = !self.tableView.editing;
[self updateEditButton];
}
@end
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"
#include "base/test/scoped_task_environment.h"
#include "ios/chrome/browser/browser_state/test_chrome_browser_state.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#include "ios/chrome/grit/ios_strings.h"
#include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h"
#include "testing/platform_test.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#include "third_party/ocmock/gtest_support.h"
#include "ui/base/l10n/l10n_util_mac.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
class SettingsRootTableViewControllerTest : public PlatformTest {
public:
SettingsRootTableViewController* Controller() {
return [[SettingsRootTableViewController alloc]
initWithTableViewStyle:UITableViewStylePlain
appBarStyle:ChromeTableViewControllerStyleWithAppBar];
}
SettingsNavigationController* NavigationController() {
if (!chrome_browser_state_) {
TestChromeBrowserState::Builder test_cbs_builder;
chrome_browser_state_ = test_cbs_builder.Build();
}
return [[SettingsNavigationController alloc]
initWithRootViewController:nil
browserState:chrome_browser_state_.get()
delegate:nil];
}
protected:
base::test::ScopedTaskEnvironment scoped_task_environment_;
std::unique_ptr<TestChromeBrowserState> chrome_browser_state_;
};
TEST_F(SettingsRootTableViewControllerTest, TestUpdateEditButton) {
SettingsRootTableViewController* controller = Controller();
id mockController = OCMPartialMock(controller);
SettingsNavigationController* navigationController = NavigationController();
OCMStub([mockController navigationController])
.andReturn(navigationController);
// Check that there the navigation controller's button if the table view isn't
// edited and the controller has the default behavior for
// |shouldShowEditButton|.
controller.tableView.editing = NO;
[controller updateEditButton];
EXPECT_NSEQ(l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON),
controller.navigationItem.rightBarButtonItem.title);
// Check that there the OK button if the table view is being edited and the
// controller has the default behavior for |shouldShowEditButton|.
controller.tableView.editing = YES;
[controller updateEditButton];
EXPECT_NSEQ(l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_DONE_BUTTON),
controller.navigationItem.rightBarButtonItem.title);
// Check that there the OK button if the table view isn't edited and the
// controller returns YES for |shouldShowEditButton|.
controller.tableView.editing = NO;
OCMStub([mockController shouldShowEditButton]).andReturn(YES);
[controller updateEditButton];
EXPECT_NSEQ(l10n_util::GetNSString(IDS_IOS_NAVIGATION_BAR_EDIT_BUTTON),
controller.navigationItem.rightBarButtonItem.title);
}
......@@ -83,6 +83,10 @@ typedef NS_ENUM(NSInteger, ChromeTableViewControllerStyle) {
- (void)performBatchTableViewUpdates:(void (^)(void))updates
completion:(void (^)(BOOL finished))completion;
// Removes the items at |indexPaths| from the model only. The changes need to be
// also done on the table view to avoid being in an inconsistent state.
- (void)removeFromModelItemAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths;
// Methods for reconfiguring and reloading the table view are provided by
// ChromeTableViewConsumer.
......
......@@ -165,6 +165,23 @@
}
}
- (void)removeFromModelItemAtIndexPaths:(NSArray<NSIndexPath*>*)indexPaths {
// Sort and enumerate in reverse order to delete the items from the collection
// view model.
NSArray* sortedIndexPaths =
[indexPaths sortedArrayUsingSelector:@selector(compare:)];
for (NSIndexPath* indexPath in [sortedIndexPaths reverseObjectEnumerator]) {
NSInteger sectionIdentifier =
[self.tableViewModel sectionIdentifierForSection:indexPath.section];
NSInteger itemType = [self.tableViewModel itemTypeForIndexPath:indexPath];
NSUInteger index =
[self.tableViewModel indexInItemTypeForIndexPath:indexPath];
[self.tableViewModel removeItemWithType:itemType
fromSectionWithIdentifier:sectionIdentifier
atIndex:index];
}
}
#pragma mark - ChromeTableViewConsumer
- (void)reconfigureCellsForItems:(NSArray*)items {
......
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