Browse Source

JsDevReload

Daniel Zlotin 8 years ago
parent
commit
dda074cd5a

+ 1
- 4
lib/android/app/src/main/java/com/reactnativenavigation/NavigationActivity.java View File

@@ -16,28 +16,25 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
16 16
 		super.onCreate(savedInstanceState);
17 17
 		app().getReactGateway().onActivityCreated(this);
18 18
 		navigator = new Navigator(this);
19
-		navigator.onActivityCreated();
19
+		setContentView(navigator.getView());
20 20
 	}
21 21
 
22 22
 	@Override
23 23
 	protected void onResume() {
24 24
 		super.onResume();
25 25
 		app().getReactGateway().onActivityResumed(this);
26
-		navigator.onActivityResumed();
27 26
 	}
28 27
 
29 28
 	@Override
30 29
 	protected void onPause() {
31 30
 		super.onPause();
32 31
 		app().getReactGateway().onActivityPaused(this);
33
-		navigator.onActivityPaused();
34 32
 	}
35 33
 
36 34
 	@Override
37 35
 	protected void onDestroy() {
38 36
 		super.onDestroy();
39 37
 		app().getReactGateway().onActivityDestroyed(this);
40
-		navigator.onActivityDestroyed();
41 38
 	}
42 39
 
43 40
 	@Override

+ 2
- 2
lib/android/app/src/main/java/com/reactnativenavigation/react/JsDevReloadHandler.java View File

@@ -23,7 +23,7 @@ public class JsDevReloadHandler {
23 23
 		}
24 24
 
