Skip to content

Commit 7cfa780

Browse files
Inbal-Tishethanshar
authored andcommitted
Feat/orientation support (#476)
* Constants - adding support for orientation event * Carousel - refactor and add support for orientation change; fix for Carousel-related components (ActionBar's demo screen, MainScreen) * Revert "Carousel - refactor and add support for orientation change; fix for Carousel-related components (ActionBar's demo screen, MainScreen)" This reverts commit 0dbe448. * pr fixes
1 parent 477d5c1 commit 7cfa780

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

src/helpers/Constants.js

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,77 @@
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+
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) ?
2349
{left: 44, right: 44, bottom: 24, top: 0} :
2450
{left: 0, right: 0, bottom: 34, top: 44};
2551
}
2652

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);
3077
}

0 commit comments

Comments
 (0)