Skip to content

Commit

Permalink
[ios] Creates a base class for Settings table views.
Browse files Browse the repository at this point in the history
Integrating with the MDC AppBar requires a bunch of boilerplate setup
and scrollview delegate code.  Move that code to a separate base class,
so that the tableview cell catalog class becomes simpler and easier to
read.

BUG=811075

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I8a14d2f8aacc60480c50ee1a361c3413b82dd20a
Reviewed-on: https://chromium-review.googlesource.com/913669
Reviewed-by: Sergio Collazos <sczs@chromium.org>
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#536109}
  • Loading branch information
rohit-rao authored and Commit Bot committed Feb 12, 2018
1 parent ca980c4 commit 78f1c2c
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 52 deletions.
2 changes: 2 additions & 0 deletions ios/chrome/browser/ui/settings/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ source_set("settings") {
"settings_navigation_controller.mm",
"settings_root_collection_view_controller.h",
"settings_root_collection_view_controller.mm",
"settings_root_table_view_controller.h",
"settings_root_table_view_controller.mm",
"settings_utils.h",
"settings_utils.mm",
"sync_create_passphrase_collection_view_controller.h",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// 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.

#ifndef IOS_CHROME_BROWSER_UI_SETTINGS_SETTINGS_ROOT_TABLE_VIEW_CONTROLLER_H_
#define IOS_CHROME_BROWSER_UI_SETTINGS_SETTINGS_ROOT_TABLE_VIEW_CONTROLLER_H_

#import <UIKit/UIKit.h>

#import "ios/chrome/browser/ui/material_components/app_bar_presenting.h"

// SettingsRootTableViewController is a base class for integrating UITableViews
// into the Settings UI. It handles the configuration and display of the MDC
// AppBar.
@interface SettingsRootTableViewController
: UITableViewController<AppBarPresenting>

- (instancetype)initWithStyle:(UITableViewStyle)style NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithNibName:(NSString*)name
bundle:(NSBundle*)bundle NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder*)decoder NS_UNAVAILABLE;

#pragma mark UIScrollViewDelegate

// Updates the MDCFlexibleHeader with changes to the table view scroll
// state. Must be called by subclasses if they override this method in order to
// maintain this functionality.
- (void)scrollViewDidScroll:(UIScrollView*)scrollView NS_REQUIRES_SUPER;

// Updates the MDCFlexibleHeader with changes to the table view scroll
// state. Must be called by subclasses if they override this method in order to
// maintain this functionality.
- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView
willDecelerate:(BOOL)decelerate NS_REQUIRES_SUPER;

// Updates the MDCFlexibleHeader with changes to the table view scroll
// state. Must be called by subclasses if they override this method in order to
// maintain this functionality.
- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView
NS_REQUIRES_SUPER;

// Updates the MDCFlexibleHeader with changes to the table view scroll
// state. Must be called by subclasses if they override this method in order to
// maintain this functionality.
- (void)scrollViewWillEndDragging:(UIScrollView*)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint*)targetContentOffset
NS_REQUIRES_SUPER;

@end

#endif // IOS_CHROME_BROWSER_UI_SETTINGS_SETTINGS_ROOT_TABLE_VIEW_CONTROLLER_H_
106 changes: 106 additions & 0 deletions ios/chrome/browser/ui/settings/settings_root_table_view_controller.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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"

#import "base/mac/foundation_util.h"
#import "ios/chrome/browser/ui/material_components/utils.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/third_party/material_components_ios/src/components/AppBar/src/MaterialAppBar.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

@interface SettingsRootTableViewController ()

// The AppBar for this view controller.
@property(nonatomic, readwrite, strong) MDCAppBar* appBar;

@end

@implementation SettingsRootTableViewController
@synthesize appBar = _appBar;

- (instancetype)initWithStyle:(UITableViewStyle)style {
if ((self = [super initWithStyle:style])) {
// Configure the AppBar.
_appBar = [[MDCAppBar alloc] init];
[self addChildViewController:_appBar.headerViewController];
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];

// Configure the app bar. This cannot be in a shared base class because
// UITableViewControllers normally do not use MDC styling, but this one does
// in order to match the other Settings screens.
ConfigureAppBarWithCardStyle(self.appBar);
self.appBar.headerViewController.headerView.trackingScrollView =
self.tableView;
// Add the AppBar's views after all other views have been registered.
[self.appBar addSubviewsToParent];
}

