Daniel Zlotin 8 years ago
parent
commit
6c414310b2

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

27
         navigationReactInstance.startReactContextOnceInBackgroundAndExecuteJS();
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
     public NavigationReactInstance getNavigationReactInstance() {
34
     public NavigationReactInstance getNavigationReactInstance() {

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

1
 package com.reactnativenavigation.bridge;
1
 package com.reactnativenavigation.bridge;
2
 
2
 
3
-import android.content.Intent;
4
-import android.os.Bundle;
5
-
6
 import com.facebook.react.bridge.ReactApplicationContext;
3
 import com.facebook.react.bridge.ReactApplicationContext;
7
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
4
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
8
 import com.facebook.react.bridge.ReactMethod;
5
 import com.facebook.react.bridge.ReactMethod;
9
 import com.facebook.react.bridge.ReadableMap;
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
  * The basic abstract components we will expose:
10
  * The basic abstract components we will expose:
34
 
30
 
35
     @ReactMethod
31
     @ReactMethod
36
     public void startApp(final ReadableMap params) {
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
     @ReactMethod
36
     @ReactMethod
82
 
71
 
83
     @ReactMethod
72
     @ReactMethod
84
     public void push(final ReadableMap params) {
73
     public void push(final ReadableMap params) {
74
+        NavigationCommandsHandler.push(BundleConverter.toBundle(params));
85
     }
75
     }
86
 
76
 
87
     @ReactMethod
77
     @ReactMethod

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

1
 package com.reactnativenavigation.controllers;
1
 package com.reactnativenavigation.controllers;
2
 
2
 
3
-import android.app.Activity;
4
 import android.content.Intent;
3
 import android.content.Intent;
5
 import android.os.Bundle;
4
 import android.os.Bundle;
6
 import android.support.v7.app.AppCompatActivity;
5
 import android.support.v7.app.AppCompatActivity;
12
 import com.reactnativenavigation.layouts.Layout;
11
 import com.reactnativenavigation.layouts.Layout;
13
 import com.reactnativenavigation.layouts.LayoutFactory;
12
 import com.reactnativenavigation.layouts.LayoutFactory;
14
 import com.reactnativenavigation.params.ActivityParams;
13
 import com.reactnativenavigation.params.ActivityParams;
15
-import com.reactnativenavigation.params.parsers.ActivityParamsParser;
14
+import com.reactnativenavigation.params.ScreenParams;
16
 import com.reactnativenavigation.react.JsDevReloadHandler;
15
 import com.reactnativenavigation.react.JsDevReloadHandler;
17
 import com.reactnativenavigation.react.NavigationReactInstance;
16
 import com.reactnativenavigation.react.NavigationReactInstance;
18
 import com.reactnativenavigation.react.RedboxPermission;
17
 import com.reactnativenavigation.react.RedboxPermission;
19
 
18
 
20
 public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, NavigationReactInstance.OnJsDevReloadListener {
19
 public class NavigationActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, NavigationReactInstance.OnJsDevReloadListener {
21
 
20
 
22
-    public static final String PARAMS_BUNDLE = "PARAMS_BUNDLE";
23
     /**
21
     /**
24
      * Although we start multiple activities, we make sure to pass Intent.CLEAR_TASK | Intent.NEW_TASK
22
      * Although we start multiple activities, we make sure to pass Intent.CLEAR_TASK | Intent.NEW_TASK
25
      * So that we actually have only 1 instance of the activity running at one time.
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
     private ActivityParams activityParams;
32
     private ActivityParams activityParams;
32
-    private ModalController modalController;
33
     private Layout layout;
33
     private Layout layout;
34
 
34
 
35
     @Override
35
     @Override
36
     protected void onCreate(Bundle savedInstanceState) {
36
     protected void onCreate(Bundle savedInstanceState) {
37
         super.onCreate(savedInstanceState);
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
         createLayout();
42
         createLayout();
45
     }
43
     }
46
 
44
 
107
     private NavigationReactInstance getNavigationReactInstance() {
105
     private NavigationReactInstance getNavigationReactInstance() {
108
         return NavigationApplication.instance.getNavigationReactInstance();
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

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
 package com.reactnativenavigation.layouts;
1
 package com.reactnativenavigation.layouts;
2
 
2
 
3
+import com.reactnativenavigation.params.ScreenParams;
4
+
3
 public interface Layout {
5
 public interface Layout {
4
     boolean onBackPressed();
6
     boolean onBackPressed();
5
 
7
 
6
     void onDestroy();
8
     void onDestroy();
7
 
9
 
8
     void removeAllReactViews();
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
     public void removeAllReactViews() {
45
     public void removeAllReactViews() {
46
         stack.destroy();
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
 
14
 
15
     public static void permissionToShowRedboxIfNeeded(Context context) {
15
     public static void permissionToShowRedboxIfNeeded(Context context) {
16
         if (NavigationApplication.instance.isDebug() &&
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
             Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
19
             Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
20
             context.startActivity(serviceIntent);
20
             context.startActivity(serviceIntent);
21
             String msg = "Overlay permissions needs to be granted in order for react native apps to run in dev mode";
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
 import static org.hamcrest.Matchers.is;
7
 import static org.hamcrest.Matchers.is;
8
 import static org.junit.Assert.assertThat;
8
 import static org.junit.Assert.assertThat;
9
 
9
 
10
+//TODO test the shit out of this project
10
 @RunWith(RobolectricTestRunner.class)
11
 @RunWith(RobolectricTestRunner.class)
11
 public class FirstTest {
12
 public class FirstTest {
12
-
13
     @Test
13
     @Test
14
     public void woohoo() {
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
+/**
2
+ * copy paste from deprecated - for now
3
+ */
4
+
1
 import utils from './utils';
5
 import utils from './utils';
2
 import Navigation from './../Navigation';
6
 import Navigation from './../Navigation';
3
 import Controllers, {Modal, Notification} from 'react-native-controllers';
7
 import Controllers, {Modal, Notification} from 'react-native-controllers';