Browse Source

Support setting custom react view dynamically

Guy Carmeli 7 years ago
parent
commit
16a3d5ccee

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/NavigationParams.java View File

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

+ 1
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java View File

81
     public Color topBarColor;
81
     public Color topBarColor;
82
     public String topBarReactView;
82
     public String topBarReactView;
83
     public String topBarReactViewAlignment;
83
     public String topBarReactViewAlignment;
84
+    public Bundle topBarReactViewInitialProps;
84
     public CollapsingTopBarParams collapsingTopBarParams;
85
     public CollapsingTopBarParams collapsingTopBarParams;
85
     public boolean topBarCollapseOnScroll;
86
     public boolean topBarCollapseOnScroll;
86
     public boolean topBarElevationShadowEnabled;
87
     public boolean topBarElevationShadowEnabled;

+ 5
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java View File

34
         result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
34
         result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
35
         result.topBarReactView = params.getString("topBarReactView");
35
         result.topBarReactView = params.getString("topBarReactView");
36
         result.topBarReactViewAlignment = params.getString("topBarReactViewAlignment");
36
         result.topBarReactViewAlignment = params.getString("topBarReactViewAlignment");
37
+        result.topBarReactViewInitialProps = getBundle("topBarReactViewInitialProps");
37
         result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
38
         result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
38
         result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
39
         result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
39
         result.topBarCollapseOnScroll = getBoolean("topBarCollapseOnScroll", false);
40
         result.topBarCollapseOnScroll = getBoolean("topBarCollapseOnScroll", false);
275
     private int getInt(String key, int defaultValue) {
276
     private int getInt(String key, int defaultValue) {
276
         return params.containsKey(key) ? params.getInt(key) : defaultValue;
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 View File

128
     private void createTitleBar() {
128
     private void createTitleBar() {
129
         addTitleBarButtons();
129
         addTitleBarButtons();
130
         if (screenParams.styleParams.hasTopBarCustomComponent()) {
130
         if (screenParams.styleParams.hasTopBarCustomComponent()) {
131
-            topBar.setReactView(screenParams.styleParams.topBarReactView, screenParams.styleParams.topBarReactViewAlignment);
131
+            topBar.setReactView(screenParams.styleParams);
132
         } else {
132
         } else {
133
             topBar.setTitle(screenParams.title);
133
             topBar.setTitle(screenParams.title);
134
             topBar.setSubtitle(screenParams.subtitle);
134
             topBar.setSubtitle(screenParams.subtitle);

+ 19
- 2
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java View File

1
 package com.reactnativenavigation.views;
1
 package com.reactnativenavigation.views;
2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
+import android.os.Bundle;
4
 import android.view.View;
5
 import android.view.View;
5
 
6
 
6
 import com.facebook.react.ReactRootView;
7
 import com.facebook.react.ReactRootView;
13
 public class ContentView extends ReactRootView {
14
 public class ContentView extends ReactRootView {
14
     private final String screenId;
15
     private final String screenId;
15
     private final NavigationParams navigationParams;
16
     private final NavigationParams navigationParams;
17
+    private Bundle initialProps;
16
 
18
 
17
     boolean isContentVisible = false;
19
     boolean isContentVisible = false;
18
     private SingleScreen.OnDisplayListener onDisplayListener;
20
     private SingleScreen.OnDisplayListener onDisplayListener;
23
     }
25
     }
24
 
26
 
25
     public ContentView(Context context, String screenId, NavigationParams navigationParams) {
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
         super(context);
32
         super(context);
27
         this.screenId = screenId;
33
         this.screenId = screenId;
28
         this.navigationParams = navigationParams;
34
         this.navigationParams = navigationParams;
35
+        this.initialProps = initialProps;
29
         attachToJS();
36
         attachToJS();
30
         viewMeasurer = new ViewMeasurer();
37
         viewMeasurer = new ViewMeasurer();
31
     }
38
     }
35
     }
42
     }
36
 
43
 
37
     private void attachToJS() {
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
     public String getNavigatorEventId() {
59
     public String getNavigatorEventId() {

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java View File

112
                 if (params.titleBarTitleTextCentered) {
112
                 if (params.titleBarTitleTextCentered) {
113
                     titleView.setX(ViewUtils.getWindowWidth(getActivity()) / 2 - titleView.getWidth() / 2);
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 View File

3
 import android.content.Context;
3
 import android.content.Context;
4
 import android.graphics.Color;
4
 import android.graphics.Color;
5
 import android.os.Build;
5
 import android.os.Build;
6
-import android.os.Bundle;
7
 import android.support.design.widget.AppBarLayout;
6
 import android.support.design.widget.AppBarLayout;
8
 import android.support.v7.app.ActionBar;
7
 import android.support.v7.app.ActionBar;
9
 import android.text.TextUtils;
8
 import android.text.TextUtils;
87
         titleBar.setSubtitle(subtitle);
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
                 addReactViewFill(view);
97
                 addReactViewFill(view);
95
             } else {
98
             } else {
96
                 addCenteredReactView(view);
99
                 addCenteredReactView(view);
127
             setTransparent();
130
             setTransparent();
128
         }
131
         }
129
         titleBar.setStyle(styleParams);
132
         titleBar.setStyle(styleParams);
133
+        setReactView(styleParams);
130
         setTopTabsStyle(styleParams);
134
         setTopTabsStyle(styleParams);
131
         if (!styleParams.topBarElevationShadowEnabled) {
135
         if (!styleParams.topBarElevationShadowEnabled) {
132
             disableElevationShadow();
136
             disableElevationShadow();

+ 1
- 0
src/deprecated/platformSpecificDeprecated.android.js View File

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