Files
notesnook/apps/mobile/App.js

138 lines
3.5 KiB
JavaScript
Raw Normal View History

2021-04-06 12:17:41 +05:00
import http from 'notes-core/utils/http';
2021-06-11 09:35:08 +05:00
import React, { useEffect } from 'react';
2021-01-11 14:29:50 +05:00
import Orientation from 'react-native-orientation';
2021-06-11 09:35:08 +05:00
import { SafeAreaProvider } from 'react-native-safe-area-context';
2020-12-10 09:54:48 +05:00
import SplashScreen from 'react-native-splash-screen';
2021-06-11 09:35:08 +05:00
import { AppRootEvents } from './AppRootEvents';
import { RootView } from './initializer.root';
2021-02-25 12:16:38 +05:00
import AppLoader from './src/components/AppLoader';
2021-06-11 09:35:08 +05:00
import { useTracked } from './src/provider';
2021-06-11 09:16:04 +05:00
import {
initialize,
useMessageStore,
useNoteStore,
useSettingStore,
2021-06-11 09:35:08 +05:00
useUserStore
2021-06-11 09:16:04 +05:00
} from './src/provider/stores';
2021-06-11 09:35:08 +05:00
import { DDS } from './src/services/DeviceDetection';
2021-01-17 12:53:04 +05:00
import {
eSendEvent,
eSubscribeEvent,
2021-06-11 09:35:08 +05:00
eUnSubscribeEvent
2021-01-17 12:53:04 +05:00
} from './src/services/EventManager';
2020-12-20 23:03:02 +05:00
import SettingsService from './src/services/SettingsService';
2021-06-11 09:35:08 +05:00
import { db } from './src/utils/DB';
2021-06-15 18:26:46 +05:00
import { eDispatchAction } from './src/utils/Events';
2021-06-11 09:35:08 +05:00
import { MMKV } from './src/utils/mmkv';
2021-01-08 12:30:05 +05:00
import EditorRoot from './src/views/Editor/EditorRoot';
2020-11-16 12:36:41 +05:00
2021-06-15 18:43:05 +05:00
let databaseHasLoaded = false;
async function loadDefaultNotes() {
try {
const isCreated = await MMKV.getItem('defaultNoteCreated');
if (isCreated) return;
const notes = await http.get(
'https://app.notesnook.com/notes/index.json',
);
if (!notes) return;
for (let note of notes) {
const content = await http.get(note.mobileContent);
await db.notes.add({
title: note.title,
headline: note.headline,
localOnly: true,
content: {type: 'tiny', data: content},
});
}
await MMKV.setItem('defaultNoteCreated', 'yes');
useNoteStore.getState().setNotes();
} catch (e) {}
}
const loadDatabase = async () => {
SplashScreen.hide();
await db.init();
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
loadDefaultNotes();
if (!requireIntro) {
await MMKV.setItem(
'askForRating',
JSON.stringify({
timestamp: Date.now() + 86400000 * 2,
}),
);
}
};
function checkOrientation() {
Orientation.getOrientation((e, r) => {
DDS.checkSmallTab(r);
useSettingStore.getState().setDimensions({width:DDS.width,height:DDS.height});
useSettingStore.getState().setDeviceMode(
DDS.isLargeTablet()
? 'tablet'
: DDS.isSmallTab
? 'smallTablet'
: 'mobile',
);
});
}
const loadMainApp = () => {
if (databaseHasLoaded) {
SettingsService.setAppLoaded();
eSendEvent('load_overlay');
initialize();
}
};
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-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-06-08 12:26:55 +05:00
useMessageStore.getState().setAnnouncement();
2021-02-10 15:58:08 +05:00
(async () => {
try {
2021-06-15 18:43:05 +05:00
checkOrientation();
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();
2021-06-15 18:43:05 +05:00
} catch (e) {} finally {
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-01-08 12:30:05 +05:00
<EditorRoot />
2021-01-11 14:29:50 +05:00
<AppRootEvents />
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;