2020-09-19 16:01:57 +05:00
|
|
|
import {useNetInfo} from '@react-native-community/netinfo';
|
2020-11-16 09:43:53 +05:00
|
|
|
import {EV} from 'notes-core/common';
|
2020-09-19 16:01:57 +05:00
|
|
|
import React, {useEffect, useState} from 'react';
|
2020-11-15 12:16:22 +05:00
|
|
|
import {AppState, Platform, StatusBar, useColorScheme} from 'react-native';
|
|
|
|
|
import DeviceInfo from 'react-native-device-info';
|
2020-03-15 10:20:34 +05:00
|
|
|
import Orientation from 'react-native-orientation';
|
2020-11-15 12:16:22 +05:00
|
|
|
import ReceiveSharingIntent from 'react-native-receive-sharing-intent';
|
2020-09-19 16:01:57 +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-09-19 16:01:57 +05:00
|
|
|
import {useTracked} from './src/provider';
|
2020-10-13 17:02:14 +05:00
|
|
|
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';
|
|
|
|
|
import {DDS} from './src/services/DeviceDetection';
|
2020-10-24 13:47:31 +05:00
|
|
|
import {
|
|
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
|
|
|
|
ToastEvent,
|
|
|
|
|
} from './src/services/EventManager';
|
2020-11-15 12:16:22 +05:00
|
|
|
import {setLoginMessage} from './src/services/Message';
|
|
|
|
|
import {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,
|
|
|
|
|
eStartSyncer,
|
|
|
|
|
} from './src/utils/Events';
|
|
|
|
|
import {MMKV} from './src/utils/mmkv';
|
2020-11-15 11:00:28 +05:00
|
|
|
import {tabBarRef} from './src/utils/Refs';
|
2020-11-15 12:16:22 +05:00
|
|
|
import {getDeviceSize, scale, updateSize} from './src/utils/SizeUtils';
|
|
|
|
|
import {sleep} from './src/utils/TimeUtils';
|
|
|
|
|
import {getNote, setIntent} from './src/views/Editor/Functions';
|
2020-09-14 09:03:19 +05:00
|
|
|
|
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-15 12:16:22 +05:00
|
|
|
const onAppFocused = () => {
|
|
|
|
|
console.log('called');
|
|
|
|
|
StatusBar.setBarStyle(COLOR_SCHEME.night ? 'light-content' : 'dark-content');
|
|
|
|
|
if (Platform.OS === 'android') {
|
|
|
|
|
StatusBar.setTranslucent(true);
|
|
|
|
|
StatusBar.setBackgroundColor(COLOR_SCHEME.bg);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-11-15 01:17:59 +05:00
|
|
|
const App = () => {
|
2020-10-24 13:47:31 +05:00
|
|
|
const [, dispatch] = useTracked(),
|
|
|
|
|
[init, setInit] = useState(false),
|
|
|
|
|
netInfo = useNetInfo(),
|
|
|
|
|
colorScheme = useColorScheme();
|
|
|
|
|
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;
|
|
|
|
|
}
|
2020-10-13 17:02:14 +05:00
|
|
|
|
2020-10-24 13:47:31 +05:00
|
|
|
I =
|
|
|
|
|
DDS.isTab && !DDS.isSmallTab
|
|
|
|
|
? require('./index.tablet')
|
|
|
|
|
: require('./index.mobile');
|
|
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
resetApp();
|
|
|
|
|
}, 1000);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
updateTheme().then((r) => r);
|
|
|
|
|
}, [colorScheme]);
|
|
|
|
|
|
|
|
|
|
const updateTheme = async () => {
|
|
|
|
|
let settings;
|
2020-10-28 15:15:35 +05:00
|
|
|
settings = await MMKV.getStringAsync('settings');
|
|
|
|
|
if (settings) {
|
|
|
|
|
settings = JSON.parse(settings);
|
2020-11-15 11:00:28 +05:00
|
|
|
let newColors = await getColorScheme(settings.useSystemTheme);
|
|
|
|
|
dispatch({type: Actions.THEME, colors: newColors});
|
2020-10-28 15:15:35 +05:00
|
|
|
}
|
2020-10-24 13:47:31 +05:00
|
|
|
};
|
2020-10-13 17:02:14 +05:00
|
|
|
|
2020-10-24 13:47:31 +05:00
|
|
|
useEffect(() => {
|
2020-10-28 15:15:35 +05:00
|
|
|
let message = 'Internet connection restored';
|
|
|
|
|
let type = 'success';
|
2020-10-24 13:47:31 +05:00
|
|
|
if (!netInfo.isConnected || !netInfo.isInternetReachable) {
|
2020-10-28 15:15:35 +05:00
|
|
|
message = 'No internet connection';
|
|
|
|
|
type = 'error';
|
2020-10-24 13:47:31 +05:00
|
|
|
}
|
2020-10-28 15:15:35 +05:00
|
|
|
db.user?.get().then((user) => {
|
|
|
|
|
if (user) {
|
|
|
|
|
ToastEvent.show(message, type);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-10-24 13:47:31 +05:00
|
|
|
}, [netInfo]);
|
|
|
|
|
|
|
|
|
|
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-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-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-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-07 15:22:13 +05:00
|
|
|
checkForIntent();
|
2020-11-15 12:16:22 +05:00
|
|
|
SplashScreen.hide();
|
|
|
|
|
}, 100);
|
|
|
|
|
})();
|
2020-10-24 13:47:31 +05:00
|
|
|
}, []);
|
|
|
|
|
|
2020-11-07 10:55:07 +05:00
|
|
|
const checkForIntent = () => {
|
|
|
|
|
ReceiveSharingIntent.getReceivedFiles(
|
|
|
|
|
(d) => {
|
|
|
|
|
let data = d[0];
|
2020-11-07 11:49:58 +05:00
|
|
|
if (data.text || data.weblink) {
|
|
|
|
|
let text = data.text;
|
|
|
|
|
let weblink = data.weblink;
|
|
|
|
|
let delta = null;
|
|
|
|
|
|
|
|
|
|
if (weblink && text) {
|
2020-11-14 10:11:07 +05:00
|
|
|
delta = [{insert: `${text + ' ' + weblink}`}];
|
2020-11-07 11:49:58 +05:00
|
|
|
text = data.text + ' ' + data.weblink;
|
|
|
|
|
} else if (text && !weblink) {
|
2020-11-14 10:11:07 +05:00
|
|
|
delta = [{insert: `${text}`}];
|
2020-11-07 11:49:58 +05:00
|
|
|
text = data.text;
|
|
|
|
|
} else if (weblink) {
|
2020-11-14 10:11:07 +05:00
|
|
|
delta = [{insert: `${weblink}`}];
|
2020-11-15 11:00:28 +05:00
|
|
|
text = weblink;
|
2020-11-07 11:49:58 +05:00
|
|
|
}
|
2020-11-15 11:00:28 +05:00
|
|
|
|
2020-11-07 15:22:13 +05:00
|
|
|
prevIntent.text = text;
|
|
|
|
|
prevIntent.weblink = weblink;
|
2020-11-14 10:11:07 +05:00
|
|
|
setIntent();
|
2020-11-15 11:00:28 +05:00
|
|
|
console.log(text, weblink, 'HERE SHOWING', delta);
|
2020-11-07 10:55:07 +05:00
|
|
|
eSendEvent(eOnLoadNote, {
|
|
|
|
|
type: 'intent',
|
|
|
|
|
data: delta,
|
2020-11-07 11:49:58 +05:00
|
|
|
text: text,
|
2020-11-07 10:55:07 +05:00
|
|
|
});
|
|
|
|
|
if (DDS.isPhone || DDS.isSmallTab) {
|
2020-11-14 10:11:07 +05:00
|
|
|
tabBarRef.current?.goToPage(1);
|
2020-11-07 10:55:07 +05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ReceiveSharingIntent.clearReceivedFiles();
|
|
|
|
|
},
|
|
|
|
|
(error) => {
|
|
|
|
|
console.log(error, 'INTENT ERROR');
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
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-15 12:16:22 +05:00
|
|
|
initAppSettings().catch((e) => console.log(e));
|
|
|
|
|
}
|
2020-11-03 12:44:31 +05:00
|
|
|
|
2020-11-15 12:16:22 +05:00
|
|
|
const initAppSettings = async () => {
|
|
|
|
|
let settings;
|
|
|
|
|
scale.fontScale = 1;
|
|
|
|
|
settings = await MMKV.getStringAsync('settings');
|
2020-10-28 15:15:35 +05:00
|
|
|
if (!settings) {
|
|
|
|
|
settings = defaultState.settings;
|
|
|
|
|
await MMKV.setStringAsync('settings', JSON.stringify(settings));
|
|
|
|
|
} else {
|
|
|
|
|
settings = JSON.parse(settings);
|
|
|
|
|
}
|
|
|
|
|
if (settings.fontScale) {
|
|
|
|
|
scale.fontScale = settings.fontScale;
|
|
|
|
|
}
|
2020-11-03 16:42:42 +05:00
|
|
|
sortSettings.sort = settings.sort;
|
|
|
|
|
sortSettings.sortOrder = settings.sortOrder;
|
2020-10-28 15:15:35 +05:00
|
|
|
dispatch({type: Actions.SETTINGS, settings: {...settings}});
|
|
|
|
|
updateSize();
|
|
|
|
|
await updateTheme();
|
2020-11-15 12:16:22 +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;
|