|
| 1 | +import _ from 'lodash'; |
1 | 2 | import {Platform, Dimensions, NativeModules, I18nManager} from 'react-native';
|
2 | 3 |
|
3 |
| -const {StatusBarManager} = NativeModules; |
4 |
| -const {height, width} = Dimensions.get('window'); |
5 | 4 |
|
| 5 | +const dimensionsScope = { |
| 6 | + WINDOW: 'window', |
| 7 | + SCREEN: 'screen' |
| 8 | +} |
| 9 | +const orientations = { |
| 10 | + PORTRAIT: 'portrait', |
| 11 | + LANDSCAPE: 'landscape' |
| 12 | +} |
| 13 | + |
| 14 | +/* Platform */ |
6 | 15 | export const isAndroid = Platform.OS === 'android';
|
7 | 16 | 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; |
16 | 17 |
|
17 | 18 | export function getAndroidVersion() {
|
18 | 19 | return isAndroid ? parseInt(Platform.Version, 10) : undefined;
|
19 | 20 | }
|
20 | 21 |
|
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 | +const {height, width} = Dimensions.get(dimensionsScope.WINDOW); |
| 41 | +export let orientation = getOrientation(height, width); |
| 42 | +export let screenWidth = width; |
| 43 | +export let screenHeight = height; |
| 44 | +export let isSmallScreen = screenWidth <= 340; |
| 45 | +export let isShortScreen = screenHeight <= 600; |
| 46 | + |
| 47 | +export function getSafeAreaInsets() { |
| 48 | + return (orientation === orientation.LANDSCAPE) ? |
23 | 49 | {left: 44, right: 44, bottom: 24, top: 0} :
|
24 | 50 | {left: 0, right: 0, bottom: 34, top: 44};
|
25 | 51 | }
|
26 | 52 |
|
27 |
| -// override guesstimate height with the actual height from StatusBarManager |
28 |
| -if (isIOS) { |
29 |
| - StatusBarManager.getHeight(data => (statusBarHeight = data.height)); |
| 53 | +/* Orientation */ |
| 54 | +function getOrientation(height, width) { |
| 55 | + return width < height ? orientations.PORTRAIT : orientations.LANDSCAPE; |
| 56 | +} |
| 57 | + |
| 58 | +function updateConstants() { |
| 59 | + const {height, width} = Dimensions.get(dimensionsScope.WINDOW); |
| 60 | + orientation = getOrientation(height, width); |
| 61 | + screenWidth = width; |
| 62 | + screenHeight = height; |
| 63 | + isSmallScreen = screenWidth <= 340; |
| 64 | + isShortScreen = screenHeight <= 600; |
| 65 | + |
| 66 | + setStatusBarHeight(); |
| 67 | +} |
| 68 | + |
| 69 | +Dimensions.addEventListener('change', updateConstants); |
| 70 | + |
| 71 | +export function addDimensionsEventListener(callback) { |
| 72 | + Dimensions.addEventListener('change', callback); |
| 73 | +} |
| 74 | + |
| 75 | +export function removeDimensionsEventListener(callback) { |
| 76 | + Dimensions.removeEventListener('change', callback); |
30 | 77 | }
|
0 commit comments