Browse Source

V2 constants (#3486)

* Constants support

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

Also added topBarHeight and bottomTabsHeight
Yogev Ben David 6 years ago
parent
commit
65aa48012c
No account linked to committer's email address

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

4
     public static final String BACK_BUTTON_JS_KEY = "backButtonId";
4
     public static final String BACK_BUTTON_JS_KEY = "backButtonId";
5
     public static final String BACK_BUTTON_ID = "RNN.back";
5
     public static final String BACK_BUTTON_ID = "RNN.back";
6
     public static final String STATUS_BAR_HEIGHT_KEY = "statusBarHeight";
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 View File

3
 import android.support.annotation.NonNull;
3
 import android.support.annotation.NonNull;
4
 
4
 
5
 import com.facebook.react.ReactInstanceManager;
5
 import com.facebook.react.ReactInstanceManager;
6
+import com.facebook.react.bridge.Arguments;
6
 import com.facebook.react.bridge.Promise;
7
 import com.facebook.react.bridge.Promise;
7
 import com.facebook.react.bridge.ReactApplicationContext;
8
 import com.facebook.react.bridge.ReactApplicationContext;
8
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
9
 import com.facebook.react.bridge.ReactContextBaseJavaModule;
9
 import com.facebook.react.bridge.ReactMethod;
10
 import com.facebook.react.bridge.ReactMethod;
10
 import com.facebook.react.bridge.ReadableMap;
11
 import com.facebook.react.bridge.ReadableMap;
12
+import com.facebook.react.bridge.WritableMap;
11
 import com.reactnativenavigation.NavigationActivity;
13
 import com.reactnativenavigation.NavigationActivity;
12
 import com.reactnativenavigation.NavigationApplication;
14
 import com.reactnativenavigation.NavigationApplication;
13
 import com.reactnativenavigation.parse.LayoutFactory;
15
 import com.reactnativenavigation.parse.LayoutFactory;
24
 import com.reactnativenavigation.viewcontrollers.ViewController;
26
 import com.reactnativenavigation.viewcontrollers.ViewController;
25
 import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalComponentCreator;
27
 import com.reactnativenavigation.viewcontrollers.externalcomponent.ExternalComponentCreator;
26
 
28
 
27
-import java.util.HashMap;
28
 import java.util.Map;
29
 import java.util.Map;
29
 
30
 
30
 public class NavigationModule extends ReactContextBaseJavaModule {
31
 public class NavigationModule extends ReactContextBaseJavaModule {
46
 		return NAME;
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
 	@ReactMethod
60
 	@ReactMethod

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

12
 import android.view.WindowManager;
12
 import android.view.WindowManager;
13
 
13
 
14
 public class UiUtils {
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
     private static int statusBarHeight = -1;
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
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
23
         view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
21
             @Override
24
             @Override
22
             public boolean onPreDraw() {
25
             public boolean onPreDraw() {
61
         return statusBarHeight;
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
     public static float dpToPx(Context context, float dp) {
79
     public static float dpToPx(Context context, float dp) {
65
         float scale = context.getResources().getDisplayMetrics().density;
80
         float scale = context.getResources().getDisplayMetrics().density;
66
         return dp * scale + 0.5f;
81
         return dp * scale + 0.5f;

+ 8
- 0
lib/ios/Constants.h View File

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 View File

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 View File

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

+ 8
- 0
lib/ios/ReactNativeNavigation.xcodeproj/project.pbxproj View File

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

+ 2
- 2
lib/src/Navigation.ts View File

162
   /**
162
   /**
163
    * Constants coming from native
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 View File

1
 import { NativeModules } from 'react-native';
1
 import { NativeModules } from 'react-native';
2
 
2
 
3
 export class Constants {
3
 export class Constants {
4
-  static get(): Constants {
4
+  static async get(): Promise<any> {
5
     if (!this.instance) {
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
     return this.instance;
9
     return this.instance;
9
   }
10
   }
12
 
13
 
13
   public readonly statusBarHeight: number;
14
   public readonly statusBarHeight: number;
14
   public readonly backButtonId: string;
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
 }