Skip to content

Commit

Permalink
Fix popping and pushing on the iPad.
Browse files Browse the repository at this point in the history
  • Loading branch information
grp committed Jan 15, 2013
1 parent a764908 commit dc09521
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 31 deletions.
10 changes: 8 additions & 2 deletions Classes/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
UIWindow *window;

SplitViewController *splitController;
EmptyController *emptyController;

NavigationController *navigationController;
NavigationController *rightNavigationController;
Expand All @@ -29,12 +28,19 @@

- (void)pushBranchViewController:(UIViewController *)branchController animated:(BOOL)animated;
- (void)pushLeafViewController:(UIViewController *)leafController animated:(BOOL)animated;
- (void)setLeafViewController:(UIViewController *)leafController;

- (BOOL)leafContainsViewController:(UIViewController *)leafController;
- (void)setLeafViewController:(UIViewController *)leafController animated:(BOOL)animated;
- (void)clearLeafViewControllerAnimated:(BOOL)animated;

- (void)popBranchToViewController:(UIViewController *)branchController animated:(BOOL)animated;
- (void)popLeafToViewController:(UIViewController *)leafController animated:(BOOL)animated;

@end

@interface UINavigationController (AppDelegate)

- (void)pushController:(UIViewController *)controller animated:(BOOL)animated;
- (void)popToController:(UIViewController *)controller animated:(BOOL)animated;

@end
66 changes: 54 additions & 12 deletions Classes/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,30 @@ - (BOOL)controllerBelongsOnLeft:(UIViewController *)controller {
|| [controller isKindOfClass:[SubmissionListController class]];
}

- (BOOL)controllerRequiresClearing:(UIViewController *)controller {
return [controller isKindOfClass:[SessionListController class]];
}

- (void)popToController:(UIViewController *)controller animated:(BOOL)animated {
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
if ([[self viewControllers] containsObject:controller]) {
[delegate popBranchToViewController:controller animated:animated];

if ([self controllerRequiresClearing:controller]) {
[delegate clearLeafViewControllerAnimated:animated];
}
} else if ([delegate leafContainsViewController:controller]) {
[delegate popLeafToViewController:controller animated:animated];
} else {
[NSException raise:@"UINavigationControllerPopException" format:@"can't find where to pop"];
}
} else {
[delegate popBranchToViewController:controller animated:animated];
}
}

- (void)pushController:(UIViewController *)controller animated:(BOOL)animated {
AppDelegate *delegate = (AppDelegate *) [[UIApplication sharedApplication] delegate];

Expand All @@ -50,7 +74,7 @@ - (void)pushController:(UIViewController *)controller animated:(BOOL)animated {
if ([self controllerBelongsOnLeft:controller]) {
[delegate pushBranchViewController:controller animated:animated];
} else if ([self controllerBelongsOnLeft:[self topViewController]]) {
[delegate setLeafViewController:controller];
[delegate setLeafViewController:controller animated:animated];
} else {
[delegate pushLeafViewController:controller animated:animated];
}
Expand All @@ -70,9 +94,7 @@ - (void)pushController:(UIViewController *)controller animated:(BOOL)animated {
}
}
} else {
if (![controller isKindOfClass:[EmptyController class]]) {
[delegate pushBranchViewController:controller animated:animated];
}
[delegate pushBranchViewController:controller animated:animated];
}
}

Expand Down Expand Up @@ -125,22 +147,21 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(

[HNEntryBodyRenderer setDefaultFontSize:13.0f];
} else if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
emptyController = [[EmptyController alloc] init];
[emptyController autorelease];

rightNavigationController = [[NavigationController alloc] initWithRootViewController:emptyController];
rightNavigationController = [[NavigationController alloc] init];
[rightNavigationController setLoginDelegate:sessionListController];
[rightNavigationController setDelegate:self];
[rightNavigationController autorelease];


[self clearLeafViewControllerAnimated:NO];

splitController = [[SplitViewController alloc] init];
[splitController setViewControllers:[NSArray arrayWithObjects:navigationController, rightNavigationController, nil]];
if ([splitController respondsToSelector:@selector(setPresentsWithGesture:)]) [splitController setPresentsWithGesture:YES];
[splitController setDelegate:self];
[splitController autorelease];

