Files
notesnook/apps/mobile/App.js

185 lines
5.1 KiB
JavaScript
Raw Normal View History

2020-11-16 12:36:41 +05:00
import * as NetInfo from '@react-native-community/netinfo';
2020-11-16 13:02:02 +05:00
import {EV} from 'notes-core/common';
import React, {useEffect, useState} from 'react';
2020-11-20 16:43:40 +05:00
import {Appearance, AppState, Platform, StatusBar} from 'react-native';
2020-11-16 13:02:02 +05:00
import {enabled} from 'react-native-privacy-snapshot';
import {SafeAreaProvider} from 'react-native-safe-area-context';
import {useTracked} from './src/provider';
import {Actions} from './src/provider/Actions';
2020-10-24 13:47:31 +05:00
import {
eSendEvent,
eSubscribeEvent,
eUnSubscribeEvent,
2020-11-16 13:02:02 +05:00
ToastEvent,
2020-10-24 13:47:31 +05:00
} from './src/services/EventManager';
2020-11-16 13:02:02 +05:00
import IntentService from './src/services/IntentService';
import {setLoginMessage} from './src/services/Message';
2020-11-23 11:30:55 +05:00
import Navigation from './src/services/Navigation';
import {editing} from './src/utils';
2020-11-16 13:02:02 +05:00
import {COLOR_SCHEME} from './src/utils/Colors';
import {db} from './src/utils/DB';
2020-11-23 11:30:55 +05:00
import {eDispatchAction, eOnLoadNote, eStartSyncer} from './src/utils/Events';
2020-11-16 13:02:02 +05:00
import {MMKV} from './src/utils/mmkv';
import {tabBarRef} from './src/utils/Refs';
2020-11-23 11:30:55 +05:00
import {getNote, setIntentNote} from './src/views/Editor/Functions';
2020-11-17 16:18:43 +05:00
2020-11-23 11:30:55 +05:00
let AppRootView = require('./initializer.intent').IntentView;
let SettingsService = null;
let Sentry = null;
2020-11-07 15:22:13 +05:00
const onAppStateChanged = async (state) => {
2020-11-23 11:30:55 +05:00
console.log('app state', state);
if (state === 'active') {
StatusBar.setBarStyle(
2020-11-20 01:11:58 +05:00
COLOR_SCHEME.night ? 'light-content' : 'dark-content',
true,
);
if (Platform.OS === 'android') {
2020-11-20 01:11:58 +05:00
StatusBar.setBackgroundColor('transparent', true);
StatusBar.setTranslucent(true, true);
}
if (SettingsService.get().privacyScreen) {
enabled(false);
}
2020-11-23 11:30:55 +05:00
MMKV.removeItem('appState');
} else {
2020-11-23 11:30:55 +05:00
if (editing.currentlyEditing) {
MMKV.setItem(
'appState',
JSON.stringify({
editing: editing.currentlyEditing,
note: getNote(),
}),
);
}
if (SettingsService.get().privacyScreen) {
enabled(true);
}
2020-11-16 12:18:59 +05:00
}
2020-11-16 12:13:02 +05:00
};
2020-11-16 12:36:41 +05:00
const onNetworkStateChanged = (netInfo) => {
let message = 'Internet connection restored';
let type = 'success';
if (!netInfo.isConnected || !netInfo.isInternetReachable) {
message = 'No internet connection';
type = 'error';
}
db.user?.get().then((user) => {
if (user) {
ToastEvent.show(message, type);
}
});
};
2019-11-15 01:17:59 +05:00
const App = () => {
2020-10-24 13:47:31 +05:00
const [, dispatch] = useTracked(),
2020-11-23 11:30:55 +05:00
[init, setInit] = useState(false),
[intent, setIntent] = useState(false);
2020-11-16 12:36:41 +05:00
2020-10-24 13:47:31 +05:00
const syncChanges = async () => {
2020-11-23 11:30:55 +05:00
dispatch({type: Actions.ALL});
};
const startSyncer = async () => {
try {
let user = await db.user.get();
if (user) {
EV.subscribe('db:refresh', syncChanges);
2020-10-24 13:47:31 +05:00
}
2020-11-23 11:30:55 +05:00
} catch (e) {
console.log(e);
}
};
2020-10-12 11:24:56 +05:00
2020-11-16 12:36:41 +05:00
const onSystemThemeChanged = async () => {
2020-11-16 13:02:02 +05:00
await SettingsService.setTheme();
2020-11-16 12:36:41 +05:00
};
2020-10-24 13:47:31 +05:00
useEffect(() => {
eSubscribeEvent(eStartSyncer, startSyncer);
eSubscribeEvent(eDispatchAction, (type) => {
dispatch(type);
});
AppState.addEventListener('change', onAppStateChanged);
2020-11-23 11:30:55 +05:00
eSubscribeEvent('nointent', loadMainApp);
2020-11-16 12:36:41 +05:00
Appearance.addChangeListener(onSystemThemeChanged);
let unsub = NetInfo.addEventListener(onNetworkStateChanged);
2020-10-24 13:47:31 +05:00
return () => {
2020-11-16 09:43:53 +05:00
EV.unsubscribe('db:refresh', syncChanges);
2020-10-24 13:47:31 +05:00
eUnSubscribeEvent(eStartSyncer, startSyncer);
eUnSubscribeEvent(eDispatchAction, (type) => {
dispatch(type);
});
2020-11-23 11:30:55 +05:00
eUnSubscribeEvent('nointent', loadMainApp);
AppState.removeEventListener('change', onAppStateChanged);
2020-11-16 12:36:41 +05:00
Appearance.removeChangeListener(onSystemThemeChanged);
unsub();
2020-10-24 13:47:31 +05:00
};
}, []);
2020-11-23 11:30:55 +05:00
const loadMainApp = () => {
setIntent(false);
AppRootView = require('./initializer.root').RootView;
setInit(true);
backupData().then((r) => r);
Sentry = require('@sentry/react-native');
Sentry.init({
dsn:
'https://317a5c31caf64d1e9b27abf15eb1a554@o477952.ingest.sentry.io/5519681',
});
};
2020-11-15 12:16:22 +05:00
const getUser = async () => {
2020-11-23 11:30:55 +05:00
try {
let user = await db.user.get();
if (user) {
dispatch({type: Actions.USER, user: user});
dispatch({type: Actions.SYNCING, syncing: true});
await db.sync();
dispatch({type: Actions.SYNCING, syncing: false});
dispatch({type: Actions.ALL});
await startSyncer();
} else {
setLoginMessage(dispatch);
}
} catch (e) {
console.log(e);
2020-11-15 12:16:22 +05:00
}
};
2020-10-24 13:47:31 +05:00
useEffect(() => {
2020-11-23 11:30:55 +05:00
SettingsService = require('./src/services/SettingsService').default;
SettingsService.init().finally(() => {
db.init().finally(runAfterInit);
});
2020-10-24 13:47:31 +05:00
}, []);
2020-11-23 11:30:55 +05:00
const runAfterInit = () => {
setIntent(true);
dispatch({type: Actions.ALL});
getUser().catch((e) => console.log);
};
2020-10-27 23:05:35 +05:00
async function backupData() {
2020-11-23 11:30:55 +05:00
let settings = SettingsService.get();
let Backup = require('./src/services/Backup').default;
2020-10-28 15:15:35 +05:00
if (await Backup.checkBackupRequired(settings.reminder)) {
try {
await Backup.run();
} catch (e) {
console.log(e);
2020-10-27 23:05:35 +05:00
}
2020-10-28 15:15:35 +05:00
}
2020-10-27 23:05:35 +05:00
}
2020-10-24 13:47:31 +05:00
return (
2020-11-23 11:30:55 +05:00
<SafeAreaProvider>
{intent && <AppRootView />}
{init && !intent && <AppRootView />}
</SafeAreaProvider>
2020-10-24 13:47:31 +05:00
);
2019-11-15 01:17:59 +05:00
};
export default App;