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-24 16:59:12 +05:00
|
|
|
import {
|
|
|
|
|
Appearance,
|
|
|
|
|
AppState,
|
|
|
|
|
Linking,
|
|
|
|
|
NativeModules,
|
|
|
|
|
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 {
|
2020-11-24 16:59:12 +05:00
|
|
|
eSendEvent,
|
2020-10-24 13:47:31 +05:00
|
|
|
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 {setLoginMessage} from './src/services/Message';
|
2020-11-23 11:30:55 +05:00
|
|
|
import {editing} from './src/utils';
|
2020-11-24 16:59:12 +05:00
|
|
|
import {ACCENT, COLOR_SCHEME} from './src/utils/Colors';
|
2020-11-16 13:02:02 +05:00
|
|
|
import {db} from './src/utils/DB';
|
2020-11-24 16:59:12 +05:00
|
|
|
import {eDispatchAction, eOnLoadNote, eStartSyncer} from './src/utils/Events';
|
2020-11-16 13:02:02 +05:00
|
|
|
import {MMKV} from './src/utils/mmkv';
|
2020-11-23 17:36:13 +05:00
|
|
|
import {sleep} from './src/utils/TimeUtils';
|
|
|
|
|
import {getNote} from './src/views/Editor/Functions';
|
2020-11-24 16:59:12 +05:00
|
|
|
import IntentService from './src/services/IntentService';
|
|
|
|
|
import {tabBarRef} from './src/utils/Refs';
|
|
|
|
|
import Navigation from './src/services/Navigation';
|
|
|
|
|
import SplashScreen from 'react-native-splash-screen';
|
|
|
|
|
const {ReceiveSharingIntent} = NativeModules;
|
2020-11-17 16:18:43 +05:00
|
|
|
|
2020-11-24 16:59:12 +05:00
|
|
|
let AppRootView = require('./initializer.root').RootView;
|
2020-11-23 11:30:55 +05:00
|
|
|
let SettingsService = null;
|
|
|
|
|
let Sentry = null;
|
2020-11-23 15:57:31 +05:00
|
|
|
let appInit = false;
|
2020-11-24 18:12:35 +05:00
|
|
|
let intentInit = false;
|
2020-11-07 15:22:13 +05:00
|
|
|
|
2020-11-18 18:18:27 +05:00
|
|
|
const onAppStateChanged = async (state) => {
|
2020-11-23 11:30:55 +05:00
|
|
|
console.log('app state', state);
|
2020-11-18 18:18:27 +05:00
|
|
|
if (state === 'active') {
|
|
|
|
|
StatusBar.setBarStyle(
|
2020-11-20 01:11:58 +05:00
|
|
|
COLOR_SCHEME.night ? 'light-content' : 'dark-content',
|
|
|
|
|
true,
|
2020-11-18 18:18:27 +05:00
|
|
|
);
|
|
|
|
|
if (Platform.OS === 'android') {
|
2020-11-20 01:11:58 +05:00
|
|
|
StatusBar.setBackgroundColor('transparent', true);
|
|
|
|
|
StatusBar.setTranslucent(true, true);
|
2020-11-18 18:18:27 +05:00
|
|
|
}
|
|
|
|
|
if (SettingsService.get().privacyScreen) {
|
|
|
|
|
enabled(false);
|
|
|
|
|
}
|
2020-11-23 15:57:31 +05:00
|
|
|
console.log('clearing state', await MMKV.getItem('appState'));
|
|
|
|
|
if (appInit) {
|
|
|
|
|
await MMKV.removeItem('appState');
|
2020-11-24 18:12:35 +05:00
|
|
|
}
|
|
|
|
|
if (intentInit) {
|
2020-11-24 16:59:12 +05:00
|
|
|
try {
|
|
|
|
|
console.log('I am running too from here');
|
|
|
|
|
_data = await ReceiveSharingIntent.getFileNames();
|
|
|
|
|
if (_data) {
|
|
|
|
|
IntentService.setIntent(_data);
|
|
|
|
|
IntentService.check((event) => {
|
|
|
|
|
console.log(event);
|
|
|
|
|
if (event) {
|
|
|
|
|
eSendEvent(eOnLoadNote, event);
|
|
|
|
|
tabBarRef.current?.goToPage(1);
|
|
|
|
|
Navigation.closeDrawer();
|
|
|
|
|
} else {
|
|
|
|
|
eSendEvent('nointent');
|
|
|
|
|
SplashScreen.hide();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
|
|
|
|
}
|
2020-11-23 15:57:31 +05:00
|
|
|
}
|
2020-11-18 18:18:27 +05:00
|
|
|
} else {
|
2020-11-23 17:36:13 +05:00
|
|
|
if (editing.currentlyEditing && appInit) {
|
2020-11-23 15:57:31 +05:00
|
|
|
let state = JSON.stringify({
|
|
|
|
|
editing: editing.currentlyEditing,
|
|
|
|
|
note: getNote(),
|
|
|
|
|
});
|
|
|
|
|
console.log('putting items in state', state);
|
|
|
|
|
await MMKV.setItem('appState', state);
|
2020-11-23 11:30:55 +05:00
|
|
|
}
|
|
|
|
|
|
2020-11-18 18:18:27 +05:00
|
|
|
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-24 16:59:12 +05:00
|
|
|
[init, setInit] = useState(true),
|
2020-11-23 11:30:55 +05:00
|
|
|
[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-11-24 16:59:12 +05:00
|
|
|
const _handleIntent = async (res) => {
|
|
|
|
|
let url = res ? res.url : '';
|
|
|
|
|
try {
|
|
|
|
|
if (res && res.startsWith('ShareMedia://dataUrl')) {
|
|
|
|
|
_data = await ReceiveSharingIntent.getFileNames(url);
|
|
|
|
|
}
|
|
|
|
|
if (_data) {
|
|
|
|
|
IntentService.setIntent(_data);
|
|
|
|
|
IntentService.check((event) => {
|
|
|
|
|
eSendEvent(eOnLoadNote, event);
|
|
|
|
|
tabBarRef.current?.goToPage(1);
|
|
|
|
|
Navigation.closeDrawer();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
};
|
|
|
|
|
|
2020-10-24 13:47:31 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
eSubscribeEvent(eStartSyncer, startSyncer);
|
|
|
|
|
eSubscribeEvent(eDispatchAction, (type) => {
|
|
|
|
|
dispatch(type);
|
|
|
|
|
});
|
2020-11-18 18:18:27 +05:00
|
|
|
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-11-24 16:59:12 +05:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
|
Linking.addEventListener('url', _handleIntent);
|
|
|
|
|
}
|
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);
|
2020-11-18 18:18:27 +05:00
|
|
|
AppState.removeEventListener('change', onAppStateChanged);
|
2020-11-16 12:36:41 +05:00
|
|
|
Appearance.removeChangeListener(onSystemThemeChanged);
|
2020-11-24 16:59:12 +05:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
|
Linking.removeEventListener('url', _handleIntent);
|
|
|
|
|
}
|
2020-11-16 12:36:41 +05:00
|
|
|
unsub();
|
2020-10-24 13:47:31 +05:00
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2020-11-23 11:30:55 +05:00
|
|
|
const loadMainApp = () => {
|
2020-11-24 16:59:12 +05:00
|
|
|
dispatch({type: Actions.ALL});
|
2020-11-23 11:30:55 +05:00
|
|
|
AppRootView = require('./initializer.root').RootView;
|
2020-11-24 16:59:12 +05:00
|
|
|
getUser().then(console.log).catch(console.log);
|
2020-11-23 11:30:55 +05:00
|
|
|
backupData().then((r) => r);
|
2020-11-24 18:12:35 +05:00
|
|
|
sleep(500).then(() => (appInit = true));
|
2020-11-24 16:59:12 +05:00
|
|
|
db.notes.init().then(() => {
|
|
|
|
|
dispatch({type: Actions.NOTES});
|
2020-11-24 18:12:35 +05:00
|
|
|
console.log('setting loading to false');
|
2020-11-24 16:59:12 +05:00
|
|
|
dispatch({type: Actions.LOADING, loading: false});
|
|
|
|
|
});
|
|
|
|
|
|
2020-11-23 17:36:13 +05:00
|
|
|
//Sentry = require('@sentry/react-native');
|
2020-11-24 16:59:12 +05:00
|
|
|
// Sentry.init({
|
|
|
|
|
// dsn:
|
|
|
|
|
// 'https://317a5c31caf64d1e9b27abf15eb1a554@o477952.ingest.sentry.io/5519681',
|
|
|
|
|
// });
|
2020-11-23 11:30:55 +05:00
|
|
|
};
|
|
|
|
|
|
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;
|
2020-11-23 17:36:13 +05:00
|
|
|
SettingsService.init();
|
|
|
|
|
db.init().finally(runAfterInit);
|
2020-10-24 13:47:31 +05:00
|
|
|
}, []);
|
|
|
|
|
|
2020-11-23 11:30:55 +05:00
|
|
|
const runAfterInit = () => {
|
2020-11-24 16:59:12 +05:00
|
|
|
IntentService.getIntent()
|
|
|
|
|
.then(() => {
|
|
|
|
|
AppRootView = require('./initializer.intent').IntentView;
|
|
|
|
|
console.log('found intent');
|
|
|
|
|
setInit(false);
|
2020-11-24 18:12:35 +05:00
|
|
|
intentInit = true;
|
2020-11-24 16:59:12 +05:00
|
|
|
dispatch({type: Actions.ALL});
|
|
|
|
|
setIntent(true);
|
|
|
|
|
ReceiveSharingIntent.clearFileNames();
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => {
|
|
|
|
|
console.log(e, 'no intent recieved');
|
|
|
|
|
ReceiveSharingIntent.clearFileNames();
|
2020-11-24 18:12:35 +05:00
|
|
|
intentInit = true;
|
2020-11-24 16:59:12 +05:00
|
|
|
loadMainApp();
|
|
|
|
|
});
|
2020-11-23 11:30:55 +05:00
|
|
|
};
|
|
|
|
|
|
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>
|
2020-11-23 17:36:13 +05:00
|
|
|
{intent ? <AppRootView /> : null}
|
|
|
|
|
{init && !intent ? <AppRootView /> : null}
|
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;
|