Skip to content

Commit

Permalink
feat(ios): support Ti.UI.NavigationWindow#open()/close() returning Pr…
Browse files Browse the repository at this point in the history
…omise
  • Loading branch information
sgtcoolguy committed Feb 8, 2021
1 parent 9c89d6d commit ca09400
Showing 1 changed file with 37 additions and 12 deletions.
49 changes: 37 additions & 12 deletions iphone/Classes/TiUINavigationWindowProxy.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

#import "TiUINavigationWindowProxy.h"
#import "TiUINavigationWindowInternal.h"
#import <TitaniumKit/KrollPromise.h>
#import <TitaniumKit/TiApp.h>

@implementation TiUINavigationWindowProxy
Expand Down Expand Up @@ -83,7 +84,6 @@ - (UINavigationController *)controller
{
if (navController == nil) {
navController = [[UINavigationController alloc] initWithRootViewController:[self rootController]];
;
navController.delegate = self;
[TiUtils configureController:navController withObject:self];
[navController.interactivePopGestureRecognizer addTarget:self action:@selector(popGestureStateHandler:)];
Expand All @@ -102,15 +102,17 @@ - (BOOL)gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
return !isRootWindow;
}

- (void)openWindow:(NSArray *)args
- (KrollPromise *)openWindow:(NSArray *)args
{
TiWindowProxy *window = [args objectAtIndex:0];
ENSURE_TYPE(window, TiWindowProxy);

JSContext *context = [self currentContext];

if (window == rootWindow) {
[rootWindow windowWillOpen];
[rootWindow windowDidOpen];
return;
return [KrollPromise resolved:@[] inContext:context];
}
[window setIsManaged:YES];
[window setTab:(TiViewProxy<TiTab> *)self];
Expand All @@ -121,31 +123,40 @@ - (void)openWindow:(NSArray *)args
if (args != nil) {
args = [NSArray arrayWithObject:args];
}
[window open:args];
return;
return [window open:args]; // return underlying promise
}

KrollPromise *promise = [[[KrollPromise alloc] initInContext:context] autorelease];
BOOL animated = args != nil && [args count] > 1 ? [TiUtils boolValue:@"animated" properties:[args objectAtIndex:1] def:YES] : YES;
[[[TiApp app] controller] dismissKeyboard];
TiThreadPerformOnMainThread(
^{
[self pushOnUIThread:args];
[self pushOnUIThread:@[ window, [NSNumber numberWithBool:animated], promise ]];
},
YES);
return promise;
}

- (void)closeWindow:(NSArray *)args
- (KrollPromise *)closeWindow:(NSArray *)args
{
TiWindowProxy *window = [args objectAtIndex:0];
ENSURE_TYPE(window, TiWindowProxy);

JSContext *context = [self currentContext];

if (window == rootWindow && ![[TiApp app] willTerminate]) {
DebugLog(@"[ERROR] Can not close the root window of the NavigationWindow. Close the NavigationWindow instead.");
return;
return [KrollPromise rejectedWithErrorMessage:@"Can not close the root window of the NavigationWindow. Close the NavigationWindow instead." inContext:context];
}

KrollPromise *promise = [[[KrollPromise alloc] initInContext:context] autorelease];
BOOL animated = args != nil && [args count] > 1 ? [TiUtils boolValue:@"animated" properties:[args objectAtIndex:1] def:YES] : YES;
TiThreadPerformOnMainThread(
^{
[self popOnUIThread:args];
[self popOnUIThread:@[ window, [NSNumber numberWithBool:animated], promise ]];
},
YES);
return promise;
}

- (void)popToRootWindow:(id)args
Expand Down Expand Up @@ -281,19 +292,25 @@ - (void)pushOnUIThread:(NSArray *)args
transitionIsAnimating = YES;
}

KrollPromise *promise = [args objectAtIndex:2];
@try {
TiWindowProxy *window = [args objectAtIndex:0];
BOOL animated = args != nil && [args count] > 1 ? [TiUtils boolValue:@"animated" properties:[args objectAtIndex:1] def:YES] : YES;

// Prevent UIKit crashes when trying to push a window while it's already in the nav stack (e.g. on really slow devices)
if ([[[self rootController].navigationController viewControllers] containsObject:window.hostingController]) {
NSLog(@"[WARN] Trying to push a view controller that is already in the navigation window controller stack. Skipping open …");
return;
}

BOOL animated = [[args objectAtIndex:1] boolValue];
[navController pushViewController:[window hostingController] animated:animated];
if (promise != nil) {
[promise resolve:@[]];
}
} @catch (NSException *ex) {
NSLog(@"[ERROR] %@", ex.description);
if (promise != nil) {
[promise rejectWithErrorMessage:ex.description];
}
}
}

Expand All @@ -304,15 +321,23 @@ - (void)popOnUIThread:(NSArray *)args
return;
}
TiWindowProxy *window = [args objectAtIndex:0];
BOOL animated = [[args objectAtIndex:1] boolValue];
KrollPromise *promise = [args objectAtIndex:2];

if (window == current) {
BOOL animated = args != nil && [args count] > 1 ? [TiUtils boolValue:@"animated" properties:[args objectAtIndex:1] def:YES] : YES;
if (animated && !transitionWithGesture) {
transitionIsAnimating = YES;
}
[navController popViewControllerAnimated:animated];
if (promise != nil) {
[promise resolve:@[]];
}
} else {
// FIXME: forward/chain the underying promise from [window close:] done internally here rather than assume success
[self closeWindow:window animated:NO];
if (promise != nil) {
[promise resolve:@[]];
}
}
}

Expand Down

0 comments on commit ca09400

Please sign in to comment.