Files
notesnook/apps/mobile/App.js

119 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-04-20 09:16:01 +05:00
import React, {useEffect, useState} from 'react';
2020-03-15 10:20:34 +05:00
import MMKV from 'react-native-mmkv-storage';
import Orientation from 'react-native-orientation';
2020-04-20 09:16:01 +05:00
import {Loading} from './Loading';
import {getColorScheme, scale, updateSize} from './src/common/common';
import {useTracked} from './src/provider';
import {ACTIONS} from './src/provider/actions';
import {defaultState} from './src/provider/defaultState';
import {eSubscribeEvent, eUnSubscribeEvent} from './src/services/eventManager';
2020-05-04 18:09:33 +05:00
import {eDispatchAction, eStartSyncer} from './src/services/events';
2020-04-20 09:16:01 +05:00
import {db, DDS} from './src/utils/utils';
import {SafeAreaProvider} from 'react-native-safe-area-context';
2020-01-14 20:48:03 +05:00
2019-11-15 01:17:59 +05:00
const App = () => {
2020-01-17 00:23:16 +05:00
const [state, dispatch] = useTracked();
const [init, setInit] = useState(false);
const I = DDS.isTab ? require('./index.tablet') : require('./index.mobile');
2020-02-22 20:00:57 +05:00
const _onOrientationChange = o => {
// Currently orientation is locked on tablet.
/* DDS.checkOrientation();
setTimeout(() => {
2020-03-10 23:19:16 +05:00
2020-02-22 20:00:57 +05:00
forceUpdate();
}, 1000); */
};
2020-01-17 21:04:21 +05:00
useEffect(() => {
2020-02-22 20:00:57 +05:00
Orientation.addOrientationListener(_onOrientationChange);
2020-01-25 19:42:11 +05:00
eSubscribeEvent(eDispatchAction, type => {
2020-01-17 21:04:21 +05:00
dispatch(type);
});
return () => {
2020-01-25 19:42:11 +05:00
eUnSubscribeEvent(eDispatchAction, type => {
2020-01-17 21:04:21 +05:00
dispatch(type);
2019-12-04 19:38:19 +05:00
});
2020-02-22 20:00:57 +05:00
Orientation.removeOrientationListener(_onOrientationChange);
2019-11-23 06:25:22 +05:00
};
2019-11-29 11:31:43 +05:00
}, []);
2020-01-25 23:47:17 +05:00
useEffect(() => {
2020-02-23 09:40:06 +05:00
DDS.isTab ? Orientation.lockToLandscape() : Orientation.lockToPortrait();
2020-01-25 23:47:17 +05:00
}, []);
2020-05-05 23:04:30 +05:00
const startSyncer = async () => {
2020-05-04 18:09:33 +05:00
let user = await db.user.get();
if (user) {
db.ev.subscribe('sync',async () => {
dispatch({type: ACTIONS.SYNCING, syncing:true});
await db.sync();
let u = await db.user.get();
dispatch({type: ACTIONS.USER, user: u});
dispatch({type: ACTIONS.ALL});
2020-05-04 18:09:33 +05:00
dispatch({type: ACTIONS.SYNCING, syncing:false});
})
}
}
useEffect(() => {
eSubscribeEvent(eStartSyncer, startSyncer)
return () => {
eUnSubscribeEvent(eStartSyncer,startSyncer)
}
})
2019-12-07 12:05:15 +05:00
useEffect(() => {
2020-03-15 10:20:34 +05:00
Initialize().then(() => {
2020-02-12 02:09:41 +05:00
db.init().then(async () => {
let user = await db.user.get();
2020-05-04 18:09:33 +05:00
startSyncer();
2020-04-20 09:16:01 +05:00
dispatch({type: ACTIONS.USER, user: user});
2020-01-17 21:04:21 +05:00
setInit(true);
});
2019-12-21 10:38:40 +05:00
});
2020-01-17 21:26:01 +05:00
}, []);
2019-12-21 10:38:40 +05:00
2020-03-15 10:20:34 +05:00
async function Initialize(colors = colors) {
2020-01-17 21:04:21 +05:00
let newColors = await getColorScheme(colors);
2020-03-24 13:05:56 +05:00
let s;
try {
s = await MMKV.getStringAsync('settings');
2020-04-20 09:16:01 +05:00
} catch (e) {}
2020-02-23 11:47:14 +05:00
if (typeof s !== 'string') {
s = defaultState.settings;
s = JSON.stringify(s);
s.fontScale = 1;
2020-03-24 13:05:56 +05:00
2020-03-23 19:53:12 +05:00
await MMKV.setStringAsync('settings', s);
2020-03-24 13:05:56 +05:00
2020-04-20 09:16:01 +05:00
dispatch({type: ACTIONS.SETTINGS, s});
2020-02-23 11:47:14 +05:00
} else {
s = JSON.parse(s);
if (s.fontScale) {
scale.fontScale = s.fontScale;
} else {
scale.fontScale = 1;
}
2020-02-23 11:47:14 +05:00
updateSize();
2020-04-20 09:16:01 +05:00
dispatch({type: ACTIONS.SETTINGS, settings: {...s}});
2020-02-23 11:47:14 +05:00
}
2020-04-20 10:54:58 +05:00
2020-04-20 09:16:01 +05:00
dispatch({type: ACTIONS.THEME, colors: newColors});
2020-01-17 21:04:21 +05:00
}
2020-01-17 00:23:16 +05:00
if (!init) {
2020-01-14 20:48:03 +05:00
return <></>;
2019-12-11 01:10:04 +05:00
}
2019-11-17 16:02:19 +05:00
return (
2020-04-16 13:57:51 +05:00
<SafeAreaProvider>
<>
<I.Initialize />
<Loading />
</>
</SafeAreaProvider>
2019-11-15 01:17:59 +05:00
);
};
export default App;