Skip to content

Commit

Permalink
feat(android): return Promise from Ti.UI.Window.close()
Browse files Browse the repository at this point in the history
* properly resolve Ti.UI.NavigationWindow.close()
* use constant reference for close event name
* move rejection of closing unopened window up hierarchy so it applies to TabGroup too
  • Loading branch information
sgtcoolguy committed Feb 8, 2021
1 parent 2fda671 commit d67537f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.view.TiUIActivityWindow;
import org.appcelerator.titanium.view.TiUIView;

Expand Down Expand Up @@ -40,7 +41,7 @@ protected void handleClose(@NonNull KrollDict options)
{
Log.d(TAG, "handleClose", Log.DEBUG_MODE);
opened = false;
fireEvent("close", null);
fireEvent(TiC.EVENT_CLOSE, null);

if (view != null) {
((TiUIActivityWindow) view).close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package ti.modules.titanium.ui;

import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollPromise;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.titanium.TiC;
Expand All @@ -14,6 +15,8 @@
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;

@Kroll.proxy(creatableInModule = UIModule.class)
public class NavigationWindowProxy extends WindowProxy
{
Expand Down Expand Up @@ -55,17 +58,19 @@ public void popToRootWindow(@Kroll.argument(optional = true) Object arg)
}
}

@Override
@Kroll.method
public void close(@Kroll.argument(optional = true) Object arg)
protected void handleClose(@NonNull KrollDict options)
{
if (opened) {
opened = false;
popToRootWindow(arg);
closeWindow(windows.get(0), arg); // close the root window
popToRootWindow(options);
closeWindow(windows.get(0), options); // close the root window
fireEvent(TiC.EVENT_CLOSE, null);
if (closePromise != null) {
closePromise.resolve(null);
closePromise = null;
}
}
super.close(arg);
super.handleClose(options);
}

@Kroll.method
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,15 +131,6 @@ public KrollPromise<Void> open(@Kroll.argument(optional = true) Object arg)
return super.open(arg);
}

@Override
public void close(@Kroll.argument(optional = true) Object arg)
{
if (!(opened || opening)) {
return;
}
super.close(arg);
}

@Override
protected void handleOpen(KrollDict options)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ public abstract class TiWindowProxy extends TiViewProxy
protected boolean windowActivityCreated = false;
protected List<Pair<View, String>> sharedElementPairs;
public TiWindowProxy navigationWindow;
private KrollPromise openPromise;
protected KrollPromise openPromise;
protected KrollPromise closePromise;

public interface PostOpenListener {
void onPostOpen(TiWindowProxy window);
Expand Down Expand Up @@ -107,10 +108,10 @@ public KrollPromise<Void> open(@Kroll.argument(optional = true) Object arg)
promise.reject(new Throwable("Window is already opened or opening."));
});
}
opening = true;
waitingForOpen = new WeakReference<TiWindowProxy>(this);

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

Expand Down Expand Up @@ -149,27 +150,36 @@ public boolean isFocused()

@SuppressWarnings("unchecked")
@Kroll.method
public void close(@Kroll.argument(optional = true) Object arg)
public KrollPromise<Void> close(@Kroll.argument(optional = true) Object arg)
{
// TODO: if not opened, ignore? We do this in WindowProxy subclass, but not the other two...
KrollDict options = null;
TiAnimation animation = null;
if (!(opened || opening)) {
return KrollPromise.create((promise) -> {
promise.reject(new Throwable("Window is not open or opening, so cannot be closed."));
});
}

// FIXME: Can we "cancel" the open() promise if it's not finished?
closePromise = KrollPromise.create((promise) -> {
KrollDict options = null;
TiAnimation animation = null;

if (arg != null) {
if (arg instanceof HashMap<?, ?>) {
options = new KrollDict((HashMap<String, Object>) arg);
if (arg != null) {
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();
}

handleClose(options);
// FIXME: Maybe fire the close event here and set opened to false as well, rather than leaving to subclasses?
handleClose(options);
// FIXME: Maybe fire the close event here and set opened to false as well, rather than leaving to subclasses?
});
return closePromise;
}

public void closeFromActivity(boolean activityIsFinishing)
Expand All @@ -196,6 +206,10 @@ public void closeFromActivity(boolean activityIsFinishing)
// And it will dispose the handler of the window in the JS if the activity
// is not forced to destroy.
fireSyncEvent(TiC.EVENT_CLOSE, data);
if (closePromise != null) {
closePromise.resolve(null);
closePromise = null; // FIXME: call release() first?
}
}

public void addProxyWaitingForActivity(KrollProxy waitingProxy)
Expand Down

0 comments on commit d67537f

Please sign in to comment.