[window setRootViewController:splitController];

[HNEntryBodyRenderer setDefaultFontSize:16.0f];
} else {
NSAssert(NO, @"Invalid Device Type");
Expand Down Expand Up @@ -209,11 +230,32 @@ - (void)pushLeafViewController:(UIViewController *)leafController animated:(BOOL
[rightNavigationController pushViewController:leafController animated:animated];
}

- (void)setLeafViewController:(UIViewController *)leafController {
- (BOOL)leafContainsViewController:(UIViewController *)leafController {
return [[rightNavigationController viewControllers] containsObject:leafController];
}

- (void)setLeafViewController:(UIViewController *)leafController animated:(BOOL)animated {
[rightNavigationController setViewControllers:[NSArray arrayWithObject:leafController]];

if (popoverItem != nil) [[leafController navigationItem] addLeftBarButtonItem:popoverItem atPosition:UINavigationItemPositionLeft];
if (popover != nil) [popover dismissPopoverAnimated:YES];
if (popover != nil) [popover dismissPopoverAnimated:animated];
}

- (void)clearLeafViewControllerAnimated:(BOOL)animated {
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
EmptyController *emptyController = [[EmptyController alloc] init];
if (popoverItem != nil) [[emptyController navigationItem] addLeftBarButtonItem:popoverItem atPosition:UINavigationItemPositionLeft];
[rightNavigationController setViewControllers:[NSArray arrayWithObject:emptyController]];
[emptyController release];
}
}

- (void)popBranchToViewController:(UIViewController *)branchController animated:(BOOL)animated {
[navigationController popToViewController:branchController animated:animated];
}

- (void)popLeafToViewController:(UIViewController *)leafController animated:(BOOL)animated {
[rightNavigationController popToViewController:leafController animated:animated];
}

#pragma mark - Startup Connection
Expand Down
19 changes: 2 additions & 17 deletions Classes/Controllers/SessionListController.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,6 @@ - (void)viewDidLoad {
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];

[self clearRightControllerIfNecessary];

NSIndexPath *selectedIndexPath = [tableView indexPathForSelectedRow];
HNSession *session = nil;

Expand Down Expand Up @@ -126,12 +124,6 @@ - (void)reloadSessions {
sessions = [[[HNSessionController sessionController] sessions] retain];
}

- (void)clearRightControllerIfNecessary {
EmptyController *emptyController = [[EmptyController alloc] init];
[[self navigationController] pushController:emptyController animated:NO];
[EmptyController release];
}

- (void)pushMainControllerForSession:(HNSession *)session animated:(BOOL)animated {
[[HNSessionController sessionController] setRecentSession:session];

Expand All @@ -147,13 +139,6 @@ - (void)pushMainControllerForSession:(HNSession *)session animated:(BOOL)animate

[[self navigationController] pushController:tabBarController animated:animated];
[tabBarController release];

if (!animated) {
// If we aren't animated, we are expecting the controller to be pushed
// instantly. However, UINavigationController takes a run loop iteration
// to actually perform the push, so let that happen before we return.
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]];
}
}

- (void)pushAnonymousSessionIfNecessaryAnimated:(BOOL)animated {
Expand Down Expand Up @@ -189,14 +174,14 @@ - (void)doneFromBarButtonItem:(BarButtonItem *)barButtonItem {
- (void)navigationController:(NavigationController *)navigationController didLoginWithSession:(HNSession *)session {
if ([navigationController topViewController] != self) {
[self setAutomaticDisplaySession:session];
[[self navigationController] popToViewController:self animated:YES];
[[self navigationController] popToController:self animated:YES];
} else {
[self pushMainControllerForSession:session animated:YES];
}
}

- (void)navigationControllerRequestedSessions:(NavigationController *)navigationController {
[[self navigationController] popToViewController:self animated:YES];
[[self navigationController] popToController:self animated:YES];
}

- (void)addSessionFromBarButtonItem:(BarButtonItem *)barButtonItem {
Expand Down

0 comments on commit dc09521

Please sign in to comment.