Files
notesnook/apps/mobile/App.js

99 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-04-12 20:11:31 +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-12 20:11:31 +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';
import { eDispatchAction } from './src/services/events';
import { db, DDS } from './src/utils/utils';
import { test } from './src/utils/storage';
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');
console.log(DDS.isTab, 'TABLET');
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
}, []);
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 () => {
2020-04-12 20:11:31 +05:00
2020-02-12 02:09:41 +05:00
let user = await db.user.get();
2020-04-12 20:11:31 +05:00
dispatch({ type: ACTIONS.USER, user: user });
2020-03-21 11:30:35 +05:00
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-12 20:11:31 +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-12 20:11:31 +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-03-03 09:49:54 +05:00
2020-04-12 20:11:31 +05:00
dispatch({ type: ACTIONS.SETTINGS, settings: { ...s } });
2020-02-23 11:47:14 +05:00
}
2020-04-12 20:11:31 +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-01-17 21:04:21 +05:00
<>
2020-03-15 11:03:35 +05:00
<I.Initialize />
<Loading />
2020-01-17 21:04:21 +05:00
</>
2019-11-15 01:17:59 +05:00
);
};
export default App;