Skip to content

Commit 5fa6258

Browse files
committed
feat(ios): return Promise from Ti.UI.Window.open()/close()
1 parent 91d0564 commit 5fa6258

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed

iphone/TitaniumKit/TitaniumKit/Sources/API/TiControllerProtocols.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
/**
22
* Appcelerator Titanium Mobile
3-
* Copyright (c) 2013 by Appcelerator, Inc. All Rights Reserved.
3+
* Copyright (c) 2013-Present by Appcelerator, Inc. All Rights Reserved.
44
* Licensed under the terms of the Apache Public License
55
* Please see the LICENSE included with this distribution for details.
66
*/
77

88
#import "TiViewProxy.h"
99
#import <Foundation/Foundation.h>
1010

11+
@class KrollPromise;
12+
1113
/**
1214
Protocol for orientation controller.
1315
*/
@@ -24,8 +26,8 @@
2426
*/
2527
@protocol TiWindowProtocol <TiOrientationController>
2628

27-
- (void)open:(id)args;
28-
- (void)close:(id)args;
29+
- (KrollPromise *)open:(id)args;
30+
- (KrollPromise *)close:(id)args;
2931
- (BOOL)_handleOpen:(id)args;
3032
- (BOOL)_handleClose:(id)args;
3133
- (BOOL)opening;

iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/**
22
* Appcelerator Titanium Mobile
3-
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
3+
* Copyright (c) 2009-2021 by Appcelerator, Inc. All Rights Reserved.
44
* Licensed under the terms of the Apache Public License
55
* Please see the LICENSE included with this distribution for details.
66
*/
@@ -10,6 +10,8 @@
1010

1111
#import "TiUIiOSTransitionAnimationProxy.h"
1212

13+
@class KrollPromise;
14+
1315
@interface TiWindowProxy : TiViewProxy <TiWindowProtocol, TiAnimationDelegate> {
1416
@protected
1517
TiViewController *controller;
@@ -28,6 +30,8 @@
2830
TiAnimation *closeAnimation;
2931
UIView *animatedOver;
3032
TiUIiOSTransitionAnimationProxy *transitionProxy;
33+
KrollPromise *openPromise;
34+
KrollPromise *closePromise;
3135
}
3236

3337
@property (nonatomic, readwrite, assign) TiViewProxy<TiTab> *tab;

iphone/TitaniumKit/TitaniumKit/Sources/API/TiWindowProxy.m

+53-17
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/**
22
* Appcelerator Titanium Mobile
3-
* Copyright (c) 2009-2014 by Appcelerator, Inc. All Rights Reserved.
3+
* Copyright (c) 2009-2021 by Appcelerator, Inc. All Rights Reserved.
44
* Licensed under the terms of the Apache Public License
55
* Please see the LICENSE included with this distribution for details.
66
*/
77

88
#import "TiWindowProxy.h"
9+
#import "KrollPromise.h"
910
#import "TiApp.h"
1011
#import "TiErrorController.h"
1112
#import "TiUIWindow.h"
@@ -37,6 +38,9 @@ - (void)dealloc
3738
RELEASE_TO_NIL(transitionProxy)
3839
}
3940

41+
RELEASE_TO_NIL(openPromise);
42+
RELEASE_TO_NIL(closePromise);
43+
4044
[super dealloc];
4145
}
4246

@@ -113,6 +117,10 @@ - (void)windowDidOpen
113117
if ([self _hasListeners:@"open"]) {
114118
[self fireEvent:@"open" withObject:nil withSource:self propagate:NO reportSuccess:NO errorCode:0 message:nil];
115119
}
120+
if (openPromise != nil) {
121+
[openPromise resolve:@[]];
122+
RELEASE_TO_NIL(openPromise);
123+
}
116124
if (focussed) {
117125
[self fireFocusEvent];
118126
}
@@ -140,6 +148,10 @@ - (void)windowDidClose
140148
if ([self _hasListeners:@"close"]) {
141149
[self fireEvent:@"close" withObject:nil withSource:self propagate:NO reportSuccess:NO errorCode:0 message:nil];
142150
}
151+
if (closePromise != nil) {
152+
[closePromise resolve:@[]];
153+
RELEASE_TO_NIL(closePromise);
154+
}
143155
[self forgetProxy:closeAnimation];
144156
RELEASE_TO_NIL(closeAnimation);
145157
if (tab == nil && !self.isManaged) {
@@ -202,32 +214,39 @@ - (BOOL)isRootViewAttached
202214
}
203215

