Skip to content

Commit c52d0e4

Browse files
committed
Constants - adding support for orientation event
1 parent e86e9b5 commit c52d0e4

File tree

1 file changed

+64
-15
lines changed

1 file changed

+64
-15
lines changed

src/helpers/Constants.js

Lines changed: 64 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,79 @@
1+
import _ from 'lodash';
12
import {Platform, Dimensions, NativeModules, I18nManager} from 'react-native';
23

3-
const {StatusBarManager} = NativeModules;
4-
const {height, width} = Dimensions.get('window');
54

5+
const dimensionsScope = {
6+
WINDOW: 'window',
7+
SCREEN: 'screen'
8+
}
9+
const orientations = {
10+
PORTRAIT: 'portrait',
11+
LANDSCAPE: 'landscape'
12+
}
13+
14+
/* Platform */
615
export const isAndroid = Platform.OS === 'android';
716
export const isIOS = Platform.OS === 'ios';
8-
export const screenWidth = width;
9-
export const screenHeight = height;
10-
// export const isSmallScreen = isIOS ? screenWidth <= 320 : screenWidth <= 360;
11-
export const isSmallScreen = screenWidth <= 340;
12-
export const isShortScreen = screenHeight <= 600;
13-
export let statusBarHeight = isIOS ? 20 : StatusBarManager.HEIGHT; // eslint-disable-line
14-
export const isIphoneX = isIOS && !Platform.isPad && !Platform.isTVOS && (screenHeight >= 812 || screenWidth >= 812);
15-
export const isRTL = I18nManager.isRTL;
1617

1718
export function getAndroidVersion() {
1819
return isAndroid ? parseInt(Platform.Version, 10) : undefined;
1920
}
2021

21-
export function getSafeAreaInsets(mode) {
22-
return (mode === 'landscape') ?
22+
/* Devices */
23+
export const isIphoneX = isIOS && !Platform.isPad && !Platform.isTVOS && (screenHeight >= 812 || screenWidth >= 812);
24+
25+
/* Navigation */
26+
const {StatusBarManager} = NativeModules;
27+
export let statusBarHeight = setStatusBarHeight();
28+
29+
function setStatusBarHeight() {
30+
statusBarHeight = isIOS ? 20 : StatusBarManager.HEIGHT; // eslint-disable-line
31+
if (isIOS) {
32+
// override guesstimate height with the actual height from StatusBarManager
33+
StatusBarManager.getHeight(data => (statusBarHeight = data.height));
34+
}
35+
}
36+
37+
/* Layout */
38+
export const isRTL = I18nManager.isRTL;
39+
40+
export function getSafeAreaInsets() {
41+
const orientation = getOrientation();
42+
return (orientation === orientation.LANDSCAPE) ?
2343
{left: 44, right: 44, bottom: 24, top: 0} :
2444
{left: 0, right: 0, bottom: 34, top: 44};
2545
}
2646

27-
// override guesstimate height with the actual height from StatusBarManager
28-
if (isIOS) {
29-
StatusBarManager.getHeight(data => (statusBarHeight = data.height));
47+
/* Dimensions */
48+
const {height, width} = Dimensions.get(dimensionsScope.WINDOW);
49+
export let screenWidth = width;
50+
export let screenHeight = height;
51+
export let isSmallScreen = screenWidth <= 340;
52+
export let isShortScreen = screenHeight <= 600;
53+
54+
function updateConstants() {
55+
const {height, width} = Dimensions.get(dimensionsScope.WINDOW);
56+
screenWidth = width;
57+
screenHeight = height;
58+
isSmallScreen = screenWidth <= 340;
59+
isShortScreen = screenHeight <= 600;
60+
setStatusBarHeight();
61+
}
62+
63+
/* Orientation */
64+
Dimensions.addEventListener('change', () => {
65+
updateConstants();
66+
});
67+
68+
export function getOrientation() {
69+
const {height, width} = Dimensions.get(dimensionsScope.WINDOW);
70+
return width < height ? orientations.PORTRAIT : orientations.LANDSCAPE;
71+
}
72+
73+
export function addDimensionsEventListener(callback) {
74+
Dimensions.addEventListener('change', callback);
75+
}
76+
77+
export function removeDimensionsEventListener(callback) {
78+
Dimensions.removeEventListener('change', callback);
3079
}

0 commit comments

Comments
 (0)