Skip to content

Commit

Permalink
feat(android): return Promise from Ti.UI.Window.open()
Browse files Browse the repository at this point in the history
  • Loading branch information
sgtcoolguy committed Feb 8, 2021
1 parent 5fa6258 commit 2fda671
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2018 by Axway, Inc. All Rights Reserved.
* Copyright (c) 2018-2021 by Axway, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollPromise;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.proxy.TiWindowProxy;
Expand All @@ -27,7 +28,7 @@ public NavigationWindowProxy()

@Override
@Kroll.method
public void open(@Kroll.argument(optional = true) Object arg)
public KrollPromise<Void> open(@Kroll.argument(optional = true) Object arg)
{
// FIXME: Shouldn't this complain/blow up if window isn't specified?
if (!opened && getProperties().containsKeyAndNotNull(TiC.PROPERTY_WINDOW)) {
Expand All @@ -37,9 +38,11 @@ public void open(@Kroll.argument(optional = true) Object arg)
openWindow(rootView, arg);
fireEvent(TiC.EVENT_OPEN, null);
}
return;
return KrollPromise.create((promise) -> {
promise.resolve(null);
});
}
super.open(arg);
return super.open(arg);
}

@Kroll.method
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2013 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2013-2021 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -11,6 +11,7 @@
import java.util.HashMap;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollPromise;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiActivity;
Expand Down Expand Up @@ -105,7 +106,7 @@ public TiUIView createView(Activity activity)
}

@Override
public void open(@Kroll.argument(optional = true) Object arg)
public KrollPromise<Void> open(@Kroll.argument(optional = true) Object arg)
{
HashMap<String, Object> option = null;
if (arg instanceof HashMap) {
Expand All @@ -127,7 +128,7 @@ public void open(@Kroll.argument(optional = true) Object arg)
properties.remove(TiC.PROPERTY_BOTTOM);
properties.remove(TiC.PROPERTY_LEFT);
properties.remove(TiC.PROPERTY_RIGHT);
super.open(arg);
return super.open(arg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Copyright (c) 2009-2021 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
Expand All @@ -12,6 +12,7 @@
import java.util.List;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollPromise;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
Expand Down Expand Up @@ -72,6 +73,7 @@ public abstract class TiWindowProxy extends TiViewProxy
protected boolean windowActivityCreated = false;
protected List<Pair<View, String>> sharedElementPairs;
public TiWindowProxy navigationWindow;
private KrollPromise openPromise;

public interface PostOpenListener {
void onPostOpen(TiWindowProxy window);
Expand All @@ -98,34 +100,39 @@ public TiUIView createView(Activity activity)

@Kroll.method
@SuppressWarnings("unchecked")
public void open(@Kroll.argument(optional = true) Object arg)
public KrollPromise<Void> open(@Kroll.argument(optional = true) Object arg)
{
if (opened || opening) {
return;
return KrollPromise.create((promise) -> {
promise.reject(new Throwable("Window is already opened or opening."));
});
}

waitingForOpen = new WeakReference<TiWindowProxy>(this);
opening = true;
KrollDict options = null;
TiAnimation animation = null;
openPromise = KrollPromise.create((promise) -> {
waitingForOpen = new WeakReference<TiWindowProxy>(this);
opening = true;
KrollDict options = null;
TiAnimation animation = null;

if (arg != null) {
if (arg instanceof KrollDict) {
options = (KrollDict) arg;
if (arg != null) {
if (arg instanceof KrollDict) {
options = (KrollDict) arg;

} else if (arg instanceof HashMap<?, ?>) {
options = new KrollDict((HashMap<String, Object>) arg);
} else if (arg instanceof HashMap<?, ?>) {
options = new KrollDict((HashMap<String, Object>) arg);

} else if (arg instanceof TiAnimation) {
} else if (arg instanceof TiAnimation) {
options = new KrollDict();
options.put("_anim", animation);
}

} else {
options = new KrollDict();
options.put("_anim", animation);
}

} else {
options = new KrollDict();
}

handleOpen(options);
handleOpen(options);
});
return openPromise;
}

@Kroll.getProperty(name = "closed")
Expand Down Expand Up @@ -505,6 +512,11 @@ public void run()
if (nativeView != null) {
nativeView.postInvalidate();
}

if (openPromise != null) {
openPromise.resolve(null);
openPromise = null;
}
}

protected void fillIntent(Activity activity, Intent intent)
Expand Down
5 changes: 3 additions & 2 deletions common/Resources/ti.internal/extensions/ti/ui/tabgroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ if (OS_ANDROID) {
TabGroup.prototype.open = function (options) {

if (this.currentState === this.state.opened) {
return;
return Promise.reject(new Error('Window is already opened or opening.'));
}

this.currentState = this.state.opening;
Expand Down Expand Up @@ -104,9 +104,10 @@ if (OS_ANDROID) {
if (this._activeTab !== -1) {
this.setActiveTab(this._activeTab);
}
_open.call(this, options);
const result = _open.call(this, options);

this.currentState = this.state.opened;
return result;
};

const _addTab = TabGroup.prototype.addTab;
Expand Down
2 changes: 1 addition & 1 deletion common/Resources/ti.internal/extensions/ti/ui/window.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (OS_ANDROID) {
}
});

_open.call(this, options);
return _open.call(this, options);
};

const _add = Window.prototype.add;
Expand Down

0 comments on commit 2fda671

Please sign in to comment.