Created
December 7, 2019 18:03
-
-
Save keima/c4603fdaec7671a5d98b1dd7c391caea to your computer and use it in GitHub Desktop.
React Native orientation change listener
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { orientationListenerSaga } from './saga' | |
import { getOrientation, Orientation } from './util' | |
import actionCreatorFactory from 'typescript-fsa' | |
import { reducerWithInitialState } from 'typescript-fsa-reducers' | |
const actionCreator = actionCreatorFactory('device') | |
const changeOrientation = actionCreator<{ orientation: Orientation }>( | |
'CHANGE_ORIENTATION', | |
) | |
export const forSaga = { | |
changeOrientation, | |
} | |
export { orientationListenerSaga } | |
export class DeviceState { | |
orientation: Orientation = getOrientation() | |
} | |
export default reducerWithInitialState<DeviceState>(new DeviceState()) | |
.case(changeOrientation, (state, { orientation }) => { | |
return { | |
...state, | |
orientation, | |
} | |
}) | |
.build() | |
export { Orientation } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MIT License | |
Copyright (c) 2019 OTOBANK Inc. | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { forSaga } from './index' | |
import { getOrientation, Orientation } from './util' | |
import { Dimensions, ScaledSize } from 'react-native' | |
import { eventChannel } from 'redux-saga' | |
import { call, cancelled, fork, put, take } from 'redux-saga/effects' | |
function newDimensionsChangedListener( | |
callback: (orientation: Orientation) => void, | |
) { | |
return (newDimensions: { window: ScaledSize; screen: ScaledSize }) => { | |
const orientation = getOrientation(newDimensions.window) | |
callback(orientation) | |
} | |
} | |
function createDimensionsChangedChannel() { | |
return eventChannel((cb) => { | |
const listener = newDimensionsChangedListener(cb) | |
Dimensions.addEventListener('change', listener) | |
return () => { | |
Dimensions.removeEventListener('change', listener) | |
} | |
}) | |
} | |
function* orientationChangesListenerSaga() { | |
const chan = yield call(createDimensionsChangedChannel) | |
try { | |
while (true) { | |
const orientation = yield take(chan) | |
yield put(forSaga.changeOrientation({ orientation })) | |
} | |
} finally { | |
if (yield cancelled()) { | |
chan.close() | |
} | |
} | |
} | |
export function* orientationListenerSaga() { | |
yield fork(orientationChangesListenerSaga) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { State } from '@/redux' | |
export function getDeviceOrientation(state: State) { | |
return state.device.orientation | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Dimensions, ScaledSize } from 'react-native' | |
export type Orientation = 'portrait' | 'landscape' | |
export function getOrientation( | |
{ width, height }: ScaledSize = Dimensions.get('window'), | |
) { | |
// TODO: iPadのSplitViewなどで誤検知するかもしれない | |
return width < height ? 'portrait' : 'landscape' | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment