@applicaster/zapp-react-native-utils
Version:
Applicaster Zapp React Native utilities package
117 lines (103 loc) • 2.88 kB
text/typescript
import {
allowedOrientationsForScreen,
ORIENTATIONS,
} from "@applicaster/zapp-react-native-utils/appUtils/orientationHelper";
const isOrientationPortrait = (orientation: ORIENTATIONS): boolean => {
return [ORIENTATIONS.portrait, ORIENTATIONS.portraitUpsideDown].includes(
orientation
);
};
const isOrientationLandscape = (orientation: ORIENTATIONS): boolean => {
return [
ORIENTATIONS.landscapeLeft,
ORIENTATIONS.landscapeRight,
ORIENTATIONS.landscapeSensor,
].includes(orientation);
};
export const orientationWasChangedFromLandscapeToPortrait = ({
fromOrientation,
toOrientation,
}): boolean => {
return (
(isOrientationLandscape(fromOrientation) ||
ORIENTATIONS.unknown === fromOrientation) &&
isOrientationPortrait(toOrientation)
);
};
export const orientationWasChangedFromPortraitToLandscape = ({
fromOrientation,
toOrientation,
}): boolean => {
return (
(isOrientationPortrait(fromOrientation) ||
ORIENTATIONS.unknown === fromOrientation) &&
isOrientationLandscape(toOrientation)
);
};
export const orientationWasChangedFromLandscapeToLandscape = ({
fromOrientation,
toOrientation,
}): boolean => {
return (
isOrientationLandscape(fromOrientation) &&
isOrientationLandscape(toOrientation)
);
};
export const printOrientation = (orientation: ORIENTATIONS): string => {
switch (orientation) {
case ORIENTATIONS.unknown:
return "unknown";
case ORIENTATIONS.portrait:
return "portrait";
case ORIENTATIONS.landscapeRight:
return "landscapeRight";
case ORIENTATIONS.landscapeLeft:
return "landscapeLeft";
case ORIENTATIONS.landscapeSensor:
return "landscapeSensor";
case ORIENTATIONS.allButUpsideDown:
return "allButUpsideDown";
case ORIENTATIONS.portraitUpsideDown:
return "portraitUpsideDown";
case ORIENTATIONS.all:
return "all";
default:
return `orientation:<${orientation}> is not recognized`;
}
};
export const transitToOnlyPortrait = () => {
allowedOrientationsForScreen(ORIENTATIONS.portrait);
};
export const transitToOnlyLandscape = () => {
allowedOrientationsForScreen(ORIENTATIONS.landscapeSensor);
};
export const noop = () => {};
export const skipIf = (condition, fn) => (condition ? noop : fn);
export const shouldHandleOrientationChange = ({
toOrientation,
isPortrait,
docked,
physicalChange,
skipAllEffects,
}: {
toOrientation: ORIENTATIONS;
docked: boolean;
physicalChange?: boolean;
skipAllEffects: boolean;
isPortrait?: boolean;
}) => {
if (
physicalChange === false ||
docked ||
skipAllEffects ||
toOrientation === ORIENTATIONS.portraitUpsideDown
) {
return false;
}
if (!isPortrait) {
return [ORIENTATIONS.landscapeLeft, ORIENTATIONS.landscapeRight].includes(
toOrientation
);
}
return true;
};