25 25
 		if (keyCode == KeyEvent.KEYCODE_R) {
26
-			if (moreThan500MillisSinceLastR()) {
26
+			if (lessThan500MillisSinceLastR()) {
27 27
 				reactInstanceManager.getDevSupportManager().handleReloadJS();
28 28
 				return true;
29 29
 			}
@@ -32,7 +32,7 @@ public class JsDevReloadHandler {
32 32
 		return false;
33 33
 	}
34 34
 
35
-	private boolean moreThan500MillisSinceLastR() {
35
+	private boolean lessThan500MillisSinceLastR() {
36 36
 		return firstRTimestamp != 0 && System.currentTimeMillis() - firstRTimestamp < 500;
37 37
 	}
38 38
 }

+ 14
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationReactNativeHost.java View File

@@ -4,11 +4,14 @@ import android.app.Application;
4 4
 
5 5
 import com.facebook.react.ReactNativeHost;
6 6
 import com.facebook.react.ReactPackage;
7
+import com.facebook.react.devsupport.RedBoxHandler;
7 8
 import com.facebook.react.shell.MainReactPackage;
8 9
 
9 10
 import java.util.Arrays;
10 11
 import java.util.List;
11 12
 
13
+import javax.annotation.Nullable;
14
+
12 15
 public class NavigationReactNativeHost extends ReactNativeHost {
13 16
 
14 17
 	private final boolean isDebug;
@@ -18,6 +21,17 @@ public class NavigationReactNativeHost extends ReactNativeHost {
18 21
 		this.isDebug = isDebug;
19 22
 	}
20 23
 
24
+	@Override
25
+	public String getJSMainModuleName() {
26
+		return super.getJSMainModuleName();
27
+	}
28
+
29
+	@Nullable
30
+	@Override
31
+	public RedBoxHandler getRedBoxHandler() {
32
+		return super.getRedBoxHandler();
33
+	}
34
+
21 35
 	@Override
22 36
 	public boolean getUseDeveloperSupport() {
23 37
 		return isDebug;

+ 27
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/ReactGateway.java View File

@@ -1,15 +1,42 @@
1 1
 package com.reactnativenavigation.react;
2 2
 
3
+import com.facebook.react.devsupport.DevSupportManager;
4
+import com.facebook.react.devsupport.DevSupportManagerImpl;
5
+import com.facebook.react.devsupport.ReactInstanceDevCommandsHandler;
6
+import com.facebook.react.devsupport.RedBoxHandler;
3 7
 import com.reactnativenavigation.NavigationActivity;
4 8
 import com.reactnativenavigation.NavigationApplication;
9
+import com.reactnativenavigation.utils.ReflectionUtils;
5 10
 
6 11
 public class ReactGateway {
12
+
13
+	public interface JsReloadListener {
14
+		void onJsReload();
15
+	}
16
+
7 17
 	private final NavigationReactNativeHost reactNativeHost;
8 18
 	private final NavigationReactInitializer initializer;
9 19
 	private final JsDevReloadHandler jsDevReloadHandler;
10 20
 
11 21
 	public ReactGateway(final NavigationApplication application, final boolean isDebug) {
12 22
 		reactNativeHost = new NavigationReactNativeHost(application, isDebug);
23
+
24
+		DevSupportManager devSupportManager = reactNativeHost.getReactInstanceManager().getDevSupportManager();
25
+		ReactInstanceDevCommandsHandler reactInstanceCommandsHandler = (ReactInstanceDevCommandsHandler) ReflectionUtils.getDeclaredField(devSupportManager, "mReactInstanceCommandsHandler");
26
+		String packagerPathForJsBundle = reactNativeHost.getJSMainModuleName();
27
+		boolean enableOnCreate = reactNativeHost.getUseDeveloperSupport();
28
+		RedBoxHandler redBoxHandler = reactNativeHost.getRedBoxHandler();
29
+		DevSupportManagerImpl proxy = new DevSupportManagerImpl(application, reactInstanceCommandsHandler, packagerPathForJsBundle, enableOnCreate, redBoxHandler) {
30
+			@Override
31
+			public void handleReloadJS() {
32
+				super.handleReloadJS();
33
+				// onJsReload
34
+//				ReactContext currentReactContext = reactNativeHost.getReactInstanceManager().getCurrentReactContext();
35
+//				((NavigationActivity) currentReactContext.getCurrentActivity()).getNavigator().onDestroy();
36
+			}
37
+		};
38
+		ReflectionUtils.setField(reactNativeHost.getReactInstanceManager(), "mDevSupportManager", proxy);
39
+
13 40
 		initializer = new NavigationReactInitializer(reactNativeHost.getReactInstanceManager(), isDebug);
14 41
 		jsDevReloadHandler = new JsDevReloadHandler(reactNativeHost.getReactInstanceManager());
15 42
 	}

+ 0
- 24
lib/android/app/src/main/java/com/reactnativenavigation/viewcontrollers/Navigator.java View File

@@ -13,7 +13,6 @@ import java.util.Collections;
13 13
 public class Navigator extends ParentController {
14 14
 
15 15
 	private ViewController root;
16
-	private boolean activityResumed = false;
17 16
 
18 17
 	public Navigator(final Activity activity) {
19 18
 		super(activity, "navigator" + CompatUtils.generateViewId());
@@ -30,29 +29,6 @@ public class Navigator extends ParentController {
30 29
 		return root == null ? Collections.<ViewController>emptyList() : Collections.singletonList(root);
31 30
 	}
32 31
 
33
-	/*
34
-	 * Activity lifecycle
35
-	 */
36
-
37
-	public boolean isActivityResumed() {
38
-		return activityResumed;
39
-	}
40
-
41
-	public void onActivityCreated() {
42
-		getActivity().setContentView(getView());
43
-	}
44
-
45
-	public void onActivityResumed() {
46
-		activityResumed = true;
47
-	}
48
-
49
-	public void onActivityPaused() {
50
-		activityResumed = false;
51
-	}
52
-
53
-	public void onActivityDestroyed() {
54
-	}
55
-
56 32
 	@Override
57 33
 	public boolean handleBack() {
58 34
 		return root != null && root.handleBack();

+ 0
- 20
lib/android/app/src/test/java/com/reactnativenavigation/viewcontrollers/NavigatorTest.java View File

@@ -10,7 +10,6 @@ import com.reactnativenavigation.mocks.TestStackAnimator;
10 10
 import com.reactnativenavigation.utils.CompatUtils;
11 11
 
12 12
 import org.junit.Test;
13
-import org.robolectric.Shadows;
14 13
 
15 14
 import java.util.Arrays;
16 15
 
@@ -41,25 +40,6 @@ public class NavigatorTest extends BaseTest {
41 40
 		child5 = new SimpleViewController(activity, "child5");
42 41
 	}
43 42
 
44
-
45
-	@Test
46
-	public void isActivityResumed() throws Exception {
47
-		assertThat(uut.isActivityResumed()).isFalse();
48
-		uut.onActivityCreated();
49
-		assertThat(uut.isActivityResumed()).isFalse();
50
-		uut.onActivityResumed();
51
-		assertThat(uut.isActivityResumed()).isTrue();
52
-		uut.onActivityPaused();
53
-		assertThat(uut.isActivityResumed()).isFalse();
54
-	}
55
-
56
-	@Test
57
-	public void setsItselfAsContentView() throws Exception {
58
-		assertThat(Shadows.shadowOf(activity).getContentView()).isNull();
59
-		uut.onActivityCreated();
60
-		assertThat(Shadows.shadowOf(activity).getContentView()).isNotNull().isEqualTo(uut.getView());
61
-	}
62
-
63 43
 	@Test
64 44
 	public void setRoot_AddsChildControllerView() throws Exception {
65 45
 		assertThat(uut.getView().getChildCount()).isZero();