Commit bf8e840d authored by Rohit Rao's avatar Rohit Rao Committed by Commit Bot

[ios] Introduces BookmarkHomeSharedState.

An upcoming refactoring will move methods out of BookmarkTableView and into
BookmarkHomeViewController or BookmarkHomeMediator. To ease this transition, we
introduce a data structure that holds various ivars that used to be in
BookmarkTableView.

BUG=839427,840381

Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Iaeb854e3a5cc243e90dd6b5a6e15e70f679728f6
Reviewed-on: https://chromium-review.googlesource.com/1047365
Commit-Queue: Rohit Rao <rohitrao@chromium.org>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#556471}
parent 9628f381
......@@ -17,6 +17,8 @@ source_set("bookmarks") {
"bookmark_folder_table_view_cell.mm",
"bookmark_folder_view_controller.h",
"bookmark_folder_view_controller.mm",
"bookmark_home_shared_state.h",
"bookmark_home_shared_state.mm",
"bookmark_home_view_controller.h",
"bookmark_home_view_controller.mm",
"bookmark_home_waiting_view.h",
......@@ -102,6 +104,7 @@ source_set("bookmarks") {
"//ios/chrome/browser/ui/icons",
"//ios/chrome/browser/ui/image_util",
"//ios/chrome/browser/ui/keyboard",
"//ios/chrome/browser/ui/list_model",
"//ios/chrome/browser/ui/main:feature_flags",
"//ios/chrome/browser/ui/material_components",
"//ios/chrome/browser/ui/ntp",
......
// 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_BOOKMARKS_BOOKMARK_HOME_SHARED_STATE_H_
#define IOS_CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_HOME_SHARED_STATE_H_
#import <UIKit/UIKit.h>
#import <set>
#import "ios/chrome/browser/ui/list_model/list_model.h"
@class BookmarkHomeSharedState;
@class BookmarkTableCell;
@class MDCFlexibleHeaderView;
@class TableViewModel;
namespace bookmarks {
class BookmarkModel;
class BookmarkNode;
}
typedef NS_ENUM(NSInteger, BookmarkHomeSectionIdentifier) {
BookmarkHomeSectionIdentifierPromo = kSectionIdentifierEnumZero,
BookmarkHomeSectionIdentifierBookmarks,
};
typedef NS_ENUM(NSInteger, BookmarkHomeItemType) {
BookmarkHomeItemTypePromo = kItemTypeEnumZero,
BookmarkHomeItemTypeBookmark,
};
@protocol BookmarkHomeSharedStateObserver
// Called when the set of edit nodes is cleared.
- (void)sharedStateDidClearEditNodes:(BookmarkHomeSharedState*)sharedState;
@end
// BookmarkHomeSharedState is a data structure that contains a number of fields
// that were previously ivars of BookmarkTableView. They are moved to a separate
// data structure in order to ease moving code between files.
@interface BookmarkHomeSharedState : NSObject
// Models.
// The model backing the table view.
@property(nonatomic, readonly, strong) TableViewModel* tableViewModel;
// The model holding bookmark data.
@property(nonatomic, readonly, assign) bookmarks::BookmarkModel* bookmarkModel;
// Views.
// The UITableView to show bookmarks.
@property(nonatomic, strong) UITableView* tableView;
// Header view to display the shadow below the app bar. It must be tracking the
// |tableView|.
@property(nonatomic, weak) MDCFlexibleHeaderView* headerView;
// State variables.
// The BookmarkNode that is currently being displayed by the table view. May be
// nil.
@property(nonatomic, assign)
const bookmarks::BookmarkNode* tableViewDisplayedRootNode;
// If the table view is in edit mode.
@property(nonatomic, assign) BOOL currentlyInEditMode;
// The set of nodes currently being edited.
@property(nonatomic, readonly, assign)
std::set<const bookmarks::BookmarkNode*>& editNodes;
// If a new folder is being added currently.
@property(nonatomic, assign) BOOL addingNewFolder;
// The cell for the newly created folder while its name is being edited. Set
// to nil once the editing completes. Corresponds to |editingFolderNode|.
@property(nonatomic, weak) BookmarkTableCell* editingFolderCell;
// The newly created folder node its name is being edited.
@property(nonatomic, assign) const bookmarks::BookmarkNode* editingFolderNode;
// Counts the number of favicon download requests from Google server in the
// lifespan of this tableView.
@property(nonatomic, assign) NSUInteger faviconDownloadCount;
// True if the promo is visible.
@property(nonatomic, assign) BOOL promoVisible;
// This object can have at most one observer.
@property(nonatomic, weak) id<BookmarkHomeSharedStateObserver> observer;
// Constants
// Minimal acceptable favicon size, in points.
+ (CGFloat)minFaviconSizePt;
// Desired favicon size, in points.
+ (CGFloat)desiredFaviconSizePt;
// Cell height, in points.
+ (CGFloat)cellHeightPt;
// Minimium spacing between keyboard and the titleText when creating new folder,
// in points.
+ (CGFloat)keyboardSpacingPt;
// Max number of favicon download requests in the lifespan of this tableView.
+ (NSUInteger)maxDownloadFaviconCount;
- (instancetype)initWithBookmarkModel:(bookmarks::BookmarkModel*)bookmarkModel
displayedRootNode:
(const bookmarks::BookmarkNode*)displayedRootNode
NS_DESIGNATED_INITIALIZER;
- (instancetype)init NS_UNAVAILABLE;
@end
#endif // IOS_CHROME_BROWSER_UI_BOOKMARKS_BOOKMARK_HOME_SHARED_STATE_H_
// 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/bookmarks/bookmark_home_shared_state.h"
#include "base/logging.h"
#import "ios/chrome/browser/ui/bookmarks/cells/bookmark_table_cell.h"
#import "ios/chrome/browser/ui/table_view/table_view_model.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Minimal acceptable favicon size, in points.
const CGFloat kMinFaviconSizePt = 16.0;
// Desired favicon size, in points.
const CGFloat kDesiredFaviconSizePt = 32.0;
// Cell height, in points.
const CGFloat kCellHeightPt = 56.0;
// Minimium spacing between keyboard and the titleText when creating new folder,
// in points.
const CGFloat kKeyboardSpacingPt = 16.0;
// Max number of favicon download requests in the lifespan of this tableView.
const NSUInteger kMaxDownloadFaviconCount = 50;
} // namespace
@implementation BookmarkHomeSharedState {
std::set<const bookmarks::BookmarkNode*> _editNodes;
}
@synthesize addingNewFolder = _addingNewFolder;
@synthesize bookmarkModel = _bookmarkModel;
@synthesize currentlyInEditMode = _currentlyInEditMode;
@synthesize editingFolderCell = _editingFolderCell;
@synthesize editingFolderNode = _editingFolderNode;
@synthesize faviconDownloadCount = _faviconDownloadCount;
@synthesize headerView = _headerView;
@synthesize observer = _observer;
@synthesize promoVisible = _promoVisible;
@synthesize tableView = _tableView;
@synthesize tableViewDisplayedRootNode = _tableViewDisplayedRootNode;
@synthesize tableViewModel = _tableViewModel;
- (instancetype)initWithBookmarkModel:(bookmarks::BookmarkModel*)bookmarkModel
displayedRootNode:
(const bookmarks::BookmarkNode*)displayedRootNode {
if ((self = [super init])) {
_tableViewModel = [[TableViewModel alloc] init];
_bookmarkModel = bookmarkModel;
_tableViewDisplayedRootNode = displayedRootNode;
}
return self;
}
- (void)setCurrentlyInEditMode:(BOOL)currentlyInEditMode {
DCHECK(self.tableView);
// If not in editing mode but the tableView's editing is ON, it means the
// table is waiting for a swipe-to-delete confirmation. In this case, we need
// to close the confirmation by setting tableView.editing to NO.
if (!_currentlyInEditMode && self.tableView.editing) {
self.tableView.editing = NO;
}
[self.editingFolderCell stopEdit];
_currentlyInEditMode = currentlyInEditMode;
_editNodes.clear();
[self.observer sharedStateDidClearEditNodes:self];
[self.tableView setEditing:currentlyInEditMode animated:YES];
}
- (std::set<const bookmarks::BookmarkNode*>&)editNodes {
return _editNodes;
}
+ (CGFloat)minFaviconSizePt {
return kMinFaviconSizePt;
}
+ (CGFloat)desiredFaviconSizePt {
return kDesiredFaviconSizePt;
}
+ (CGFloat)cellHeightPt {
return kCellHeightPt;
}
+ (CGFloat)keyboardSpacingPt {
return kKeyboardSpacingPt;
}
+ (NSUInteger)maxDownloadFaviconCount {
return kMaxDownloadFaviconCount;
}
@end
......@@ -16,6 +16,7 @@
#import "ios/chrome/browser/ui/bookmarks/bookmark_edit_view_controller.h"
#import "ios/chrome/browser/ui/bookmarks/bookmark_folder_editor_view_controller.h"
#import "ios/chrome/browser/ui/bookmarks/bookmark_folder_view_controller.h"
#import "ios/chrome/browser/ui/bookmarks/bookmark_home_shared_state.h"
#import "ios/chrome/browser/ui/bookmarks/bookmark_home_waiting_view.h"
#include "ios/chrome/browser/ui/bookmarks/bookmark_model_bridge_observer.h"
#import "ios/chrome/browser/ui/bookmarks/bookmark_navigation_controller.h"
......@@ -72,6 +73,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
BookmarkEditViewControllerDelegate,
BookmarkFolderEditorViewControllerDelegate,
BookmarkFolderViewControllerDelegate,
BookmarkHomeSharedStateObserver,
BookmarkModelBridgeObserver,
BookmarkPromoControllerDelegate,
BookmarkTableViewDelegate,
......@@ -87,6 +89,10 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
BOOL _addedConstraints;
}
// Shared state between BookmarkHome classes. Used as a temporary refactoring
// aid.
@property(nonatomic, strong) BookmarkHomeSharedState* sharedState;
// The bookmark model used.
@property(nonatomic, assign) bookmarks::BookmarkModel* bookmarks;
......@@ -153,6 +159,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
@synthesize dispatcher = _dispatcher;
@synthesize cachedContentPosition = _cachedContentPosition;
@synthesize isReconstructingFromCache = _isReconstructingFromCache;
@synthesize sharedState = _sharedState;
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -243,13 +250,18 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
#pragma mark - Protected
- (void)loadBookmarkViews {
DCHECK(_rootNode);
self.sharedState =
[[BookmarkHomeSharedState alloc] initWithBookmarkModel:_bookmarks
displayedRootNode:_rootNode];
self.sharedState.observer = self;
self.automaticallyAdjustsScrollViewInsets = NO;
self.bookmarksTableView = [[BookmarkTableView alloc]
initWithBrowserState:self.browserState
delegate:self
rootNode:_rootNode
frame:self.view.bounds
presenter:self /* id<SigninPresenter> */];
self.bookmarksTableView =
[[BookmarkTableView alloc] initWithSharedState:self.sharedState
browserState:self.browserState
delegate:self
frame:self.view.bounds];
[self.bookmarksTableView
setAutoresizingMask:UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight];
......@@ -413,7 +425,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
selectedEditNodes:
(const std::set<const bookmarks::BookmarkNode*>&)nodes {
// Early return if bookmarks table is not in edit mode.
if (!self.bookmarksTableView.editing) {
if (!self.sharedState.currentlyInEditMode) {
return;
}
......@@ -716,15 +728,15 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
// Set up navigation bar for the new UI.
- (void)setupNavigationBar {
DCHECK(self.sharedState.tableView);
self.navigationController.navigationBarHidden = YES;
self.appBar = [[MDCAppBar alloc] init];
[self addChildViewController:self.appBar.headerViewController];
ConfigureAppBarWithCardStyle(self.appBar);
// Set the header view's tracking scroll view.
self.appBar.headerViewController.headerView.trackingScrollView =
self.bookmarksTableView.tableView;
self.bookmarksTableView.headerView =
self.appBar.headerViewController.headerView;
self.sharedState.tableView;
self.sharedState.headerView = self.appBar.headerViewController.headerView;
[self.appBar addSubviewsToParent];
// Prevent the touch events on appBar from being forwarded to the tableView.
......@@ -840,7 +852,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
// Sets the editing mode for tableView, update context bar state accordingly.
- (void)setTableViewEditing:(BOOL)editing {
[self.bookmarksTableView setEditing:editing];
self.sharedState.currentlyInEditMode = editing;
[self setContextBarState:editing ? BookmarksContextBarBeginSelection
: BookmarksContextBarDefault];
}
......@@ -854,7 +866,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
return;
}
const std::set<const bookmarks::BookmarkNode*> nodes =
[self.bookmarksTableView editNodes];
self.sharedState.editNodes;
switch (self.contextBarState) {
case BookmarksContextBarDefault:
// New Folder clicked.
......@@ -886,7 +898,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
return;
}
const std::set<const bookmarks::BookmarkNode*> nodes =
[self.bookmarksTableView editNodes];
self.sharedState.editNodes;
// Center button is shown and is clickable only when at least
// one node is selected.
DCHECK(nodes.size() > 0);
......@@ -941,7 +953,7 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
return;
}
// Toggle edit mode.
[self setTableViewEditing:!self.bookmarksTableView.editing];
[self setTableViewEditing:!self.sharedState.currentlyInEditMode];
}
#pragma mark - ContextBarStates
......@@ -1202,4 +1214,11 @@ std::vector<GURL> GetUrlsToOpen(const std::vector<const BookmarkNode*>& nodes) {
[self.dispatcher showSignin:command baseViewController:self];
}
#pragma mark - BookmarkHomeSharedStateObserver
- (void)sharedStateDidClearEditNodes:(BookmarkHomeSharedState*)sharedState {
[self bookmarkTableView:self.bookmarksTableView
selectedEditNodes:sharedState.editNodes];
}
@end
......@@ -8,6 +8,8 @@
#import <UIKit/UIKit.h>
#include <set>
@class BookmarkHomeSharedState;
@class BookmarkTableView;
class GURL;
@protocol SigninPresenter;
@class SigninPromoViewConfigurator;
......@@ -21,9 +23,6 @@ namespace ios {
class ChromeBrowserState;
}
@class BookmarkTableView;
@class MDCFlexibleHeaderView;
// Delegate to handle actions on the table.
@protocol BookmarkTableViewDelegate<NSObject>
......@@ -70,24 +69,15 @@ class ChromeBrowserState;
@end
@interface BookmarkTableView : UIView
// If the table is in edit mode.
@property(nonatomic, assign) BOOL editing;
// The UITableView to show bookmarks.
@property(nonatomic, strong, readonly) UITableView* tableView;
// Header view to display the shadow below the app bar. It must be tracking the
// |tableView|.
@property(nonatomic, weak) MDCFlexibleHeaderView* headerView;
// Shows all sub-folders and sub-urls of a folder node (that is set as the root
// node) in a UITableView. Note: This class intentionally does not try to
// maintain state through a folder transition. A new instance of this class is
// pushed on the navigation stack for a different root node.
- (instancetype)initWithBrowserState:(ios::ChromeBrowserState*)browserState
delegate:(id<BookmarkTableViewDelegate>)delegate
rootNode:(const bookmarks::BookmarkNode*)rootNode
frame:(CGRect)frame
presenter:(id<SigninPresenter>)presenter
NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithSharedState:(BookmarkHomeSharedState*)sharedState
browserState:(ios::ChromeBrowserState*)browserState
delegate:(id<BookmarkTableViewDelegate>)delegate
frame:(CGRect)frame NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithFrame:(CGRect)frame NS_UNAVAILABLE;
- (instancetype)initWithFrame:(CGRect)frame
......@@ -108,9 +98,6 @@ class ChromeBrowserState;
// Called when adding a new folder
- (void)addNewFolder;
// Returns the currently selected edit nodes.
- (const std::set<const bookmarks::BookmarkNode*>&)editNodes;
// Returns a vector of edit nodes.
- (std::vector<const bookmarks::BookmarkNode*>)getEditNodesInVector;
......
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