Преглед изворни кода

Support adding react component to TopBar

Usage:
    navBarCustomView: 'example.CustomTopBar', // registered component name
    navBarComponentAlignment: 'center' // 'center'/'fill'
Guy Carmeli пре 7 година
родитељ
комит
e581b2dec6

+ 7
- 0
android/app/src/main/java/com/reactnativenavigation/params/StyleParams.java Прегледај датотеку

3
 import android.graphics.Typeface;
3
 import android.graphics.Typeface;
4
 import android.os.Bundle;
4
 import android.os.Bundle;
5
 import android.support.annotation.ColorInt;
5
 import android.support.annotation.ColorInt;
6
+import android.text.TextUtils;
6
 
7
 
7
 import com.reactnativenavigation.utils.TypefaceLoader;
8
 import com.reactnativenavigation.utils.TypefaceLoader;
8
 
9
 
78
     public Color contextualMenuBackgroundColor;
79
     public Color contextualMenuBackgroundColor;
79
 
80
 
80
     public Color topBarColor;
81
     public Color topBarColor;
82
+    public String topBarReactView;
83
+    public String topBarReactViewAlignment;
81
     public CollapsingTopBarParams collapsingTopBarParams;
84
     public CollapsingTopBarParams collapsingTopBarParams;
82
     public boolean topBarCollapseOnScroll;
85
     public boolean topBarCollapseOnScroll;
83
     public boolean topBarElevationShadowEnabled;
86
     public boolean topBarElevationShadowEnabled;
122
     public Font bottomTabFontFamily;
125
     public Font bottomTabFontFamily;
123
 
126
 
124
     public Color navigationBarColor;
127
     public Color navigationBarColor;
128
+
129
+    public boolean hasTopBarCustomComponent() {
130
+        return !TextUtils.isEmpty(topBarReactView);
131
+    }
125
 }
132
 }

+ 2
- 0
android/app/src/main/java/com/reactnativenavigation/params/parsers/StyleParamsParser.java Прегледај датотеку

32
         result.contextualMenuBackgroundColor = getColor("contextualMenuBackgroundColor", getDefaultContextualMenuBackgroundColor());
32
         result.contextualMenuBackgroundColor = getColor("contextualMenuBackgroundColor", getDefaultContextualMenuBackgroundColor());
33
 
33
 
34
         result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
34
         result.topBarColor = getColor("topBarColor", getDefaultTopBarColor());
35
+        result.topBarReactView = params.getString("topBarReactView");
36
+        result.topBarReactViewAlignment = params.getString("topBarReactViewAlignment");
35
         result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
37
         result.titleBarHideOnScroll = getBoolean("titleBarHideOnScroll", getDefaultTitleBarHideOnScroll());
36
         result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
38
         result.topBarTransparent = getBoolean("topBarTransparent", getDefaultTopBarHidden());
37
         result.topBarCollapseOnScroll = getBoolean("topBarCollapseOnScroll", false);
39
         result.topBarCollapseOnScroll = getBoolean("topBarCollapseOnScroll", false);

+ 6
- 2
android/app/src/main/java/com/reactnativenavigation/screens/Screen.java Прегледај датотеку

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

+ 15
- 1
android/app/src/main/java/com/reactnativenavigation/utils/ViewUtils.java Прегледај датотеку

