Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

minor refactoring

Ammar Ahmeddf799d0daf38
diff --git a/apps/mobile/App.js b/apps/mobile/App.js
index 386351ba8..5ee92908e 100644
--- a/apps/mobile/App.js
+++ b/apps/mobile/App.js
@@ -26,42 +26,77 @@ import { eDispatchAction } from './src/utils/Events';
import { MMKV } from './src/utils/mmkv';
import EditorRoot from './src/views/Editor/EditorRoot';
-let initStatus = false;
+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');
+ 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();
+ }
+};
+
const App = () => {
const [, dispatch] = useTracked();
- const setDeviceMode = useSettingStore(state => state.setDeviceMode);
const setVerifyUser = useUserStore(state => state.setVerifyUser);
useEffect(() => {
+ databaseHasLoaded = false;
useMessageStore.getState().setAnnouncement();
(async () => {
try {
- Orientation.getOrientation((e, r) => {
- DDS.checkSmallTab(r);
- setDeviceMode(
- DDS.isLargeTablet()
- ? 'tablet'
- : DDS.isSmallTab
- ? 'smallTablet'
- : 'mobile',
- );
- });
-
- let func = async () => {
- SplashScreen.hide();
- await db.init();
- let requireIntro = await MMKV.getItem('introCompleted');
- loadDefaultNotes();
- if (!requireIntro) {
- await MMKV.setItem(
- 'askForRating',
- JSON.stringify({
- timestamp: Date.now() + 86400000 * 2,
- }),
- );
- }
- };
-
+ checkOrientation();
await SettingsService.init();
if (
SettingsService.get().appLockMode &&
@@ -69,10 +104,9 @@ const App = () => {
) {
setVerifyUser(true);
}
- await func();
- } catch (e) {
- } finally {
- initStatus = true;
+ await loadDatabase();
+ } catch (e) {} finally {
+ databaseHasLoaded = true;
loadMainApp();
}
})();
@@ -89,41 +123,6 @@ const App = () => {
};
}, []);
- const loadMainApp = () => {
- if (initStatus) {
- SettingsService.setAppLoaded();
- eSendEvent('load_overlay');
- initialize();
- }
- };
-
- async function loadDefaultNotes() {
- try {
- console.log('creating note');
- 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) {
- console.log('getting content');
- const content = await http.get(note.mobileContent);
- await db.notes.add({
- title: note.title,
- headline: note.headline,
- localOnly: true,
- content: {type: 'tiny', data: content},
- });
- }
- console.log('default note created');
- await MMKV.setItem('defaultNoteCreated', 'yes');
- useNoteStore.getState().setNotes();
- } catch (e) {
- console.log(e, 'loading note on welcome');
- }
- }
-
return (
<SafeAreaProvider>
<RootView />
diff --git a/apps/mobile/initializer.root.js b/apps/mobile/initializer.root.js
index f5293a4bf..1bafe3bf4 100644
--- a/apps/mobile/initializer.root.js
+++ b/apps/mobile/initializer.root.js
@@ -3,7 +3,7 @@ import {
deactivateKeepAwake
} from '@sayem314/react-native-keep-awake';
import React, { useEffect, useRef } from 'react';
-import { Dimensions, View } from 'react-native';
+import { View } from 'react-native';
import Animated, { useValue } from 'react-native-reanimated';
import { notesnook } from './e2e/test.ids';
import ContextMenu from './src/components/ContextMenu';
@@ -38,7 +38,6 @@ import tiny from './src/views/Editor/tiny/tiny';
let layoutTimer = null;
const onChangeTab = async obj => {
- console.log(obj.i);
if (obj.i === 1) {
editing.movedAway = false;
activateKeepAwake();
@@ -205,8 +204,6 @@ const NativeStack = React.memo(
},
});
}
-
- console.log('resetting tabs');
setTimeout(() => {
if (current === 'tablet') {
tabBarRef.current?.goToIndex(0);
@@ -238,7 +235,6 @@ const NativeStack = React.memo(
};
const toggleView = show => {
- //console.log('toggling overlay view',show);
animatedTranslateY.setValue(show ? 0 : -9999);
};