Files
notesnook/apps/mobile/App.js

129 lines
3.2 KiB
JavaScript
Raw Normal View History

2022-01-22 09:32:12 +05:00
import React, { useEffect } from 'react';
2021-01-11 14:29:50 +05:00
import Orientation from 'react-native-orientation';
2022-01-22 09:32:12 +05:00
import { SafeAreaProvider } from 'react-native-safe-area-context';
2021-02-25 12:16:38 +05:00
import AppLoader from './src/components/AppLoader';
2022-01-22 09:32:12 +05:00
import { RootView } from './src/navigation/RootView';
import { useTracked } from './src/provider';
2021-06-11 09:16:04 +05:00
import {
2022-01-22 09:32:12 +05:00
initialize, useSettingStore,
2021-07-17 20:34:07 +05:00
useUserStore
2021-06-11 09:16:04 +05:00
} from './src/provider/stores';
2022-01-22 09:32:12 +05:00
import { DDS } from './src/services/DeviceDetection';
2021-01-17 12:53:04 +05:00
import {
eSendEvent,
eSubscribeEvent,
2021-07-17 20:34:07 +05:00
eUnSubscribeEvent
2021-01-17 12:53:04 +05:00
} from './src/services/EventManager';
import Notifications from './src/services/Notifications';
2020-12-20 23:03:02 +05:00
import SettingsService from './src/services/SettingsService';
2022-01-22 09:32:12 +05:00
import { Tracker } from './src/utils';
import { db } from './src/utils/database';
import { eDispatchAction } from './src/utils/Events';
import { MMKV } from './src/utils/mmkv';
import { useAppEvents } from './src/utils/use-app-events';
import RNBootSplash from "react-native-bootsplash";
2020-11-16 12:36:41 +05:00
2021-06-15 18:43:05 +05:00
let databaseHasLoaded = false;
const loadDatabase = async () => {
2022-01-22 09:32:12 +05:00
RNBootSplash.hide({fade:true})
2021-06-15 18:43:05 +05:00
await db.init();
Notifications.get();
2021-07-28 11:37:40 +05:00
await checkFirstLaunch();
};
async function checkFirstLaunch() {
2021-06-15 18:43:05 +05:00
let requireIntro = await MMKV.getItem('introCompleted');
2021-06-26 08:47:52 +05:00
useSettingStore.getState().setIntroCompleted(requireIntro ? true : false);
2021-06-15 18:43:05 +05:00
if (!requireIntro) {
await MMKV.setItem(
'askForRating',
JSON.stringify({
2021-07-20 09:53:04 +05:00
timestamp: Date.now() + 86400000 * 2
})
2021-06-15 18:43:05 +05:00
);
2021-07-25 11:32:04 +05:00
await MMKV.setItem(
'askForBackup',
JSON.stringify({
timestamp: Date.now() + 86400000 * 3
})
);
2021-06-15 18:43:05 +05:00
}
2021-07-28 11:37:40 +05:00
}
2021-06-15 18:43:05 +05:00
function checkOrientation() {
Orientation.getOrientation((e, r) => {
DDS.checkSmallTab(r);
2021-06-26 10:01:33 +05:00
useSettingStore
.getState()
.setDimensions({width: DDS.width, height: DDS.height});
useSettingStore
.getState()
.setDeviceMode(
DDS.isLargeTablet()
? 'tablet'
: DDS.isSmallTab
? 'smallTablet'
2021-07-20 09:53:04 +05:00
: 'mobile'
2021-06-26 10:01:33 +05:00
);
2021-06-15 18:43:05 +05:00
});
}
const loadMainApp = () => {
if (databaseHasLoaded) {
SettingsService.setAppLoaded();
eSendEvent('load_overlay');
initialize();
}
};
2021-08-06 09:16:52 +05:00
checkOrientation();
2019-11-15 01:17:59 +05:00
const App = () => {
const [, dispatch] = useTracked();
2021-06-11 09:16:04 +05:00
const setVerifyUser = useUserStore(state => state.setVerifyUser);
2021-10-02 10:09:12 +05:00
const appEvents = useAppEvents();
2021-06-08 12:26:55 +05:00
2021-06-11 09:16:04 +05:00
useEffect(() => {
2021-06-15 18:43:05 +05:00
databaseHasLoaded = false;
2021-02-10 15:58:08 +05:00
(async () => {
try {
2021-02-12 16:26:11 +05:00
await SettingsService.init();
2021-04-06 12:17:41 +05:00
if (
SettingsService.get().appLockMode &&
SettingsService.get().appLockMode !== 'none'
) {
2021-06-11 09:16:04 +05:00
setVerifyUser(true);
}
2021-06-26 08:47:52 +05:00
await loadDatabase();
useUserStore.getState().setUser(await db.user.getUser());
2021-06-26 10:01:33 +05:00
if (SettingsService.get().telemetry) {
2022-01-20 00:13:33 +05:00
Tracker.record('3c6890ce-8410-49d5-8831-15fb2eb28a21');
2021-06-26 10:01:33 +05:00
}
} catch (e) {
} finally {
2021-06-15 18:43:05 +05:00
databaseHasLoaded = true;
2021-02-15 11:06:40 +05:00
loadMainApp();
2021-02-10 15:58:08 +05:00
}
})();
2021-01-11 14:29:50 +05:00
}, []);
2020-12-20 23:01:35 +05:00
2021-04-19 12:13:42 +05:00
const _dispatch = data => {
2021-02-09 16:34:18 +05:00
dispatch(data);
};
2020-12-20 23:01:35 +05:00
useEffect(() => {
2021-02-09 16:34:18 +05:00
eSubscribeEvent(eDispatchAction, _dispatch);
2020-12-20 23:01:35 +05:00
return () => {
2021-02-09 16:34:18 +05:00
eUnSubscribeEvent(eDispatchAction, _dispatch);
2020-12-20 23:01:35 +05:00
};
}, []);
2020-10-24 13:47:31 +05:00
return (
2020-11-23 11:30:55 +05:00
<SafeAreaProvider>
<RootView />
2021-02-25 12:16:38 +05:00
<AppLoader onLoad={loadMainApp} />
2020-11-23 11:30:55 +05:00
</SafeAreaProvider>
2020-10-24 13:47:31 +05:00
);
2019-11-15 01:17:59 +05:00
};
export default App;