204216
#pragma mark - TiWindowProtocol Base Methods
205-
- (void)open:(id)args
217+
- (KrollPromise *)open:(id)args
206218
{
207-
//If an error is up, Go away
219+
JSContext *context = [self currentContext];
220+
221+
// If an error is up, Go away
208222
if ([[[[TiApp app] controller] topPresentedController] isKindOfClass:[TiErrorNavigationController class]]) {
209223
DebugLog(@"[ERROR] ErrorController is up. ABORTING open");
210-
return;
224+
return [KrollPromise rejectedWithErrorMessage:@"ErrorController is up. ABORTING open" inContext:context];
211225
}
212226

213-
//I am already open or will be soon. Go Away
227+
// I am already open or will be soon. Go Away
214228
if (opening || opened) {
215-
return;
229+
return [KrollPromise rejectedWithErrorMessage:@"Window is already opened or opening." inContext:context];
216230
}
217231

218-
//Lets keep ourselves safe
232+
// Lets keep ourselves safe
219233
[self rememberSelf];
220234

235+
// Don't recreate if we're calling ourselves again because root view is not loaded/attached!
236+
if (openPromise == nil) {
237+
openPromise = [[KrollPromise alloc] initInContext:context];
238+
}
239+
221240
//Make sure our RootView Controller is attached
222241
if (![self isRootViewLoaded]) {
223242
DebugLog(@"[WARN] ROOT VIEW NOT LOADED. WAITING");
224243
[self performSelector:@selector(open:) withObject:args afterDelay:0.1];
225-
return;
244+
return openPromise;
226245
}
227246
if (![self isRootViewAttached]) {
228247
DebugLog(@"[WARN] ROOT VIEW NOT ATTACHED. WAITING");
229248
[self performSelector:@selector(open:) withObject:args afterDelay:0.1];
230-
return;
249+
return openPromise;
231250
}
232251

233252
opening = YES;
@@ -262,6 +281,7 @@ - (void)open:(id)args
262281
[self openOnUIThread:args];
263282
},
264283
NO);
284+
return openPromise;
265285
}
266286

267287
- (void)setStatusBarStyle:(id)style
@@ -278,23 +298,26 @@ - (void)setStatusBarStyle:(id)style
278298
}
279299
}
280300

281-
- (void)close:(id)args
301+
- (KrollPromise *)close:(id)args
282302
{
303+
JSContext *context = [self currentContext];
304+
283305
if (!opened) {
284306
// If I've been asked to open but haven't yet, short-circuit it and tell it not to open
285307
if (opening) {
286308
opening = NO; // _handleOpen: should check this and abort opening
287309
DebugLog(@"Window is not open yet. Attempting to stop it from opening...");
288-
return;
310+
// Should we reject or resolve here?! This feels more like "success", so maybe resolve? or do we have to wait on the end result of the openPromise to know?
311+
return [KrollPromise resolved:@[] inContext:context];
289312
}
290313

291314
DebugLog(@"Window is not open. Ignoring this close call");
292-
return;
315+
return [KrollPromise rejectedWithErrorMessage:@"Window is not open. Ignoring this close call" inContext:context];
293316
}
294317

295318
if (closing) {
296319
DebugLog(@"Window is already closing. Ignoring this close call.");
297-
return;
320+
return [KrollPromise rejectedWithErrorMessage:@"Window is already closing. Ignoring this close call." inContext:context];
298321
}
299322

300323
if (tab != nil) {
@@ -303,22 +326,26 @@ - (void)close:(id)args
303326
} else {
304327
args = [NSArray arrayWithObject:self];
305328
}
306-
[tab closeWindow:args];
307-
return;
329+
return [tab closeWindow:args];
330+
}
331+
332+
if (closePromise == nil) {
333+
closePromise = [[KrollPromise alloc] initInContext:context];
308334
}
309335

310336
closing = YES;
311337

312-
//TODO Argument Processing
338+
// TODO: Argument Processing
313339
closeAnimation = [[TiAnimation animationFromArg:args context:[self pageContext] create:NO] retain];
314340
[self rememberProxy:closeAnimation];
315341

316-
//GO ahead and call close on UI thread
342+
// GO ahead and call close on UI thread
317343
TiThreadPerformOnMainThread(
318344
^{
319345
[self closeOnUIThread:args];
320346
},
321347
NO);
348+
return closePromise;
322349
}
323350

324351
- (NSNumber *)closed
@@ -595,6 +622,11 @@ - (void)openOnUIThread:(NSArray *)args
595622
opened = NO;
596623
[self forgetProxy:openAnimation];
597624
RELEASE_TO_NIL(openAnimation);
625+
// reject the openPromise!
626+
if (openPromise != nil) {
627+
[openPromise rejectWithErrorMessage:@"open aborted"];
628+
RELEASE_TO_NIL(openPromise);
629+
}
598630
}
599631
}
600632

@@ -619,6 +651,10 @@ - (void)closeOnUIThread:(NSArray *)args
619651
DebugLog(@"[WARN] CLOSE ABORTED. _handleClose returned NO");
620652
closing = NO;
621653
RELEASE_TO_NIL(closeAnimation);
654+
if (closePromise != nil) {
655+
[closePromise rejectWithErrorMessage:@"close aborted"];
656+
RELEASE_TO_NIL(closePromise);
657+
}
622658
}
623659
}
624660

0 commit comments

Comments
 (0)