Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Busy-wait for activity on getLaunchArgs() #7822

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.reactnativenavigation.react;

import static com.reactnativenavigation.utils.UiUtils.pxToDp;

import android.app.Activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand Down Expand Up @@ -32,10 +36,6 @@
import java.util.ArrayList;
import java.util.Objects;

import static com.reactnativenavigation.utils.UiUtils.pxToDp;

import android.app.Activity;

public class NavigationModule extends ReactContextBaseJavaModule {
private static final String NAME = "RNNBridgeModule";

Expand Down Expand Up @@ -87,6 +87,12 @@ public String getName() {

@ReactMethod
public void getLaunchArgs(String commandId, Promise promise) {
// This is work-around for the RN problem described here:
// https://github.com/facebook/react-native/issues/37518
Activity activity = activity();
if (activity == null) {
NullRNActivityWorkaround.waitForActivity(getReactApplicationContext());
}
promise.resolve(LaunchArgsParser.parse(activity()));
}

Expand Down Expand Up @@ -114,15 +120,15 @@ public WritableMap getNavigationConstantsSync() {
@ReactMethod
public void setRoot(String commandId, ReadableMap rawLayoutTree, Promise promise) {
final LayoutNode layoutTree = LayoutNodeParser.parse(Objects.requireNonNull(jsonParser.parse(rawLayoutTree).optJSONObject("root")));
handle(() -> {
runOnUIThread(() -> {
final ViewController<?> viewController = layoutFactory.create(layoutTree);
navigator().setRoot(viewController, new NativeCommandListener("setRoot", commandId, promise, eventEmitter, now), reactInstanceManager);
});
}

@ReactMethod
public void setDefaultOptions(ReadableMap options) {
handle(() -> {
runOnUIThread(() -> {
Options defaultOptions = parse(options);
layoutFactory.setDefaultOptions(defaultOptions);
navigator().setDefaultOptions(defaultOptions);
Expand All @@ -131,21 +137,21 @@ public void setDefaultOptions(ReadableMap options) {

@ReactMethod
public void mergeOptions(String onComponentId, @Nullable ReadableMap options) {
handle(() -> navigator().mergeOptions(onComponentId, parse(options)));
runOnUIThread(() -> navigator().mergeOptions(onComponentId, parse(options)));
}

@ReactMethod
public void push(String commandId, String onComponentId, ReadableMap rawLayoutTree, Promise promise) {
final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree));
handle(() -> {
runOnUIThread(() -> {
final ViewController<?> viewController = layoutFactory.create(layoutTree);
navigator().push(onComponentId, viewController, new NativeCommandListener("push", commandId, promise, eventEmitter, now));
});
}

@ReactMethod
public void setStackRoot(String commandId, String onComponentId, ReadableArray children, Promise promise) {
handle(() -> {
runOnUIThread(() -> {
ArrayList<ViewController<?>> _children = new ArrayList<>();
for (int i = 0; i < children.size(); i++) {
final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(children.getMap(i)));
Expand All @@ -157,58 +163,67 @@ public void setStackRoot(String commandId, String onComponentId, ReadableArray c

@ReactMethod
public void pop(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
handle(() -> navigator().pop(componentId, parse(mergeOptions), new NativeCommandListener("pop", commandId, promise, eventEmitter, now)));
runOnUIThread(() -> navigator().pop(componentId, parse(mergeOptions), new NativeCommandListener("pop", commandId, promise, eventEmitter, now)));
}

@ReactMethod
public void popTo(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
handle(() -> navigator().popTo(componentId, parse(mergeOptions), new NativeCommandListener("popTo", commandId, promise, eventEmitter, now)));
runOnUIThread(() -> navigator().popTo(componentId, parse(mergeOptions), new NativeCommandListener("popTo", commandId, promise, eventEmitter, now)));
}

@ReactMethod
public void popToRoot(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
handle(() -> navigator().popToRoot(componentId, parse(mergeOptions), new NativeCommandListener("popToRoot", commandId, promise, eventEmitter, now)));
runOnUIThread(() -> navigator().popToRoot(componentId, parse(mergeOptions), new NativeCommandListener("popToRoot", commandId, promise, eventEmitter, now)));
}

@ReactMethod
public void showModal(String commandId, ReadableMap rawLayoutTree, Promise promise) {
final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree));
handle(() -> {
runOnUIThread(() -> {
final ViewController<?> viewController = layoutFactory.create(layoutTree);
navigator().showModal(viewController, new NativeCommandListener("showModal", commandId, promise, eventEmitter, now));
});
}

@ReactMethod
public void dismissModal(String commandId, String componentId, @Nullable ReadableMap mergeOptions, Promise promise) {
handle(() -> {
runOnUIThread(() -> {
navigator().mergeOptions(componentId, parse(mergeOptions));
navigator().dismissModal(componentId, new NativeCommandListener("dismissModal", commandId, promise, eventEmitter, now));
});
}

@ReactMethod
public void dismissAllModals(String commandId, @Nullable ReadableMap mergeOptions, Promise promise) {
handle(() -> navigator().dismissAllModals(parse(mergeOptions), new NativeCommandListener("dismissAllModals", commandId, promise, eventEmitter, now)));
runOnUIThread(() -> navigator().dismissAllModals(parse(mergeOptions), new NativeCommandListener("dismissAllModals", commandId, promise, eventEmitter, now)));
}

@ReactMethod
public void showOverlay(String commandId, ReadableMap rawLayoutTree, Promise promise) {
final LayoutNode layoutTree = LayoutNodeParser.parse(jsonParser.parse(rawLayoutTree));
handle(() -> {
runOnUIThread(() -> {
final ViewController<?> viewController = layoutFactory.create(layoutTree);
navigator().showOverlay(viewController, new NativeCommandListener("showOverlay", commandId, promise, eventEmitter, now));
});
}

@ReactMethod
public void dismissOverlay(String commandId, String componentId, Promise promise) {
handle(() -> navigator().dismissOverlay(componentId, new NativeCommandListener("dismissOverlay", commandId, promise, eventEmitter, now)));
runOnUIThread(() -> navigator().dismissOverlay(componentId, new NativeCommandListener("dismissOverlay", commandId, promise, eventEmitter, now)));
}

@ReactMethod
public void dismissAllOverlays(String commandId, Promise promise) {
handle(() -> navigator().dismissAllOverlays(new NativeCommandListener("dismissAllOverlays", commandId, promise, eventEmitter, now)));
runOnUIThread(() -> navigator().dismissAllOverlays(new NativeCommandListener("dismissAllOverlays", commandId, promise, eventEmitter, now)));
}

@Override
public void onCatalystInstanceDestroy() {
final NavigationActivity navigationActivity = activity();
if (navigationActivity != null) {
navigationActivity.onCatalystInstanceDestroy();
}
super.onCatalystInstanceDestroy();
}

private Navigator navigator() {
Expand All @@ -221,7 +236,7 @@ private Options parse(@Nullable ReadableMap mergeOptions) {
null ? Options.EMPTY : Options.parse(ctx, new TypefaceLoader(activity()), jsonParser.parse(mergeOptions));
}

protected void handle(Runnable task) {
protected void runOnUIThread(Runnable task) {
d4vidi marked this conversation as resolved.
Show resolved Hide resolved
UiThread.post(() -> {
if (getCurrentActivity() != null && !activity().isFinishing()) {
task.run();
Expand All @@ -232,13 +247,4 @@ protected void handle(Runnable task) {
protected NavigationActivity activity() {
return (NavigationActivity) getCurrentActivity();
}

@Override
public void onCatalystInstanceDestroy() {
final NavigationActivity navigationActivity = activity();
if (navigationActivity != null) {
navigationActivity.onCatalystInstanceDestroy();
}
super.onCatalystInstanceDestroy();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.reactnativenavigation.react

import com.facebook.react.bridge.ReactApplicationContext
import com.reactnativenavigation.utils.sleep

private const val ACTIVITY_WAIT_INTERVAL = 100L
private const val ACTIVITY_WAIT_TRIES = 150

object NullRNActivityWorkaround {
@JvmStatic
fun waitForActivity(reactAppContext: ReactApplicationContext) {
var tries = 0
while (tries < ACTIVITY_WAIT_TRIES && !isActivityReady(reactAppContext)) {
sleep(ACTIVITY_WAIT_INTERVAL)
tries++
}
}

private fun isActivityReady(reactAppContext: ReactApplicationContext): Boolean {
return reactAppContext.hasCurrentActivity()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.reactnativenavigation.utils

fun sleep(ms: Long) {
try {
Thread.sleep(ms)
} catch (e: InterruptedException) {
e.printStackTrace()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ public void postCommandsOnMainThread_doesNotCrashIfActivityIsNull() {
NavigationModule spy = spy(uut);
when(spy.activity()).thenReturn(mock(NavigationActivity.class));
Runnable runnable = mock(Runnable.class);
spy.handle(runnable);
spy.runOnUIThread(runnable);
ShadowLooper.idleMainLooper();
verify(runnable).run();

when(spy.activity()).thenReturn(null);
Runnable dontRun = mock(Runnable.class);
spy.handle(dontRun);
spy.runOnUIThread(dontRun);
ShadowLooper.idleMainLooper();
verify(dontRun, times(0)).run();
}
Expand Down
Loading