2020-05-08 17:04:37 +00:00
|
|
|
// https://github.com/bamlab/redux-enhancer-react-native-appstate
|
|
|
|
import { AppState } from 'react-native';
|
|
|
|
|
2022-12-21 16:07:17 +00:00
|
|
|
import { removeNotificationsAndBadge } from '../notifications';
|
2022-04-07 14:19:54 +00:00
|
|
|
import { APP_STATE } from '../../actions/actionsTypes';
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
export default () =>
|
2022-04-07 14:19:54 +00:00
|
|
|
(createStore: any) =>
|
|
|
|
(...args: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const store = createStore(...args);
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
let currentState = '';
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2022-04-07 14:19:54 +00:00
|
|
|
const handleAppStateChange = (nextAppState: any) => {
|
2021-09-13 20:41:05 +00:00
|
|
|
if (nextAppState !== 'inactive') {
|
|
|
|
if (currentState !== nextAppState) {
|
|
|
|
let type;
|
|
|
|
if (nextAppState === 'active') {
|
|
|
|
type = APP_STATE.FOREGROUND;
|
2022-12-21 16:07:17 +00:00
|
|
|
removeNotificationsAndBadge();
|
2021-09-13 20:41:05 +00:00
|
|
|
} else if (nextAppState === 'background') {
|
|
|
|
type = APP_STATE.BACKGROUND;
|
|
|
|
}
|
|
|
|
if (type) {
|
|
|
|
store.dispatch({
|
|
|
|
type
|
|
|
|
});
|
|
|
|
}
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
currentState = nextAppState;
|
2020-05-08 17:04:37 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
AppState.addEventListener('change', handleAppStateChange);
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
// setTimeout to allow redux-saga to catch the initial state fired by redux-enhancer-react-native-appstate library
|
|
|
|
setTimeout(() => handleAppStateChange(AppState.currentState));
|
|
|
|
return store;
|
|
|
|
};
|