Commit ce3464b2 authored by edchin's avatar edchin Committed by Commit Bot

[ios] Basic setup of grid view controller

This CL creates a simple grid view controller.
It also adds an entry in showcase.

Partly to avoid name collision, we name the single grid
as a GridViewController. The parent tab switcher with multiple
panels and other controls is called the TabGridViewController.

Bug: 804496
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Ib5b06befc35408958b8613a776f2c93742137bc7
Reviewed-on: https://chromium-review.googlesource.com/920641
Commit-Queue: edchin <edchin@chromium.org>
Reviewed-by: default avatarMark Cogan <marq@chromium.org>
Reviewed-by: default avataredchin <edchin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537213}
parent 4e890408
......@@ -23,8 +23,11 @@ source_set("tab_grid_ui") {
sources = [
"grid_cell.h",
"grid_cell.mm",
"grid_consumer.h",
"grid_item.h",
"grid_item.mm",
"grid_view_controller.h",
"grid_view_controller.mm",
"tab_grid_view_controller.h",
"tab_grid_view_controller.mm",
]
......
// 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_TAB_GRID_GRID_CONSUMER_H_
#define IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_CONSUMER_H_
@class GridItem;
// Supports insert/delete/updates to a grid.
@protocol GridConsumer
// Tells the consumer to replace its current set of items with |items| and
// update the selected index to be |selectedIndex|. It's an error to call this
// method with a |selectedIndex| value higher than the maximum index of |items|.
- (void)populateItems:(NSArray<GridItem*>*)items
selectedIndex:(NSInteger)selectedIndex;
@end
#endif // IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_CONSUMER_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.
#ifndef IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_VIEW_CONTROLLER_H_
#define IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_VIEW_CONTROLLER_H_
#import <UIKit/UIKit.h>
#import "ios/chrome/browser/ui/tab_grid/grid_consumer.h"
// A view controller that contains a grid of items.
@interface GridViewController : UIViewController<GridConsumer>
@end
#endif // IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_VIEW_CONTROLLER_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/tab_grid/grid_view_controller.h"
#import "base/mac/foundation_util.h"
#import "base/numerics/safe_conversions.h"
#import "ios/chrome/browser/ui/tab_grid/grid_cell.h"
#import "ios/chrome/browser/ui/tab_grid/grid_item.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
NSString* const kCellIdentifier = @"GridCellIdentifier";
} // namespace
@interface GridViewController ()<UICollectionViewDataSource>
// A collection view of items in a grid format.
@property(nonatomic, weak) UICollectionView* collectionView;
// The local model backing the collection view.
@property(nonatomic, strong) NSMutableArray<GridItem*>* items;
// Selected index of the selected item. A negative value indicates that no item
// is selected.
@property(nonatomic, assign) NSInteger selectedIndex;
@end
@implementation GridViewController
@synthesize collectionView = _collectionView;
@synthesize items = _items;
@synthesize selectedIndex = _selectedIndex;
- (instancetype)init {
if (self = [super init]) {
_items = [[NSMutableArray<GridItem*> alloc] init];
}
return self;
}
- (void)loadView {
UICollectionViewFlowLayout* layout =
[[UICollectionViewFlowLayout alloc] init];
layout.sectionInset = UIEdgeInsetsMake(20.0f, 20.0f, 20.0f, 20.0f);
layout.itemSize = CGSizeMake(160.0f, 160.0f);
UICollectionView* collectionView =
[[UICollectionView alloc] initWithFrame:CGRectZero
collectionViewLayout:layout];
[collectionView registerClass:[GridCell class]
forCellWithReuseIdentifier:kCellIdentifier];
collectionView.dataSource = self;
collectionView.backgroundColor = [UIColor blackColor];
self.collectionView = collectionView;
self.view = collectionView;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView*)collectionView
numberOfItemsInSection:(NSInteger)section {
return base::checked_cast<NSInteger>(self.items.count);
}
- (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView
cellForItemAtIndexPath:(NSIndexPath*)indexPath {
GridCell* cell = base::mac::ObjCCastStrict<GridCell>([collectionView
dequeueReusableCellWithReuseIdentifier:kCellIdentifier
forIndexPath:indexPath]);
[cell configureWithItem:self.items[indexPath.item] theme:GridCellThemeLight];
return cell;
}
#pragma mark - GridConsumer
- (void)populateItems:(NSArray<GridItem*>*)items
selectedIndex:(NSInteger)selectedIndex {
self.items = [items mutableCopy];
self.selectedIndex = selectedIndex;
if ([self isViewLoaded]) {
[self.collectionView
reloadItemsAtIndexPaths:[self.collectionView
indexPathsForVisibleItems]];
}
}
#pragma mark - Private
- (void)setSelectedIndex:(NSInteger)selectedIndex {
DCHECK_LT(selectedIndex, base::checked_cast<NSInteger>(self.items.count));
_selectedIndex = selectedIndex;
}
@end
......@@ -9,6 +9,8 @@
#import "ios/chrome/browser/ui/tab_switcher/tab_switcher.h"
// View controller representing a tab switcher. The tab switcher has an
// incognito tab grid, regular tab grid, and remote tabs.
@interface TabGridViewController : UIViewController<TabSwitcher>
@end
......
......@@ -67,9 +67,9 @@
showcase::kUseCaseKey : @"Bubble",
},
@{
showcase::kClassForDisplayKey : @"TabGridViewController",
showcase::kClassForInstantiationKey : @"SCTabGridCoordinator",
showcase::kUseCaseKey : @"Tab grid",
showcase::kClassForDisplayKey : @"GridViewController",
showcase::kClassForInstantiationKey : @"SCGridCoordinator",
showcase::kUseCaseKey : @"Grid UI",
},
@{
showcase::kClassForDisplayKey : @"GridCell",
......
......@@ -6,8 +6,8 @@ source_set("tab_grid") {
sources = [
"sc_grid_cell_view_controller.h",
"sc_grid_cell_view_controller.mm",
"sc_tab_grid_coordinator.h",
"sc_tab_grid_coordinator.mm",
"sc_grid_coordinator.h",
"sc_grid_coordinator.mm",
]
deps = [
"//base",
......
......@@ -7,7 +7,7 @@
#import "ios/showcase/common/navigation_coordinator.h"
@interface SCTabGridCoordinator : NSObject<NavigationCoordinator>
@interface SCGridCoordinator : NSObject<NavigationCoordinator>
@end
#endif // IOS_SHOWCASE_TAB_GRID_SC_TAB_GRID_COORDINATOR_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/showcase/tab_grid/sc_grid_coordinator.h"
#import "ios/chrome/browser/ui/tab_grid/grid_item.h"
#import "ios/chrome/browser/ui/tab_grid/grid_view_controller.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface SCGridCoordinator ()
@end
@implementation SCGridCoordinator
@synthesize baseViewController = _baseViewController;
- (void)start {
GridViewController* gridViewController = [[GridViewController alloc] init];
GridItem* item1 = [[GridItem alloc] init];
item1.title = @"Basecamp: project with an attitude.";
GridItem* item2 = [[GridItem alloc] init];
item2.title = @"Basecamp: project with an attitude.";
GridItem* item3 = [[GridItem alloc] init];
item3.title = @"Basecamp: project with an attitude.";
NSArray* items = @[ item1, item2, item3 ];
[gridViewController populateItems:items selectedIndex:1];
gridViewController.title = @"Grid UI";
[self.baseViewController pushViewController:gridViewController animated:YES];
}
@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/showcase/tab_grid/sc_tab_grid_coordinator.h"
#import "ios/chrome/browser/ui/tab_grid/tab_grid_view_controller.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@interface SCTabGridCoordinator ()
@property(nonatomic, strong) TabGridViewController* viewController;
@end
@implementation SCTabGridCoordinator
@synthesize baseViewController = _baseViewController;
@synthesize viewController = _viewController;
- (void)start {
self.viewController = [[TabGridViewController alloc] init];
self.viewController.title = @"Tab Grid";
[self.baseViewController setHidesBarsOnSwipe:YES];
[self.baseViewController pushViewController:self.viewController animated:YES];
}
@end
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