Files
notesnook/apps/mobile/App.js

236 lines
6.4 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';
import {Appearance, AppState, Platform, StatusBar} from 'react-native';
2020-11-15 12:16:22 +05:00
import DeviceInfo from 'react-native-device-info';
2020-03-15 10:20:34 +05:00
import Orientation from 'react-native-orientation';
2020-11-16 13:02:02 +05:00
import {enabled} from 'react-native-privacy-snapshot';
2020-11-15 12:16:22 +05:00
import ReceiveSharingIntent from 'react-native-receive-sharing-intent';
2020-11-16 13:02:02 +05:00
import {SafeAreaProvider} from 'react-native-safe-area-context';
2020-11-15 12:16:22 +05:00
import SplashScreen from 'react-native-splash-screen';
2020-11-16 13:02:02 +05:00
import {useTracked} from './src/provider';
import {Actions} from './src/provider/Actions';
import {defaultState} from './src/provider/DefaultState';
2020-11-15 12:16:22 +05:00
import Backup from './src/services/Backup';
2020-11-16 13:02:02 +05:00
import {DDS} from './src/services/DeviceDetection';
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';
import SettingsService from './src/services/SettingsService';
import {AndroidModule, sortSettings} from './src/utils';
import {COLOR_SCHEME} from './src/utils/Colors';
import {getColorScheme} from './src/utils/ColorUtils';
import {db} from './src/utils/DB';
2020-10-24 13:47:31 +05:00
import {
eDispatchAction,
eOnLoadNote,
eResetApp,
2020-11-16 13:02:02 +05:00
eStartSyncer,
2020-10-24 13:47:31 +05:00
} from './src/utils/Events';
2020-11-16 13:02:02 +05:00
import {MMKV} from './src/utils/mmkv';
import {tabBarRef} from './src/utils/Refs';
import {getDeviceSize, scale, updateSize} from './src/utils/SizeUtils';
import {sleep} from './src/utils/TimeUtils';
import {getNote, setIntent} from './src/views/Editor/Functions';
2020-10-12 11:24:56 +05:00
let firstLoad = true;
let note = null;
2020-11-07 15:22:13 +05:00
let prevIntent = {
text: null,
weblink: null,
};
2020-11-16 12:13:02 +05:00
const onAppFocused = async () => {
2020-11-15 12:16:22 +05:00
StatusBar.setBarStyle(COLOR_SCHEME.night ? 'light-content' : 'dark-content');
if (Platform.OS === 'android') {
StatusBar.setTranslucent(true);
StatusBar.setBackgroundColor(COLOR_SCHEME.bg);
}
2020-11-16 13:02:02 +05:00
if (SettingsService.get().privacyScreen) {
2020-11-16 12:18:59 +05:00
enabled(false);
}
2020-11-15 12:16:22 +05:00
};
2020-11-16 12:13:02 +05:00
const onAppBlur = async () => {
2020-11-16 13:02:02 +05:00
if (SettingsService.get().privacyScreen) {
2020-11-16 12:18:59 +05:00
enabled(true);
}
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-16 12:36:41 +05:00
[init, setInit] = useState(false);
2020-10-24 13:47:31 +05:00
let I =
DDS.isTab && !DDS.isSmallTab
? require('./index.tablet')
: require('./index.mobile');
const _onOrientationChange = (o) => {
let smallTab = DDS.isSmallTab;
DDS.setNewValues();
DDS.checkSmallTab(o);
if (smallTab === DDS.isSmallTab) {
return;
}
I =
DDS.isTab && !DDS.isSmallTab
? require('./index.tablet')
: require('./index.mobile');
setTimeout(() => {
resetApp();
}, 1000);
};
const syncChanges = async () => {
dispatch({type: Actions.ALL});
},
2020-11-15 12:16:22 +05:00
resetApp = async () => {
2020-10-24 13:47:31 +05:00
note = getNote();
setInit(false);
2020-11-15 12:16:22 +05:00
Initialize();
setInit(true);
await sleep(300);
if (note && note.id) {
eSendEvent(eOnLoadNote, note);
if (DDS.isPhone || DDS.isSmallTab) {
tabBarRef.current?.goToPage(1);
2020-10-12 11:24:56 +05:00
}
2020-11-15 12:16:22 +05:00
note = null;
}
2020-10-24 13:47:31 +05:00
},
startSyncer = async () => {
try {
let user = await db.user.get();
if (user) {
2020-11-16 09:43:53 +05:00
EV.subscribe('db:refresh', syncChanges);
2020-10-24 13:47:31 +05:00
}
} catch (e) {
console.log(e);
}
2020-05-06 22:31:35 +05:00
};
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(eResetApp, resetApp);
Orientation.addOrientationListener(_onOrientationChange);
eSubscribeEvent(eDispatchAction, (type) => {
dispatch(type);
});
2020-11-15 12:16:22 +05:00
AppState.addEventListener('focus', onAppFocused);
2020-11-16 12:13:02 +05:00
AppState.addEventListener('blur', onAppBlur);
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(eResetApp, resetApp);
eUnSubscribeEvent(eDispatchAction, (type) => {
dispatch(type);
});
Orientation.removeOrientationListener(_onOrientationChange);
2020-11-15 12:16:22 +05:00
AppState.removeEventListener('focus', onAppFocused);
2020-11-16 12:13:02 +05:00
AppState.removeEventListener('blur', onAppBlur);
2020-11-16 12:36:41 +05:00
Appearance.removeChangeListener(onSystemThemeChanged);
unsub();
2020-10-24 13:47:31 +05:00
};
}, []);
2020-11-15 12:16:22 +05:00
const getUser = async () => {
let user = await db.user.get();
if (user) {
dispatch({type: Actions.USER, user: user});
2020-11-16 09:43:53 +05:00
dispatch({type: Actions.SYNCING, syncing: true});
await db.sync();
dispatch({type: Actions.SYNCING, syncing: false});
dispatch({type: Actions.ALL});
2020-11-15 12:16:22 +05:00
await startSyncer();
} else {
setLoginMessage(dispatch);
}
};
2020-10-24 13:47:31 +05:00
useEffect(() => {
2020-11-15 12:16:22 +05:00
Initialize();
2020-10-24 13:47:31 +05:00
let error = null;
2020-11-15 12:16:22 +05:00
(async () => {
2020-10-24 13:47:31 +05:00
try {
await db.init();
2020-11-01 10:57:55 +05:00
} catch (e) {
error = e;
} finally {
2020-11-15 12:16:22 +05:00
dispatch({type: Actions.ALL});
getUser().catch((e) => console.log);
backupData().then((r) => r);
2020-10-28 15:15:35 +05:00
}
setInit(true);
2020-11-01 10:57:55 +05:00
setTimeout(() => {
if (error) {
2020-11-01 11:15:42 +05:00
console.log(error);
ToastEvent.show('Error initializing database.');
2020-11-01 10:57:55 +05:00
}
2020-11-16 13:02:02 +05:00
IntentService.check();
2020-11-15 12:16:22 +05:00
SplashScreen.hide();
}, 100);
})();
2020-10-24 13:47:31 +05:00
}, []);
2020-10-27 23:05:35 +05:00
async function backupData() {
2020-10-28 15:15:35 +05:00
await sleep(1000);
2020-11-03 12:44:31 +05:00
let settings = await MMKV.getStringAsync('settings');
2020-10-28 15:15:35 +05:00
settings = JSON.parse(settings);
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-11-15 12:16:22 +05:00
function Initialize() {
2020-10-24 13:47:31 +05:00
if (firstLoad) {
if (DeviceInfo.isTablet() && getDeviceSize() > 9) {
Orientation.lockToLandscape();
_onOrientationChange('LANDSCAPE');
} else {
Orientation.lockToPortrait();
}
firstLoad = false;
2020-10-12 11:24:56 +05:00
}
2020-11-16 13:02:02 +05:00
SettingsService.init().then((r) => r);
2020-11-15 12:16:22 +05:00
}
2020-11-03 12:44:31 +05:00
2020-10-24 13:47:31 +05:00
return (
<>
<SafeAreaProvider>
2020-11-15 12:16:22 +05:00
<>{!init ? <></> : <I.Initialize />}</>
2020-10-24 13:47:31 +05:00
</SafeAreaProvider>
</>
);
2019-11-15 01:17:59 +05:00
};
export default App;