Skip to content

Commit 4da3f38

Browse files
committed
refactor Constants and fix issue with statusBarHeight on iOS
1 parent 0d9b7d6 commit 4da3f38

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

src/helpers/Constants.js

Lines changed: 53 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,78 +4,91 @@ const dimensionsScope = {
44
WINDOW: 'window',
55
SCREEN: 'screen'
66
};
7-
export const orientations = {
7+
8+
const constants = {};
9+
10+
constants.orientations = {
811
PORTRAIT: 'portrait',
912
LANDSCAPE: 'landscape'
1013
};
1114

1215
/* Platform */
13-
export const isAndroid = Platform.OS === 'android';
14-
export const isIOS = Platform.OS === 'ios';
16+
constants.isAndroid = Platform.OS === 'android';
17+
constants.isIOS = Platform.OS === 'ios';
1518

16-
export function getAndroidVersion() {
17-
return isAndroid ? parseInt(Platform.Version, 10) : undefined;
18-
}
19+
constants.getAndroidVersion = () => {
20+
return constants.isAndroid ? parseInt(Platform.Version, 10) : undefined;
21+
};
1922

2023
/* Navigation */
21-
const {StatusBarManager} = NativeModules;
22-
export const statusBarHeight = setStatusBarHeight();
23-
2424
function setStatusBarHeight() {
25-
let height = 0;
26-
height = isIOS ? 20 : StatusBarManager.HEIGHT;
27-
if (isIOS) {
25+
const {StatusBarManager} = NativeModules;
26+
constants.statusBarHeight = 0; // so there will be a value for any case
27+
constants.statusBarHeight = constants.isIOS ? 20 : StatusBarManager.HEIGHT;
28+
if (constants.isIOS) {
2829
// override guesstimate height with the actual height from StatusBarManager
29-
StatusBarManager.getHeight(data => (height = data.height));
30+
StatusBarManager.getHeight(data => (constants.statusBarHeight = data.height));
3031
}
31-
return height;
3232
}
3333

34-
/* Layout */
35-
export const isRTL = I18nManager.isRTL;
34+
setStatusBarHeight();
3635

36+
/* Layout */
3737
const {height, width} = Dimensions.get(dimensionsScope.SCREEN);
38-
export let orientation = getOrientation(height, width);
39-
export let isLandscape = orientation === orientations.LANDSCAPE;
40-
export let screenWidth = width;
41-
export let screenHeight = height;
42-
export let isSmallScreen = screenWidth <= 340;
43-
export let isShortScreen = screenHeight <= 600;
44-
export const screenAspectRatio = screenWidth < screenHeight ? screenHeight / screenWidth : screenWidth / screenHeight;
45-
export const isTablet = Platform.isPad || (screenAspectRatio < 1.6 && Math.max(screenWidth, screenHeight) >= 900);
46-
47-
export function getSafeAreaInsets() {
48-
return orientation === orientations.LANDSCAPE
38+
39+
constants.isRTL = I18nManager.isRTL;
40+
constants.orientation = getOrientation(height, width);
41+
constants.isLandscape = constants.orientation === constants.orientations.LANDSCAPE;
42+
constants.screenWidth = width;
43+
constants.screenHeight = height;
44+
constants.isSmallScreen = constants.screenWidth <= 340;
45+
constants.isShortScreen = constants.screenHeight <= 600;
46+
constants.screenAspectRatio =
47+
constants.screenWidth < constants.screenHeight
48+
? constants.screenHeight / constants.screenWidth
49+
: constants.screenWidth / constants.screenHeight;
50+
constants.isTablet =
51+
Platform.isPad ||
52+
(constants.screenAspectRatio < 1.6 && Math.max(constants.screenWidth, constants.screenHeight) >= 900);
53+
54+
constants.getSafeAreaInsets = () => {
55+
return constants.orientation === constants.orientations.LANDSCAPE
4956
? {left: 44, right: 44, bottom: 24, top: 0}
5057
: {left: 0, right: 0, bottom: 34, top: 44};
51-
}
58+
};
5259

5360
/* Devices */
54-
export const isIphoneX = isIOS && !Platform.isPad && !Platform.isTVOS && (screenHeight >= 812 || screenWidth >= 812);
61+
constants.isIphoneX =
62+
constants.isIOS &&
63+
!Platform.isPad &&
64+
!Platform.isTVOS &&
65+
(constants.screenHeight >= 812 || constants.screenWidth >= 812);
5566

5667
/* Orientation */
5768
function getOrientation(height, width) {
58-
return width < height ? orientations.PORTRAIT : orientations.LANDSCAPE;
69+
return width < height ? constants.orientations.PORTRAIT : constants.orientations.LANDSCAPE;
5970
}
6071

6172
function updateConstants(dimensions) {
6273
const {height, width} = dimensions.screen;
63-
orientation = getOrientation(height, width);
64-
isLandscape = orientation === orientations.LANDSCAPE;
65-
screenWidth = width;
66-
screenHeight = height;
67-
isSmallScreen = screenWidth <= 340;
68-
isShortScreen = screenHeight <= 600;
74+
constants.orientation = getOrientation(height, width);
75+
constants.isLandscape = constants.orientation === constants.orientations.LANDSCAPE;
76+
constants.screenWidth = width;
77+
constants.screenHeight = height;
78+
constants.isSmallScreen = constants.screenWidth <= 340;
79+
constants.isShortScreen = constants.screenHeight <= 600;
6980

7081
setStatusBarHeight();
7182
}
7283

7384
Dimensions.addEventListener('change', updateConstants);
7485

75-
export function addDimensionsEventListener(callback) {
86+
constants.addDimensionsEventListener = (callback) => {
7687
Dimensions.addEventListener('change', callback);
77-
}
88+
};
7889

79-
export function removeDimensionsEventListener(callback) {
90+
constants.removeDimensionsEventListener = (callback) => {
8091
Dimensions.removeEventListener('change', callback);
81-
}
92+
};
93+
94+
export default constants;

src/helpers/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module.exports = {
33
return require('./AvatarHelper');
44
},
55
get Constants() {
6-
return require('./Constants');
6+
return require('./Constants').default;
77
},
88
get DocsGenerator() {
99
return require('./DocsGenerator');

0 commit comments

Comments
 (0)