28
 public class ViewUtils {
28
 public class ViewUtils {
29
     private static final AtomicInteger viewId = new AtomicInteger(1);
29
     private static final AtomicInteger viewId = new AtomicInteger(1);
30
     private static int statusBarHeight = -1;
30
     private static int statusBarHeight = -1;
31
+    private static int toolBarHeight = -1;
31
 
32
 
32
     public static void runOnPreDraw(final View view, final Runnable task) {
33
     public static void runOnPreDraw(final View view, final Runnable task) {
33
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
34
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
81
     public static float getWindowHeight(Activity activity) {
82
     public static float getWindowHeight(Activity activity) {
82
         DisplayMetrics metrics = new DisplayMetrics();
83
         DisplayMetrics metrics = new DisplayMetrics();
83
         activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
84
         activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
84
-        return metrics.widthPixels;
85
+        return metrics.heightPixels;
85
     }
86
     }
86
 
87
 
87
     private static int compatGenerateViewId() {
88
     private static int compatGenerateViewId() {
181
         return statusBarHeight;
182
         return statusBarHeight;
182
     }
183
     }
183
 
184
 
185
+    public static int getToolBarHeight() {
186
+        if (toolBarHeight > 0) {
187
+            return toolBarHeight;
188
+        }
189
+        final Resources resources = NavigationApplication.instance.getResources();
190
+        final int resourceId = resources.getIdentifier("action_bar_size", "dimen", "android");
191
+        toolBarHeight = resourceId > 0 ?
192
+                resources.getDimensionPixelSize(resourceId) :
193
+                (int) convertDpToPixel(56);
194
+        return toolBarHeight;
195
+    }
196
+
197
+
184
     public static ForegroundColorSpan[] getForegroundColorSpans(TextView view) {
198
     public static ForegroundColorSpan[] getForegroundColorSpans(TextView view) {
185
         SpannedString text = new SpannedString(view.getText());
199
         SpannedString text = new SpannedString(view.getText());
186
         return text.getSpans(0, text.length(), ForegroundColorSpan.class);
200
         return text.getSpans(0, text.length(), ForegroundColorSpan.class);

+ 1
- 2
android/app/src/main/java/com/reactnativenavigation/views/ContentView.java Прегледај датотеку

51
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
51
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
52
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
52
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
53
         int measuredHeight = viewMeasurer.getMeasuredHeight(heightMeasureSpec);
53
         int measuredHeight = viewMeasurer.getMeasuredHeight(heightMeasureSpec);
54
-        setMeasuredDimension(viewMeasurer.getMeasuredWidth(widthMeasureSpec),
55
-                measuredHeight);
54
+        setMeasuredDimension(viewMeasurer.getMeasuredWidth(widthMeasureSpec), measuredHeight);
56
     }
55
     }
57
 
56
 
58
     @Override
57
     @Override

+ 4
- 0
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java Прегледај датотеку

51
         addButtonsToTitleBar(navigatorEventId, menu);
51
         addButtonsToTitleBar(navigatorEventId, menu);
52
     }
52
     }
53
 
53
 
54
+    public int getRightButtonsWidth() {
55
+        return actionMenuView.getWidth();
56
+    }
57
+
54
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
58
     public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
55
                               LeftButtonOnClickListener leftButtonOnClickListener,
59
                               LeftButtonOnClickListener leftButtonOnClickListener,
56
                               String navigatorEventId,
60
                               String navigatorEventId,

+ 34
- 0
android/app/src/main/java/com/reactnativenavigation/views/TopBar.java Прегледај датотеку

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;
6
 import android.support.design.widget.AppBarLayout;
7
 import android.support.design.widget.AppBarLayout;
8
+import android.support.v7.app.ActionBar;
9
+import android.text.TextUtils;
10
+import android.view.Gravity;
7
 import android.view.ViewGroup;
11
 import android.view.ViewGroup;
8
 import android.widget.FrameLayout;
12
 import android.widget.FrameLayout;
9
 
13
 
11
 import com.reactnativenavigation.animation.VisibilityAnimator;
15
 import com.reactnativenavigation.animation.VisibilityAnimator;
12
 import com.reactnativenavigation.params.BaseScreenParams;
16
 import com.reactnativenavigation.params.BaseScreenParams;
13
 import com.reactnativenavigation.params.ContextualMenuParams;
17
 import com.reactnativenavigation.params.ContextualMenuParams;
18
+import com.reactnativenavigation.params.NavigationParams;
14
 import com.reactnativenavigation.params.StyleParams;
19
 import com.reactnativenavigation.params.StyleParams;
15
 import com.reactnativenavigation.params.TitleBarButtonParams;
20
 import com.reactnativenavigation.params.TitleBarButtonParams;
16
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
21
 import com.reactnativenavigation.params.TitleBarLeftButtonParams;
22
+import com.reactnativenavigation.screens.Screen;
17
 import com.reactnativenavigation.utils.ViewUtils;
23
 import com.reactnativenavigation.utils.ViewUtils;
18
 
24
 
19
 import java.util.List;
25
 import java.util.List;
81
         titleBar.setSubtitle(subtitle);
87
         titleBar.setSubtitle(subtitle);
82
     }
88
     }
83
 
89
 
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)) {
94
+                addReactViewFill(view);
95
+            } else {
96
+                addCenteredReactView(view);
97
+            }
98
+        }
99
+    }
100
+
101
+    private void addReactViewFill(ContentView view) {
102
+        view.setLayoutParams(new LayoutParams(MATCH_PARENT, ViewUtils.getToolBarHeight()));
103
+        titleBar.addView(view);
104
+    }
105
+
106
+    private void addCenteredReactView(final ContentView view) {
107
+        titleBar.addView(view, new LayoutParams(WRAP_CONTENT, ViewUtils.getToolBarHeight()));
108
+        view.setOnDisplayListener(new Screen.OnDisplayListener() {
109
+            @Override
110
+            public void onDisplay() {
111
+                view.getLayoutParams().width = (int) (float) view.getChildAt(0).getMeasuredWidth();
112
+                ((ActionBar.LayoutParams) view.getLayoutParams()).gravity = Gravity.CENTER;
113
+                view.requestLayout();
114
+            }
115
+        });
116
+    }
117
+
84
     public void setButtonColor(StyleParams styleParams) {
118
     public void setButtonColor(StyleParams styleParams) {
85
         titleBar.setButtonColor(styleParams.titleBarButtonColor);
119
         titleBar.setButtonColor(styleParams.titleBarButtonColor);
86
     }
120
     }

+ 2
- 0
src/deprecated/platformSpecificDeprecated.android.js Прегледај датотеку

136
   let ret = {
136
   let ret = {
137
     orientation: originalStyleObject.orientation,
137
     orientation: originalStyleObject.orientation,
138
     statusBarColor: processColor(originalStyleObject.statusBarColor),
138
     statusBarColor: processColor(originalStyleObject.statusBarColor),
139
+    topBarReactView: originalStyleObject.navBarCustomView,
140
+    topBarReactViewAlignment: originalStyleObject.navBarComponentAlignment,
139
     topBarColor: processColor(originalStyleObject.navBarBackgroundColor),
141
     topBarColor: processColor(originalStyleObject.navBarBackgroundColor),
140
     topBarTransparent: originalStyleObject.navBarTransparent,
142
     topBarTransparent: originalStyleObject.navBarTransparent,
141
     topBarTranslucent: originalStyleObject.navBarTranslucent,
143
     topBarTranslucent: originalStyleObject.navBarTranslucent,