2021-03-18 13:18:33 +00:00
|
|
|
import { put, takeLatest } from 'redux-saga/effects';
|
2019-12-17 14:08:06 +00:00
|
|
|
import RNBootSplash from 'react-native-bootsplash';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
import { BIOMETRY_ENABLED_KEY, CURRENT_SERVER, TOKEN_KEY } from '../lib/constants';
|
2022-04-07 13:13:19 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { selectServerRequest, serverRequest } from '../actions/server';
|
2018-09-04 14:29:20 +00:00
|
|
|
import { setAllPreferences } from '../actions/sortPreferences';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { APP } from '../actions/actionsTypes';
|
2022-06-06 14:17:51 +00:00
|
|
|
import log from '../lib/methods/helpers/log';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { localAuthenticate } from '../lib/methods/helpers/localAuthentication';
|
2022-02-02 18:27:10 +00:00
|
|
|
import { appReady, appStart } from '../actions/app';
|
|
|
|
import { RootEnum } from '../definitions';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { getSortPreferences } from '../lib/methods';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
import appConfig from '../../app.json';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
2019-12-04 16:50:22 +00:00
|
|
|
export const initLocalSettings = function* initLocalSettings() {
|
2022-04-28 20:37:25 +00:00
|
|
|
const sortPreferences = getSortPreferences();
|
2019-12-04 16:50:22 +00:00
|
|
|
yield put(setAllPreferences(sortPreferences));
|
|
|
|
};
|
|
|
|
|
2022-02-08 16:44:34 +00:00
|
|
|
const BIOMETRY_MIGRATION_KEY = 'kBiometryMigration';
|
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
const restore = function* restore() {
|
|
|
|
try {
|
2022-05-30 22:01:25 +00:00
|
|
|
// const server = UserPreferences.getString(CURRENT_SERVER);
|
|
|
|
// let userId = UserPreferences.getString(`${TOKEN_KEY}-${server}`);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2022-02-08 16:44:34 +00:00
|
|
|
// Migration biometry setting from WatermelonDB to MMKV
|
|
|
|
// TODO: remove it after a few versions
|
2022-03-09 19:41:26 +00:00
|
|
|
const hasMigratedBiometry = UserPreferences.getBool(BIOMETRY_MIGRATION_KEY);
|
2022-02-08 16:44:34 +00:00
|
|
|
if (!hasMigratedBiometry) {
|
|
|
|
const serversDB = database.servers;
|
|
|
|
const serversCollection = serversDB.get('servers');
|
|
|
|
const servers = yield serversCollection.query().fetch();
|
|
|
|
const isBiometryEnabled = servers.some(server => !!server.biometry);
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.setBool(BIOMETRY_ENABLED_KEY, isBiometryEnabled);
|
|
|
|
UserPreferences.setBool(BIOMETRY_MIGRATION_KEY, true);
|
2022-02-08 16:44:34 +00:00
|
|
|
}
|
|
|
|
|
2022-05-30 22:01:25 +00:00
|
|
|
// if (!server) {
|
|
|
|
// yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
|
|
|
// } else if (!userId) {
|
|
|
|
// const serversDB = database.servers;
|
|
|
|
// const serversCollection = serversDB.get('servers');
|
|
|
|
// const servers = yield serversCollection.query().fetch();
|
2021-03-18 13:18:33 +00:00
|
|
|
|
2022-05-30 22:01:25 +00:00
|
|
|
// // Check if there're other logged in servers and picks first one
|
|
|
|
// if (servers.length > 0) {
|
|
|
|
// for (let i = 0; i < servers.length; i += 1) {
|
|
|
|
// const newServer = servers[i].id;
|
|
|
|
// userId = UserPreferences.getString(`${TOKEN_KEY}-${newServer}`);
|
|
|
|
// if (userId) {
|
|
|
|
// return yield put(selectServerRequest(newServer));
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
// }
|
2021-08-16 18:19:06 +00:00
|
|
|
const { server } = appConfig;
|
2022-05-30 22:01:25 +00:00
|
|
|
const userId = UserPreferences.getString(`${TOKEN_KEY}-${server}`);
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-08-16 18:19:06 +00:00
|
|
|
if (!userId) {
|
2022-05-30 22:01:25 +00:00
|
|
|
UserPreferences.removeItem(TOKEN_KEY);
|
|
|
|
UserPreferences.removeItem(CURRENT_SERVER);
|
2021-08-16 18:19:06 +00:00
|
|
|
yield put(serverRequest(appConfig.server));
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
2019-09-27 18:52:51 +00:00
|
|
|
} else {
|
2019-09-16 20:26:32 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const serverCollections = serversDB.get('servers');
|
2020-05-08 17:04:37 +00:00
|
|
|
|
2020-09-23 17:16:04 +00:00
|
|
|
let serverObj;
|
|
|
|
try {
|
|
|
|
yield localAuthenticate(server);
|
|
|
|
serverObj = yield serverCollections.find(server);
|
|
|
|
} catch {
|
|
|
|
// Server not found
|
|
|
|
}
|
2019-04-17 17:01:03 +00:00
|
|
|
yield put(selectServerRequest(server, serverObj && serverObj.version));
|
2018-12-05 20:52:08 +00:00
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
yield put(appReady({}));
|
2017-08-21 03:00:41 +00:00
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2022-02-02 18:27:10 +00:00
|
|
|
yield put(appStart({ root: RootEnum.ROOT_OUTSIDE }));
|
2017-08-21 03:00:41 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-28 11:01:18 +00:00
|
|
|
|
2021-07-20 13:23:29 +00:00
|
|
|
const start = function* start() {
|
2022-10-04 13:36:05 +00:00
|
|
|
yield RNBootSplash.hide({ fade: true });
|
2019-03-12 16:23:06 +00:00
|
|
|
};
|
|
|
|
|
2017-11-28 11:01:18 +00:00
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(APP.INIT, restore);
|
2019-03-12 16:23:06 +00:00
|
|
|
yield takeLatest(APP.START, start);
|
2019-12-04 16:50:22 +00:00
|
|
|
yield takeLatest(APP.INIT_LOCAL_SETTINGS, initLocalSettings);
|
2017-11-28 11:01:18 +00:00
|
|
|
};
|
|
|
|
export default root;
|