Daniel Zlotin 8 years ago
parent
commit
6c414310b2

+ 2
- 2
android/app/src/main/java/com/reactnativenavigation/NavigationApplication.java View File

@@ -27,8 +27,8 @@ public abstract class NavigationApplication extends Application {
27 27
         navigationReactInstance.startReactContextOnceInBackgroundAndExecuteJS();
28 28
     }
29 29
 
30
-    public Handler getMainHandler() {
31
-        return handler;
30
+    public void runOnMainThread(Runnable runnable) {
31
+        handler.post(runnable);
32 32
     }
33 33
 
34 34
     public NavigationReactInstance getNavigationReactInstance() {

+ 3
- 13
android/app/src/main/java/com/reactnativenavigation/bridge/NavigationReactModule.java View File

@@ -1,14 +1,10 @@
1 1
 package com.reactnativenavigation.bridge;
2 2
 
3
-import android.content.Intent;
4
-import android.os.Bundle;
5
-
6 3
 import com.facebook.react.bridge.ReactApplicationContext;
7 4
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
8 5
 import com.facebook.react.bridge.ReactMethod;
9 6
 import com.facebook.react.bridge.ReadableMap;
10
-import com.reactnativenavigation.NavigationApplication;
11
-import com.reactnativenavigation.controllers.NavigationActivity;
7
+import com.reactnativenavigation.controllers.NavigationCommandsHandler;
12 8
 
13 9
 /**
14 10
  * The basic abstract components we will expose:
@@ -34,14 +30,7 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
34 30
 
35 31
     @ReactMethod
36 32
     public void startApp(final ReadableMap params) {
37
-        NavigationApplication context = NavigationApplication.instance;
38
-        Intent intent = new Intent(context, NavigationActivity.class);
39
-        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
40
-
41
-        Bundle paramsBundle = BundleConverter.toBundle(params);
42
-
43
-        intent.putExtra(NavigationActivity.PARAMS_BUNDLE, paramsBundle);
44
-        context.startActivity(intent);
33
+        NavigationCommandsHandler.startApp(BundleConverter.toBundle(params));
45 34
     }
46 35
 
47 36
     @ReactMethod
@@ -82,6 +71,7 @@ public class NavigationReactModule extends ReactContextBaseJavaModule {
82 71
 
83 72
     @ReactMethod
84 73
     public void push(final ReadableMap params) {
74
+        NavigationCommandsHandler.push(BundleConverter.toBundle(params));
85 75
     }
86 76
 
87 77
     @ReactMethod

+ 14
- 12
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationActivity.java View File

@@ -1,6 +1,5 @@
1 1
 package com.reactnativenavigation.controllers;
2 2
 
3
-import android.app.Activity;
4 3
 import android.content.Intent;
5 4
 import android.os.Bundle;
6 5
 import android.support.v7.app.AppCompatActivity;
@@ -12,35 +11,34 @@ import com.reactnativenavigation.NavigationApplication;
12 11
 import com.reactnativenavigation.layouts.Layout;
13 12
 import com.reactnativenavigation.layouts.LayoutFactory;
14 13
 import com.reactnativenavigation.params.ActivityParams;
15
-import com.reactnativenavigation.params.parsers.ActivityParamsParser;
14
+import com.reactnativenavigation.params.ScreenParams;
16 15
 import com.reactnativenavigation.react.JsDevReloadHandler;
17 16
 import com.reactnativenavigation.react.NavigationReactInstance;
18 17
 import com.reactnativenavigation.react.RedboxPermission;
19 18
 
20 19
 public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, NavigationReactInstance.OnJsDevReloadListener {
21 20
 
22
-    public static final String PARAMS_BUNDLE = "PARAMS_BUNDLE";
23 21
     /**
24 22
      * Although we start multiple activities, we make sure to pass Intent.CLEAR_TASK | Intent.NEW_TASK
25 23
      * So that we actually have only 1 instance of the activity running at one time.
26
-     * We hold the currentActivity (resume->pause) so we know when we need to destroy the javascript context.
27
-     * This is somewhat weird, and in the future either fully support multiple activities,
28
-     * OR a single activity with changing contentView similar to ReactNative impl.
24
+     * We hold the currentActivity (resume->pause) so we know when we need to destroy the javascript context
25
+     * (when currentActivity is null, ie pause and destroy was called without resume).
26
+     * This is somewhat weird, and in the future we better use a single activity with changing contentView similar to ReactNative impl.
27
+     * Along with that, we should handle commands from the bridge using onNewIntent
29 28
      */
30
-    private static Activity currentActivity;
29
+    static NavigationActivity currentActivity;
30
+
31
+    private final ModalController modalController = new ModalController();
31 32
     private ActivityParams activityParams;
32
-    private ModalController modalController;
33 33
     private Layout layout;
34 34
 
35 35
     @Override
36 36
     protected void onCreate(Bundle savedInstanceState) {
37 37
         super.onCreate(savedInstanceState);
38
+        RedboxPermission.permissionToShowRedboxIfNeeded(this);
38 39
 
39
-        activityParams = ActivityParamsParser.parse(getIntent().getBundleExtra(PARAMS_BUNDLE));
40
-
41
-        modalController = new ModalController();
40
+        activityParams = NavigationCommandsHandler.getActivityParams(getIntent());
42 41
 
43
-        RedboxPermission.permissionToShowRedboxIfNeeded(this);
44 42
         createLayout();
45 43
     }
46 44
 
@@ -107,4 +105,8 @@ public class NavigationActivity extends AppCompatActivity implements DefaultHard
107 105
     private NavigationReactInstance getNavigationReactInstance() {
108 106
         return NavigationApplication.instance.getNavigationReactInstance();
109 107
     }
108
+
109
+    void push(ScreenParams params) {
110
+        layout.push(params);
111
+    }
110 112
 }

+ 46
- 0
android/app/src/main/java/com/reactnativenavigation/controllers/NavigationCommandsHandler.java View File

@@ -0,0 +1,46 @@
1
+package com.reactnativenavigation.controllers;
2
+
3
+import android.content.Intent;
4
+import android.os.Bundle;
5
+
6
+import com.reactnativenavigation.NavigationApplication;
7
+import com.reactnativenavigation.params.ActivityParams;
8
+import com.reactnativenavigation.params.ScreenParams;
9
+import com.reactnativenavigation.params.parsers.ActivityParamsParser;
10
+import com.reactnativenavigation.params.parsers.ScreenParamsParser;
11
+
12
+public class NavigationCommandsHandler {
13
+
14
+    private static final String ACTIVITY_PARAMS_BUNDLE = "ACTIVITY_PARAMS_BUNDLE";
15
+
16
+    /**
17
+     * start a new activity with CLEAR_TASK | NEW_TASK
18
+     *
19
+     * @param params ActivityParams as bundle
20
+     */
21
+    public static void startApp(Bundle params) {
22
+        Intent intent = new Intent(NavigationApplication.instance, NavigationActivity.class);
23
+        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
24
+        intent.putExtra(ACTIVITY_PARAMS_BUNDLE, params);
25
+        NavigationApplication.instance.startActivity(intent);
26
+    }
27
+
28
+    public static void push(Bundle screenParams) {
29
+        final NavigationActivity currentActivity = NavigationActivity.currentActivity;
30
+        if (currentActivity == null) {
31
+            return;
32
+        }
33
+
34
+        final ScreenParams params = ScreenParamsParser.parse(screenParams);
35
+        NavigationApplication.instance.runOnMainThread(new Runnable() {
36
+            @Override
37
+            public void run() {
38
+                currentActivity.push(params);
39
+            }
40
+        });
41
+    }
42
+
43
+    static ActivityParams getActivityParams(Intent intent) {
44
+        return ActivityParamsParser.parse(intent.getBundleExtra(NavigationCommandsHandler.ACTIVITY_PARAMS_BUNDLE));
45
+    }
46
+}

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/Layout.java View File

@@ -1,9 +1,13 @@
1 1
 package com.reactnativenavigation.layouts;
2 2
 
3
+import com.reactnativenavigation.params.ScreenParams;
4
+
3 5
 public interface Layout {
4 6
     boolean onBackPressed();
5 7
 
6 8
     void onDestroy();
7 9
 
8 10
     void removeAllReactViews();
11
+
12
+    void push(ScreenParams params);
9 13
 }

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/layouts/SingleScreenLayout.java View File

@@ -45,4 +45,9 @@ public class SingleScreenLayout extends FrameLayout implements Layout {
45 45
     public void removeAllReactViews() {
46 46
         stack.destroy();
47 47
     }
48
+
49
+    @Override
50
+    public void push(ScreenParams params) {
51
+        stack.push(params);
52
+    }
48 53
 }

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

@@ -14,8 +14,8 @@ public class RedboxPermission {
14 14
 
15 15
     public static void permissionToShowRedboxIfNeeded(Context context) {
16 16
         if (NavigationApplication.instance.isDebug() &&
17
-            Build.VERSION.SDK_INT >= 23 &&
18
-            !Settings.canDrawOverlays(context)) {
17
+                Build.VERSION.SDK_INT >= 23 &&
18
+                !Settings.canDrawOverlays(context)) {
19 19
             Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
20 20
             context.startActivity(serviceIntent);
21 21
             String msg = "Overlay permissions needs to be granted in order for react native apps to run in dev mode";

+ 2
- 4
android/app/src/test/java/com/reactnativenavigation/FirstTest.java View File

@@ -7,13 +7,11 @@ import org.robolectric.RobolectricTestRunner;
7 7
 import static org.hamcrest.Matchers.is;
8 8
 import static org.junit.Assert.assertThat;
9 9
 
10
+//TODO test the shit out of this project
10 11
 @RunWith(RobolectricTestRunner.class)
11 12
 public class FirstTest {
12
-
13 13
     @Test
14 14
     public void woohoo() {
15
-        assertThat(1+2, is(3));
15
+        assertThat(1 + 2, is(3));
16 16
     }
17
-
18
-
19 17
 }

+ 4
- 0
src/platformSpecific.ios.js View File

@@ -1,3 +1,7 @@
1
+/**
2
+ * copy paste from deprecated - for now
3
+ */
4
+
1 5
 import utils from './utils';
2 6
 import Navigation from './../Navigation';
3 7
 import Controllers, {Modal, Notification} from 'react-native-controllers';