2021-09-13 20:41:05 +00:00
|
|
|
import { select, takeLatest } from 'redux-saga/effects';
|
2017-11-28 11:01:18 +00:00
|
|
|
|
2019-05-28 16:18:46 +00:00
|
|
|
import log from '../utils/log';
|
2020-05-08 17:04:37 +00:00
|
|
|
import { localAuthenticate, saveLastLocalAuthenticationSession } from '../utils/localAuthentication';
|
|
|
|
import { APP_STATE } from '../actions/actionsTypes';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { RootEnum } from '../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2022-05-13 14:37:02 +00:00
|
|
|
import { setBadgeCount } from '../lib/notifications';
|
2017-12-04 18:24:21 +00:00
|
|
|
|
2017-11-28 11:01:18 +00:00
|
|
|
const appHasComeBackToForeground = function* appHasComeBackToForeground() {
|
2018-08-01 19:35:06 +00:00
|
|
|
const appRoot = yield select(state => state.app.root);
|
2022-02-02 18:27:10 +00:00
|
|
|
if (appRoot === RootEnum.ROOT_OUTSIDE) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-03-05 16:10:21 +00:00
|
|
|
const login = yield select(state => state.login);
|
|
|
|
const server = yield select(state => state.server);
|
|
|
|
if (!login.isAuthenticated || login.isFetching || server.connecting || server.loading || server.changingServer) {
|
2017-12-04 18:24:21 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2021-03-05 16:10:21 +00:00
|
|
|
yield localAuthenticate(server.server);
|
2022-04-28 20:37:25 +00:00
|
|
|
Services.checkAndReopen();
|
2022-05-13 14:37:02 +00:00
|
|
|
setBadgeCount();
|
2022-04-28 20:37:25 +00:00
|
|
|
return yield Services.setUserPresenceOnline();
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-12-04 18:24:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const appHasComeBackToBackground = function* appHasComeBackToBackground() {
|
2018-08-01 19:35:06 +00:00
|
|
|
const appRoot = yield select(state => state.app.root);
|
2022-02-02 18:27:10 +00:00
|
|
|
if (appRoot === RootEnum.ROOT_OUTSIDE) {
|
2018-08-01 19:35:06 +00:00
|
|
|
return;
|
|
|
|
}
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
2020-05-08 17:04:37 +00:00
|
|
|
const server = yield select(state => state.server.server);
|
|
|
|
yield saveLastLocalAuthenticationSession(server);
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
yield Services.setUserPresenceAway();
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2020-05-08 17:04:37 +00:00
|
|
|
yield takeLatest(APP_STATE.FOREGROUND, appHasComeBackToForeground);
|
|
|
|
yield takeLatest(APP_STATE.BACKGROUND, appHasComeBackToBackground);
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export default root;
|