Ver código fonte

V2 constants (#3486)

* Constants support

* Change Navigation.constants() to an async function on Android

Also added topBarHeight and bottomTabsHeight
Yogev Ben David 6 anos atrás
pai
commit
65aa48012c
Nenhuma conta vinculada ao e-mail do autor do commit

+ 3
- 0
lib/android/app/src/main/java/com/reactnativenavigation/react/Constants.java Ver arquivo

@@ -4,4 +4,7 @@ public class Constants {
4 4
     public static final String BACK_BUTTON_JS_KEY = "backButtonId";
5 5
     public static final String BACK_BUTTON_ID = "RNN.back";
6 6
     public static final String STATUS_BAR_HEIGHT_KEY = "statusBarHeight";
7
+    public static final String TOP_BAR_HEIGHT_KEY = "topBarHeight";
8
+    public static final String BOTTOM_TABS_HEIGHT_KEY = "bottomTabsHeight";
9
+    public static final int BOTTOM_TABS_HEIGHT = 56;
7 10
 }

+ 10
- 7
lib/android/app/src/main/java/com/reactnativenavigation/react/NavigationModule.java Ver arquivo

@@ -3,11 +3,13 @@ package com.reactnativenavigation.react;
3 3
 import android.support.annotation.NonNull;
4 4
 
5 5
 import com.facebook.react.ReactInstanceManager;
6
+import com.facebook.react.bridge.Arguments;
6 7
 import com.facebook.react.bridge.Promise;
7 8
 import com.facebook.react.bridge.ReactApplicationContext;
8 9
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
9 10
 import com.facebook.react.bridge.ReactMethod;
10 11
 import com.facebook.react.bridge.ReadableMap;
12
+import com.facebook.react.bridge.WritableMap;
11 13
 import com.reactnativenavigation.NavigationActivity;
12 14
 import com.reactnativenavigation.NavigationApplication;
13 15
 import com.reactnativenavigation.parse.LayoutFactory;
@@ -24,7 +26,6 @@ import com.reactnativenavigation.viewcontrollers.Navigator;
24 26
 import com.reactnativenavigation.viewcontrollers.ViewController;
25 27
 import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalComponentCreator;
26 28
 
27
-import java.util.HashMap;
28 29
 import java.util.Map;
29 30
 
30 31
 public class NavigationModule extends ReactContextBaseJavaModule {
@@ -46,12 +47,14 @@ public class NavigationModule extends ReactContextBaseJavaModule {
46 47
 		return NAME;
47 48
 	}
48 49
 
49
-	@Override
50
-    public Map<String, Object> getConstants() {
51
-        final Map<String, Object> constants = new HashMap<>();
52
-        constants.put(Constants.BACK_BUTTON_JS_KEY, Constants.BACK_BUTTON_ID);
53
-        constants.put(Constants.STATUS_BAR_HEIGHT_KEY, UiUtils.getStatusBarHeight(getReactApplicationContext()));
54
-        return constants;
50
+	@ReactMethod
51
+    public void getConstants(Promise promise) {
52
+        WritableMap constants = Arguments.createMap();
53
+        constants.putString(Constants.BACK_BUTTON_JS_KEY, Constants.BACK_BUTTON_ID);
54
+        constants.putDouble(Constants.STATUS_BAR_HEIGHT_KEY, UiUtils.getStatusBarHeight(getReactApplicationContext()));
55
+        constants.putDouble(Constants.TOP_BAR_HEIGHT_KEY, UiUtils.getToolBarHeight(getReactApplicationContext()));
56
+        constants.putDouble(Constants.TOP_BAR_HEIGHT_KEY, UiUtils.dpToPx(getReactApplicationContext(), Constants.BOTTOM_TABS_HEIGHT));
57
+        promise.resolve(constants);
55 58
     }
56 59
 
57 60
 	@ReactMethod

+ 18
- 3
lib/android/app/src/main/java/com/reactnativenavigation/utils/UiUtils.java Ver arquivo

@@ -12,11 +12,14 @@ import android.view.ViewTreeObserver;
12 12
 import android.view.WindowManager;
13 13
 
14 14
 public class UiUtils {
15
-    public static final int STATUS_BAR_HEIGHT_M = 24;
16
-    public static final int STATUS_BAR_HEIGHT_L = 25;
15
+    private static final int STATUS_BAR_HEIGHT_M = 24;
16
+    private static final int STATUS_BAR_HEIGHT_L = 25;
17
+    private static final int DEFAULT_TOOLBAR_HEIGHT = 56;
18
+
17 19
     private static int statusBarHeight = -1;
20
+    private static int toolBarHeight = -1;
18 21
 
19
-	public static void runOnPreDrawOnce(final View view, final Runnable task) {
22
+    public static void runOnPreDrawOnce(final View view, final Runnable task) {
20 23
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
21 24
             @Override
22 25
             public boolean onPreDraw() {
@@ -61,6 +64,18 @@ public class UiUtils {
61 64
         return statusBarHeight;
62 65
     }
63 66
 
67
+    public static int getToolBarHeight(Context context) {
68
+        if (toolBarHeight > 0) {
69
+            return toolBarHeight;
70
+        }
71
+        final Resources resources = context.getResources();
72
+        final int resourceId = resources.getIdentifier("action_bar_size", "dimen", "android");
73
+        toolBarHeight = resourceId > 0 ?
74
+                resources.getDimensionPixelSize(resourceId) :
75
+                (int) dpToPx(context, DEFAULT_TOOLBAR_HEIGHT);
76
+        return toolBarHeight;
77
+    }
78
+
64 79
     public static float dpToPx(Context context, float dp) {
65 80
         float scale = context.getResources().getDisplayMetrics().density;
66 81
         return dp * scale + 0.5f;

+ 8
- 0
lib/ios/Constants.h Ver arquivo

@@ -0,0 +1,8 @@
1
+#import <Foundation/Foundation.h>
2
+#import <UIKit/UIKit.h>
3
+
4
+@interface Constants : NSObject
5
+
6
++ (NSDictionary *)getConstants;
7
+
8
+@end

+ 21
- 0
lib/ios/Constants.m Ver arquivo

@@ -0,0 +1,21 @@
1
+#import "Constants.h"
2
+
3
+@implementation Constants
4
+
5
++ (NSDictionary *)getConstants {
6
+	return @{@"topBarHeight": @([self topBarHeight]), @"statusBarHeight": @([self statusBarHeight]), @"bottomTabsHeight": @([self bottomTabsHeight])};
7
+}
8
+
9
++ (CGFloat)topBarHeight {
10
+	return UIApplication.sharedApplication.delegate.window.rootViewController.navigationController.navigationBar.frame.size.height;
11
+}
12
+
13
++ (CGFloat)statusBarHeight {
14
+	return [UIApplication sharedApplication].statusBarFrame.size.height;
15
+}
16
+
17
++ (CGFloat)bottomTabsHeight {
18
+	return UIApplication.sharedApplication.delegate.window.rootViewController.tabBarController.tabBar.frame.size.height;
19
+}
20
+
21
+@end

+ 5
- 0
lib/ios/RNNBridgeModule.m Ver arquivo

@@ -1,4 +1,5 @@
1 1
 #import "RNNBridgeModule.h"
2
+#import "Constants.h"
2 3
 
3 4
 @implementation RNNBridgeModule {
4 5
 	RNNCommandsHandler* _commandsHandler;
@@ -101,5 +102,9 @@ RCT_EXPORT_METHOD(getLaunchArgs:(NSString*)commandId :(RCTPromiseResolveBlock)re
101 102
 	resolve(args);
102 103
 }
103 104
 
105
+RCT_EXPORT_METHOD(getConstants:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) {
106
+	resolve([Constants getConstants]);
107
+}
108
+
104 109
 @end
105 110
 

+ 8
- 0
lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj Ver arquivo

@@ -91,6 +91,8 @@
91 91
 		50570BEB2063E09B006A1B5C /* RNNTitleViewHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 50570BE92063E09B006A1B5C /* RNNTitleViewHelper.m */; };
92 92
 		5064495D20DC62B90026709C /* RNNSideMenuSideOptions.h in Headers */ = {isa = PBXBuildFile; fileRef = 5064495B20DC62B90026709C /* RNNSideMenuSideOptions.h */; };
93 93
 		5064495E20DC62B90026709C /* RNNSideMenuSideOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = 5064495C20DC62B90026709C /* RNNSideMenuSideOptions.m */; };
94
+		50644A2020E11A720026709C /* Constants.h in Headers */ = {isa = PBXBuildFile; fileRef = 50644A1E20E11A720026709C /* Constants.h */; };
95
+		50644A2120E11A720026709C /* Constants.m in Sources */ = {isa = PBXBuildFile; fileRef = 50644A1F20E11A720026709C /* Constants.m */; };
94 96
 		506A2B1420973DFD00F43A95 /* RNNErrorHandler.h in Headers */ = {isa = PBXBuildFile; fileRef = 506A2B1220973DFD00F43A95 /* RNNErrorHandler.h */; };
95 97
 		506A2B1520973DFD00F43A95 /* RNNErrorHandler.m in Sources */ = {isa = PBXBuildFile; fileRef = 506A2B1320973DFD00F43A95 /* RNNErrorHandler.m */; };
96 98
 		50706E6D20CE7CA5003345C3 /* UIImage+tint.h in Headers */ = {isa = PBXBuildFile; fileRef = 50706E6B20CE7CA5003345C3 /* UIImage+tint.h */; };
@@ -307,6 +309,8 @@
307 309
 		50570BE92063E09B006A1B5C /* RNNTitleViewHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNTitleViewHelper.m; sourceTree = "<group>"; };
308 310
 		5064495B20DC62B90026709C /* RNNSideMenuSideOptions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNSideMenuSideOptions.h; sourceTree = "<group>"; };
309 311
 		5064495C20DC62B90026709C /* RNNSideMenuSideOptions.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNSideMenuSideOptions.m; sourceTree = "<group>"; };
312
+		50644A1E20E11A720026709C /* Constants.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Constants.h; sourceTree = "<group>"; };
313
+		50644A1F20E11A720026709C /* Constants.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Constants.m; sourceTree = "<group>"; };
310 314
 		506A2B1220973DFD00F43A95 /* RNNErrorHandler.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNNErrorHandler.h; sourceTree = "<group>"; };
311 315
 		506A2B1320973DFD00F43A95 /* RNNErrorHandler.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNNErrorHandler.m; sourceTree = "<group>"; };
312 316
 		50706E6B20CE7CA5003345C3 /* UIImage+tint.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "UIImage+tint.h"; sourceTree = "<group>"; };
@@ -466,6 +470,8 @@
466 470
 				390AD476200F499D00A8250D /* RNNSwizzles.m */,
467 471
 				506A2B1220973DFD00F43A95 /* RNNErrorHandler.h */,
468 472
 				506A2B1320973DFD00F43A95 /* RNNErrorHandler.m */,
473
+				50644A1E20E11A720026709C /* Constants.h */,
474
+				50644A1F20E11A720026709C /* Constants.m */,
469 475
 				50706E6B20CE7CA5003345C3 /* UIImage+tint.h */,
470 476
 				50706E6C20CE7CA5003345C3 /* UIImage+tint.m */,
471 477
 			);
@@ -830,6 +836,7 @@
830 836
 				7B1126A31E2D2B6C00F9B03B /* RNNSplashScreen.h in Headers */,
831 837
 				261F0E641E6EC94900989DE2 /* RNNModalManager.h in Headers */,
832 838
 				263905C01E4C6F440023D7D3 /* SidebarAirbnbAnimation.h in Headers */,
839
+				50644A2020E11A720026709C /* Constants.h in Headers */,
833 840
 				E8367B801F7A8A4700675C05 /* VICMAImageView.h in Headers */,
834 841
 				263905E61E4CAC950023D7D3 /* RNNSideMenuChildVC.h in Headers */,
835 842
 				263905C41E4C6F440023D7D3 /* SidebarFacebookAnimation.h in Headers */,
@@ -1012,6 +1019,7 @@
1012 1019
 				390AD478200F499D00A8250D /* RNNSwizzles.m in Sources */,
1013 1020
 				7BA500751E2544B9001B9E1B /* ReactNativeNavigation.m in Sources */,
1014 1021
 				263905B21E4C6F440023D7D3 /* MMDrawerController.m in Sources */,
1022
+				50644A2120E11A720026709C /* Constants.m in Sources */,
1015 1023
 				E8AEDB3D1F55A1C2000F5A6A /* RNNElementView.m in Sources */,
1016 1024
 				E33AC20520B5C3890090DB8A /* RNNStatusBarOptions.m in Sources */,
1017 1025
 				E83BAD6B1F27363A00A9F3DD /* RNNNavigationOptions.m in Sources */,

+ 2
- 2
lib/src/Navigation.ts Ver arquivo

@@ -162,7 +162,7 @@ export class Navigation {
162 162
   /**
163 163
    * Constants coming from native
164 164
    */
165
-  public constants(): Constants {
166
-    return Constants.get();
165
+  public async constants(): Promise<any> {
166
+    return await Constants.get();
167 167
   }
168 168
 }

+ 10
- 6
lib/src/adapters/Constants.ts Ver arquivo

@@ -1,9 +1,10 @@
1 1
 import { NativeModules } from 'react-native';
2 2
 
3 3
 export class Constants {
4
-  static get(): Constants {
4
+  static async get(): Promise<any> {
5 5
     if (!this.instance) {
6
-      this.instance = new Constants();
6
+      const constants = await NativeModules.RNNBridgeModule.getConstants();
7
+      this.instance = new Constants(constants);
7 8
     }
8 9
     return this.instance;
9 10
   }
@@ -12,10 +13,13 @@ export class Constants {
12 13
 
13 14
   public readonly statusBarHeight: number;
14 15
   public readonly backButtonId: string;
16
+  public readonly topBarHeight: number;
17
+  public readonly bottomTabsHeight: number;
15 18
 
16
-  private constructor() {
17
-    const m = NativeModules.RNNBridgeModule;
18
-    this.statusBarHeight = m.statusBarHeight;
19
-    this.backButtonId = m.backButtonId;
19
+  private constructor(constants) {
20
+    this.statusBarHeight = constants.statusBarHeight;
21
+    this.topBarHeight = constants.topBarHeight;
22
+    this.backButtonId = constants.backButtonId;
23
+    this.bottomTabsHeight = constants.bottomTabsHeight;
20 24
   }
21 25
 }