瀏覽代碼

Support setting custom react view dynamically

Guy Carmeli 7 年之前
父節點
當前提交
16a3d5ccee

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/NavigationParams.java 查看文件

@@ -3,6 +3,8 @@ package com.reactnativenavigation.params;
3 3
 import android.os.Bundle;
4 4
 
5 5
 public class NavigationParams {
6
+    public static final NavigationParams EMPTY = new NavigationParams(Bundle.EMPTY);
7
+
6 8
     private static final String SCREEN_INSTANCE_ID = "screenInstanceID";
7 9
     private static final String NAVIGATOR_ID = "navigatorID";
8 10
     private static final String NAVIGATOR_EVENT_ID = "navigatorEventID";

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java 查看文件

@@ -81,6 +81,7 @@ public class StyleParams {
81 81
     public Color topBarColor;
82 82
     public String topBarReactView;
83 83
     public String topBarReactViewAlignment;
84
+    public Bundle topBarReactViewInitialProps;
84 85
     public CollapsingTopBarParams collapsingTopBarParams;
85 86
     public boolean topBarCollapseOnScroll;
86 87
     public boolean topBarElevationShadowEnabled;

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java 查看文件

@@ -34,6 +34,7 @@ public class StyleParamsParser {
34 34
         result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
35 35
         result.topBarReactView = params.getString("topBarReactView");
36 36
         result.topBarReactViewAlignment = params.getString("topBarReactViewAlignment");
37
+        result.topBarReactViewInitialProps = getBundle("topBarReactViewInitialProps");
37 38
         result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
38 39
         result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
39 40
         result.topBarCollapseOnScroll = getBoolean("topBarCollapseOnScroll", false);
@@ -275,4 +276,8 @@ public class StyleParamsParser {
275 276
     private int getInt(String key, int defaultValue) {
276 277
         return params.containsKey(key) ? params.getInt(key) : defaultValue;
277 278
     }
279
+
280
+    private Bundle getBundle(String key) {
281
+        return params.containsKey(key) ? params.getBundle(key) : Bundle.EMPTY;
282
+    }
278 283
 }

+ 1
- 1
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java 查看文件

@@ -128,7 +128,7 @@ public abstract class Screen extends RelativeLayout implements Subscriber {
128 128
     private void createTitleBar() {
129 129
         addTitleBarButtons();
130 130
         if (screenParams.styleParams.hasTopBarCustomComponent()) {
131
-            topBar.setReactView(screenParams.styleParams.topBarReactView, screenParams.styleParams.topBarReactViewAlignment);
131
+            topBar.setReactView(screenParams.styleParams);
132 132
         } else {
133 133
             topBar.setTitle(screenParams.title);
134 134
             topBar.setSubtitle(screenParams.subtitle);

+ 19
- 2
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java 查看文件

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.views;
2 2
 
3 3
 import android.content.Context;
4
+import android.os.Bundle;
4 5
 import android.view.View;
5 6
 
6 7
 import com.facebook.react.ReactRootView;
@@ -13,6 +14,7 @@ import com.reactnativenavigation.views.utils.ViewMeasurer;
13 14
 public class ContentView extends ReactRootView {
14 15
     private final String screenId;
15 16
     private final NavigationParams navigationParams;
17
+    private Bundle initialProps;
16 18
 
17 19
     boolean isContentVisible = false;
18 20
     private SingleScreen.OnDisplayListener onDisplayListener;
@@ -23,9 +25,14 @@ public class ContentView extends ReactRootView {
23 25
     }
24 26
 
25 27
     public ContentView(Context context, String screenId, NavigationParams navigationParams) {
28
+        this(context, screenId, navigationParams, Bundle.EMPTY);
29
+    }
30
+
31
+    public ContentView(Context context, String screenId, NavigationParams navigationParams, Bundle initialProps) {
26 32
         super(context);
27 33
         this.screenId = screenId;
28 34
         this.navigationParams = navigationParams;
35
+        this.initialProps = initialProps;
29 36
         attachToJS();
30 37
         viewMeasurer = new ViewMeasurer();
31 38
     }
@@ -35,8 +42,18 @@ public class ContentView extends ReactRootView {
35 42
     }
36 43
 
37 44
     private void attachToJS() {
38
-        startReactApplication(NavigationApplication.instance.getReactGateway().getReactInstanceManager(), screenId,
39
-                navigationParams.toBundle());
45
+        navigationParams.toBundle().putAll(initialProps);
46
+        startReactApplication(NavigationApplication.instance.getReactGateway().getReactInstanceManager(),
47
+                screenId,
48
+                createInitialParams()
49
+        );
50
+    }
51
+
52
+    private Bundle createInitialParams() {
53
+        final Bundle params = new Bundle();
54
+        params.putAll(navigationParams.toBundle());
55
+        params.putAll(initialProps);
56
+        return params;
40 57
     }
41 58
 
42 59
     public String getNavigatorEventId() {

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java 查看文件

@@ -112,7 +112,6 @@ public class TitleBar extends Toolbar {
112 112
                 if (params.titleBarTitleTextCentered) {
113 113
                     titleView.setX(ViewUtils.getWindowWidth(getActivity()) / 2 - titleView.getWidth() / 2);
114 114
                 }
115
-                
116 115
             }
117 116
         });
118 117
     }

+ 9
- 5
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java 查看文件

@@ -3,7 +3,6 @@ package com.reactnativenavigation.views;
3 3
 import android.content.Context;
4 4
 import android.graphics.Color;
5 5
 import android.os.Build;
6
-import android.os.Bundle;
7 6
 import android.support.design.widget.AppBarLayout;
8 7
 import android.support.v7.app.ActionBar;
9 8
 import android.text.TextUtils;
@@ -87,10 +86,14 @@ public class TopBar extends AppBarLayout {
87 86
         titleBar.setSubtitle(subtitle);
88 87
     }
89 88
 
90
-    public void setReactView(String topBarReactView, String alignment) {
91
-        if (!TextUtils.isEmpty(topBarReactView)) {
92
-            final ContentView view = new ContentView(getContext(), topBarReactView, new NavigationParams(Bundle.EMPTY));
93
-            if ("fill".equals(alignment)) {
89
+    public void setReactView(StyleParams styleParams) {
90
+        if (!TextUtils.isEmpty(styleParams.topBarReactView)) {
91
+            final ContentView view = new ContentView(getContext(),
92
+                    styleParams.topBarReactView,
93
+                    NavigationParams.EMPTY,
94
+                    styleParams.topBarReactViewInitialProps
95
+            );
96
+            if ("fill".equals(styleParams.topBarReactViewAlignment)) {
94 97
                 addReactViewFill(view);
95 98
             } else {
96 99
                 addCenteredReactView(view);
@@ -127,6 +130,7 @@ public class TopBar extends AppBarLayout {
127 130
             setTransparent();
128 131
         }
129 132
         titleBar.setStyle(styleParams);
133
+        setReactView(styleParams);
130 134
         setTopTabsStyle(styleParams);
131 135
         if (!styleParams.topBarElevationShadowEnabled) {
132 136
             disableElevationShadow();

+ 1
- 0
src/deprecated/platformSpecificDeprecated.android.js 查看文件

@@ -138,6 +138,7 @@ function convertStyleParams(originalStyleObject) {
138 138
     statusBarColor: processColor(originalStyleObject.statusBarColor),
139 139
     topBarReactView: originalStyleObject.navBarCustomView,
140 140
     topBarReactViewAlignment: originalStyleObject.navBarComponentAlignment,
141
+    topBarReactViewInitialProps: originalStyleObject.navBarCustomViewInitialProps,
141 142
     topBarColor: processColor(originalStyleObject.navBarBackgroundColor),
142 143
     topBarTransparent: originalStyleObject.navBarTransparent,
143 144
     topBarTranslucent: originalStyleObject.navBarTranslucent,