- (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];
self.navigationItem.rightBarButtonItem = doneButton;
}

- (UIViewController*)childViewControllerForStatusBarHidden {
return self.appBar.headerViewController;
}

- (UIViewController*)childViewControllerForStatusBarStyle {
return self.appBar.headerViewController;
}

#pragma mark UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
MDCFlexibleHeaderView* headerView =
self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidScroll];
}
}

- (void)scrollViewDidEndDecelerating:(UIScrollView*)scrollView {
MDCFlexibleHeaderView* headerView =
self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidEndDecelerating];
}
}

- (void)scrollViewDidEndDragging:(UIScrollView*)scrollView
willDecelerate:(BOOL)decelerate {
MDCFlexibleHeaderView* headerView =
self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView trackingScrollViewDidEndDraggingWillDecelerate:decelerate];
}
}

- (void)scrollViewWillEndDragging:(UIScrollView*)scrollView
withVelocity:(CGPoint)velocity
targetContentOffset:(inout CGPoint*)targetContentOffset {
MDCFlexibleHeaderView* headerView =
self.appBar.headerViewController.headerView;
if (scrollView == headerView.trackingScrollView) {
[headerView
trackingScrollViewWillEndDraggingWithVelocity:velocity
targetContentOffset:targetContentOffset];
}
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,15 @@
#import <UIKit/UIKit.h>

#import "ios/chrome/browser/ui/material_components/app_bar_presenting.h"
#import "ios/chrome/browser/ui/settings/settings_root_table_view_controller.h"

// TableCellCatalogViewController is a Debug-only settings screen which serves
// as a catalog of the various UITableViewCells that are used by the app.
@interface TableCellCatalogViewController
: UITableViewController<AppBarPresenting>
@interface TableCellCatalogViewController : SettingsRootTableViewController

- (instancetype)init NS_DESIGNATED_INITIALIZER;

- (instancetype)initWithStyle:(UITableViewStyle)style NS_UNAVAILABLE;
- (instancetype)initWithNibName:(NSString*)name
bundle:(NSBundle*)bundle NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder*)decoder NS_UNAVAILABLE;

@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,21 @@

#import "ios/chrome/browser/ui/settings/table_cell_catalog_view_controller.h"

#import "base/mac/foundation_util.h"
#import "ios/chrome/browser/ui/material_components/utils.h"
#import "ios/chrome/browser/ui/settings/settings_navigation_controller.h"
#import "ios/third_party/material_components_ios/src/components/AppBar/src/MaterialAppBar.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif

@interface TableCellCatalogViewController ()

// The AppBar for this view controller.
@property(nonatomic, readwrite, strong) MDCAppBar* appBar;

@end

@implementation TableCellCatalogViewController
@synthesize appBar = _appBar;

- (instancetype)init {
self = [super initWithStyle:UITableViewStyleGrouped];
if (self) {
// Configure the AppBar.
_appBar = [[MDCAppBar alloc] init];
[self addChildViewController:_appBar.headerViewController];
if ((self = [super initWithStyle:UITableViewStyleGrouped])) {
}
return self;
}

- (void)viewDidLoad {
[super viewDidLoad];
self.title = @"Table Cell Catalog";

// Configure the app bar. This cannot be in a shared base class because
// UITableViewControllers normally do not use MDC styling, but this one does
// in order to match the other Settings screens.
ConfigureAppBarWithCardStyle(self.appBar);
self.appBar.headerViewController.headerView.trackingScrollView =
self.tableView;
// Add the AppBar's views after all other views have been registered.
[self.appBar addSubviewsToParent];
self.tableView.delegate = self.appBar.headerViewController;
}

- (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];
self.navigationItem.rightBarButtonItem = doneButton;
}

- (UIViewController*)childViewControllerForStatusBarHidden {
return self.appBar.headerViewController;
}

- (UIViewController*)childViewControllerForStatusBarStyle {
return self.appBar.headerViewController;
}

@end

0 comments on commit 78f1c2c

Please sign in to comment.