/
Constants.js
81 lines (65 loc) · 2.51 KB
/
Constants.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import _ from 'lodash';
import {Platform, Dimensions, NativeModules, I18nManager} from 'react-native';
const dimensionsScope = {
WINDOW: 'window',
SCREEN: 'screen'
}
export const orientations = {
PORTRAIT: 'portrait',
LANDSCAPE: 'landscape'
}
/* Platform */
export const isAndroid = Platform.OS === 'android';
export const isIOS = Platform.OS === 'ios';
export function getAndroidVersion() {
return isAndroid ? parseInt(Platform.Version, 10) : undefined;
}
/* Navigation */
const {StatusBarManager} = NativeModules;
export let statusBarHeight = setStatusBarHeight();
function setStatusBarHeight() {
statusBarHeight = isIOS ? 20 : StatusBarManager.HEIGHT;
if (isIOS) {
// override guesstimate height with the actual height from StatusBarManager
StatusBarManager.getHeight(data => (statusBarHeight = data.height));
}
}
/* Layout */
export const isRTL = I18nManager.isRTL;
const {height, width} = Dimensions.get(dimensionsScope.WINDOW);
export let orientation = getOrientation(height, width);
export let isLandscape = orientation === orientations.LANDSCAPE;
export let screenWidth = width;
export let screenHeight = height;
export let isSmallScreen = screenWidth <= 340;
export let isShortScreen = screenHeight <= 600;
export const screenAspectRatio = screenWidth < screenHeight ? screenHeight / screenWidth : screenWidth / screenHeight;
export const isTablet = Platform.isPad || (screenAspectRatio < 1.6 && Math.max(screenWidth, screenHeight) >= 900);
export function getSafeAreaInsets() {
return (orientation === orientations.LANDSCAPE) ?
{left: 44, right: 44, bottom: 24, top: 0} :
{left: 0, right: 0, bottom: 34, top: 44};
}
/* Devices */
export const isIphoneX = isIOS && !Platform.isPad && !Platform.isTVOS && (screenHeight >= 812 || screenWidth >= 812);
/* Orientation */
function getOrientation(height, width) {
return width < height ? orientations.PORTRAIT : orientations.LANDSCAPE;
}
function updateConstants() {
const {height, width} = Dimensions.get(dimensionsScope.WINDOW);
orientation = getOrientation(height, width);
isLandscape = orientation === orientations.LANDSCAPE;
screenWidth = width;
screenHeight = height;
isSmallScreen = screenWidth <= 340;
isShortScreen = screenHeight <= 600;
setStatusBarHeight();
}
Dimensions.addEventListener('change', updateConstants);
export function addDimensionsEventListener(callback) {
Dimensions.addEventListener('change', callback);
}
export function removeDimensionsEventListener(callback) {
Dimensions.removeEventListener('change', callback);
}