xref: /MusicFree/src/hooks/useOrientation.ts (revision ab5f994a52bee1ac7c89f2ccf9b6ca60d362890a)
14173d3ebS猫头猫import {atom, useAtomValue, useSetAtom} from 'jotai';
24173d3ebS猫头猫import {useEffect} from 'react';
34173d3ebS猫头猫import {Dimensions} from 'react-native';
44173d3ebS猫头猫
5*ab5f994aSmaotoumaoconst orientationAtom = atom<'vertical' | 'horizontal'>('vertical');
64173d3ebS猫头猫
74173d3ebS猫头猫export function useListenOrientationChange() {
84173d3ebS猫头猫    const setOrientationAtom = useSetAtom(orientationAtom);
94173d3ebS猫头猫    useEffect(() => {
104173d3ebS猫头猫        const windowSize = Dimensions.get('window');
114173d3ebS猫头猫        const {width, height} = windowSize;
124173d3ebS猫头猫        if (width < height) {
134173d3ebS猫头猫            setOrientationAtom('vertical');
144173d3ebS猫头猫        } else {
15*ab5f994aSmaotoumao            setOrientationAtom('horizontal');
164173d3ebS猫头猫        }
174173d3ebS猫头猫        const subscription = Dimensions.addEventListener('change', e => {
184173d3ebS猫头猫            if (e.window.width < e.window.height) {
194173d3ebS猫头猫                setOrientationAtom('vertical');
204173d3ebS猫头猫            } else {
21*ab5f994aSmaotoumao                setOrientationAtom('horizontal');
224173d3ebS猫头猫            }
234173d3ebS猫头猫        });
244173d3ebS猫头猫
254173d3ebS猫头猫        return () => {
264173d3ebS猫头猫            subscription?.remove();
274173d3ebS猫头猫        };
284173d3ebS猫头猫    }, []);
294173d3ebS猫头猫}
304173d3ebS猫头猫
314173d3ebS猫头猫export default function useOrientation() {
324173d3ebS猫头猫    return useAtomValue(orientationAtom);
334173d3ebS猫头猫}
34