Chromium Code Reviews| Index: ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| diff --git a/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm b/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| index 7e9eee5a5606d9af6c4433c4bfd420e8c146180b..5d8541ac1ee51f2e88074339185983dda7472057 100644 |
| --- a/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| +++ b/ios/clean/chrome/browser/ui/ntp/new_tab_page_view_controller.mm |
| @@ -10,18 +10,35 @@ |
| #import "ios/chrome/browser/ui/ntp/new_tab_page_bar_item.h" |
| #import "ios/chrome/browser/ui/ntp/new_tab_page_controller.h" |
| #import "ios/chrome/browser/ui/ntp/new_tab_page_view.h" |
| +#import "ios/chrome/browser/ui/rtl_geometry.h" |
| +#include "ios/chrome/browser/ui/ui_util.h" |
| #include "ios/chrome/grit/ios_strings.h" |
| +#import "ios/clean/chrome/browser/ui/commands/ntp_commands.h" |
| #include "ui/base/l10n/l10n_util.h" |
| #if !defined(__has_feature) || !__has_feature(objc_arc) |
| #error "This file requires ARC support." |
| #endif |
| +@interface NTPViewController ()<UIScrollViewDelegate, NewTabPageBarDelegate> { |
| + BOOL _homeLoaded; |
| + BOOL _openTabsLoaded; |
| + BOOL _bookmarksLoaded; |
| +} |
| +@property(nonatomic, strong) NewTabPageView* NTPView; |
| +@property(nonatomic, strong) NSArray* tabBarItems; |
| +@end |
| + |
| @implementation NTPViewController |
| +@synthesize NTPView = _NTPView; |
| +@synthesize dispatcher = _dispatcher; |
| +@synthesize tabBarItems = _tabBarItems; |
| + |
| #pragma mark - UIViewController |
| - (void)viewDidLoad { |
| + [super viewDidLoad]; |
| self.title = l10n_util::GetNSString(IDS_NEW_TAB_TITLE); |
| self.view.backgroundColor = [UIColor whiteColor]; |
| @@ -34,46 +51,137 @@ |
| scrollView.contentMode = UIViewContentModeScaleAspectFit; |
| scrollView.bounces = YES; |
| scrollView.scrollsToTop = NO; |
| + scrollView.delegate = self; |
| NewTabPageBar* tabBar = [[NewTabPageBar alloc] initWithFrame:CGRectZero]; |
| - NewTabPageView* ntpView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
| - andScrollView:scrollView |
| - andTabBar:tabBar]; |
| - ntpView.translatesAutoresizingMaskIntoConstraints = NO; |
| - [self.view addSubview:ntpView]; |
| + tabBar.delegate = self; |
| + self.NTPView = [[NewTabPageView alloc] initWithFrame:CGRectZero |
| + andScrollView:scrollView |
| + andTabBar:tabBar]; |
| + self.NTPView.translatesAutoresizingMaskIntoConstraints = NO; |
| + [self.view addSubview:self.NTPView]; |
| [NSLayoutConstraint activateConstraints:@[ |
| - [ntpView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| - [ntpView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
| - [ntpView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| - [ntpView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
| + [self.NTPView.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| + [self.NTPView.leadingAnchor |
| + constraintEqualToAnchor:self.view.leadingAnchor], |
| + [self.NTPView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| + [self.NTPView.trailingAnchor |
| + constraintEqualToAnchor:self.view.trailingAnchor], |
| ]]; |
| - // PLACEHOLDER: this logic should move out of the UIVC. |
| - NSString* mostVisited = l10n_util::GetNSString(IDS_IOS_NEW_TAB_MOST_VISITED); |
| - NSString* bookmarks = |
| - l10n_util::GetNSString(IDS_IOS_NEW_TAB_BOOKMARKS_PAGE_TITLE_MOBILE); |
| - NSString* openTabs = l10n_util::GetNSString(IDS_IOS_NEW_TAB_RECENT_TABS); |
| - |
| - NSMutableArray* tabBarItems = [NSMutableArray array]; |
| - |
| - NewTabPageBarItem* mostVisitedItem = [NewTabPageBarItem |
| - newTabPageBarItemWithTitle:mostVisited |
| - identifier:NewTabPage::kMostVisitedPanel |
| - image:[UIImage imageNamed:@"ntp_mv_search"]]; |
| - NewTabPageBarItem* bookmarksItem = [NewTabPageBarItem |
| - newTabPageBarItemWithTitle:bookmarks |
| - identifier:NewTabPage::kBookmarksPanel |
| - image:[UIImage imageNamed:@"ntp_bookmarks"]]; |
| - [tabBarItems addObject:bookmarksItem]; |
| - [tabBarItems addObject:mostVisitedItem]; |
| - |
| - NewTabPageBarItem* openTabsItem = [NewTabPageBarItem |
| - newTabPageBarItemWithTitle:openTabs |
| - identifier:NewTabPage::kOpenTabsPanel |
| - image:[UIImage imageNamed:@"ntp_opentabs"]]; |
| - [tabBarItems addObject:openTabsItem]; |
| - tabBar.items = tabBarItems; |
| + self.NTPView.tabBar.items = _tabBarItems; |
|
lpromero
2017/04/06 13:01:06
Should you DCHECK that _tabBarItems is set? Becaus
justincohen
2017/04/06 18:25:11
Done.
|
| +} |
| + |
| +- (void)viewDidLayoutSubviews { |
| + [super viewDidLayoutSubviews]; |
| + |
| + if (!_homeLoaded) { |
| + // Make sure scrollView is properly set up. |
| + [self.NTPView layoutIfNeeded]; |
| + // PLACEHOLDER: This should come from the mediator. |
| + if (IsIPadIdiom()) { |
| + CGRect itemFrame = [self.NTPView panelFrameForItemAtIndex:1]; |
| + CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| + [self.NTPView.scrollView setContentOffset:point animated:NO]; |
| + } else { |
| + [self.dispatcher showNTPHomePanel]; |
| + } |
| + } |
| +} |
| + |
| +- (void)addControllerToScrollView:(UIViewController*)controller { |
| + [self addChildViewController:controller]; |
| + [self.NTPView.scrollView addSubview:controller.view]; |
| + [controller didMoveToParentViewController:self]; |
| +} |
| + |
| +- (void)addHomePanelViewController:(UIViewController*)controller { |
| + if (IsIPadIdiom()) { |
| + controller.view.frame = [self.NTPView panelFrameForItemAtIndex:1]; |
| + } else { |
| + controller.view.frame = [self.NTPView panelFrameForItemAtIndex:0]; |
| + } |
| + NewTabPageBarItem* item = self.NTPView.tabBar.items[1]; |
|
rohitrao (ping after 24h)
2017/04/06 13:08:13
This is weird -- we get a set of items from the me
justincohen
2017/04/06 18:25:11
More of this logic will move into the mediator in
|
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _homeLoaded = YES; |
| +} |
| + |
| +- (void)addBookmarksViewController:(UIViewController*)controller { |
| + controller.view.frame = [self.NTPView panelFrameForItemAtIndex:0]; |
| + NewTabPageBarItem* item = self.NTPView.tabBar.items[0]; |
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _bookmarksLoaded = YES; |
| +} |
| + |
| +- (void)addOpenTabsViewController:(UIViewController*)controller { |
| + controller.view.frame = [self.NTPView panelFrameForItemAtIndex:2]; |
| + NewTabPageBarItem* item = self.NTPView.tabBar.items[2]; |
| + item.view = controller.view; |
| + [self addControllerToScrollView:controller]; |
| + _openTabsLoaded = YES; |
| +} |
| + |
| +#pragma mark - NTPConsumer |
| + |
| +- (void)setBarItems:(NSMutableArray*)items { |
| + _tabBarItems = items; |
|
lpromero
2017/04/06 13:01:06
This supposes it is called before the view is load
justincohen
2017/04/06 18:25:11
Correct.
|
| +} |
| + |
| +#pragma mark - UIScrollViewDelegate |
| + |
| +- (void)scrollViewDidScroll:(UIScrollView*)scrollView { |
|
lpromero
2017/04/06 13:01:06
Could this logic be in a separate object whose sin
marq (ping after 24h)
2017/04/06 14:30:14
+1
justincohen
2017/04/06 18:25:11
Acknowledged.
justincohen
2017/04/06 18:25:11
There's not that much more logic necessary in this
|
| + // Position is used to track the exact X position of the scroll view, whereas |
| + // index is rounded to the panel that is most visible. |
| + CGFloat panelWidth = |
| + scrollView.contentSize.width / self.NTPView.tabBar.items.count; |
| + LayoutOffset position = |
| + LeadingContentOffsetForScrollView(scrollView) / panelWidth; |
| + NSUInteger index = round(position); |
| + |
| + // |scrollView| can be out of range when the frame changes. |
| + if (index >= self.NTPView.tabBar.items.count) |
| + return; |
| + |
| + NewTabPageBarItem* item = self.NTPView.tabBar.items[index]; |
| + if (item.identifier == NewTabPage::kBookmarksPanel && !_bookmarksLoaded) |
| + [self.dispatcher showNTPBookmarksPanel]; |
| + else if (item.identifier == NewTabPage::kMostVisitedPanel && !_homeLoaded) |
| + [self.dispatcher showNTPHomePanel]; |
| + else if (item.identifier == NewTabPage::kOpenTabsPanel && !_openTabsLoaded) |
| + [self.dispatcher showNTPOpenTabsPanel]; |
| + |
| + // If index changed, follow same path as if a tab bar item was pressed. When |
| + // |index| == |position|, the panel is completely in view. |
| + if (index == position && self.NTPView.tabBar.selectedIndex != index) { |
| + NewTabPageBarItem* item = [self.NTPView.tabBar.items objectAtIndex:index]; |
| + DCHECK(item); |
| + self.NTPView.tabBar.selectedIndex = index; |
| + } |
| + [self.NTPView.tabBar updateColorsForScrollView:scrollView]; |
| + |
| + self.NTPView.tabBar.overlayPercentage = |
| + scrollView.contentOffset.x / scrollView.contentSize.width; |
| +} |
| + |
| +#pragma mark - NewTabPageBarDelegate |
| + |
| +- (void)newTabBarItemDidChange:(NewTabPageBarItem*)selectedItem |
| + changePanel:(BOOL)changePanel { |
|
lpromero
2017/04/06 13:01:06
This is unused. Will it stay unused?
justincohen
2017/04/06 18:25:11
This is not unused. It's set in |tabBar.delegate
|
| + if (IsIPadIdiom()) { |
| + NSUInteger index = [self.NTPView.tabBar.items indexOfObject:selectedItem]; |
| + CGRect itemFrame = [self.NTPView panelFrameForItemAtIndex:index]; |
| + CGPoint point = CGPointMake(CGRectGetMinX(itemFrame), 0); |
| + [self.NTPView.scrollView setContentOffset:point animated:YES]; |
| + } else { |
| + if (selectedItem.identifier == NewTabPage::kBookmarksPanel) { |
| + [self.dispatcher showNTPBookmarksPanel]; |
| + } else if (selectedItem.identifier == NewTabPage::kOpenTabsPanel) { |
| + [self.dispatcher showNTPOpenTabsPanel]; |
| + } |
| + } |
| } |
| @end |