2020-09-19 16:01:57 +05:00
|
|
|
import {useNetInfo} from '@react-native-community/netinfo';
|
|
|
|
|
import React, {useEffect, useState} from 'react';
|
2020-10-12 11:24:56 +05:00
|
|
|
import {Dimensions, useColorScheme} from 'react-native';
|
2020-03-15 10:20:34 +05:00
|
|
|
import Orientation from 'react-native-orientation';
|
2020-09-19 16:01:57 +05:00
|
|
|
import {SafeAreaProvider} from 'react-native-safe-area-context';
|
2020-10-12 11:24:56 +05:00
|
|
|
import {
|
|
|
|
|
getColorScheme,
|
|
|
|
|
getDeviceSize,
|
|
|
|
|
scale,
|
|
|
|
|
updateSize,
|
|
|
|
|
} from './src/common/common';
|
2020-09-19 16:01:57 +05:00
|
|
|
import {useTracked} from './src/provider';
|
|
|
|
|
import {ACTIONS} from './src/provider/actions';
|
|
|
|
|
import {defaultState} from './src/provider/defaultState';
|
2020-10-12 11:24:56 +05:00
|
|
|
import {
|
|
|
|
|
eSendEvent,
|
|
|
|
|
eSubscribeEvent,
|
|
|
|
|
eUnSubscribeEvent,
|
|
|
|
|
} from './src/services/eventManager';
|
|
|
|
|
import {
|
|
|
|
|
eDispatchAction,
|
|
|
|
|
eOnLoadNote,
|
|
|
|
|
eResetApp,
|
|
|
|
|
eStartSyncer,
|
|
|
|
|
} from './src/services/events';
|
2020-09-19 16:01:57 +05:00
|
|
|
import {MMKV} from './src/utils/storage';
|
2020-10-12 11:24:56 +05:00
|
|
|
import {db, DDS, sleep, ToastEvent} from './src/utils/utils';
|
|
|
|
|
import DeviceInfo from 'react-native-device-info';
|
|
|
|
|
import {getNote} from './src/views/Editor/func';
|
|
|
|
|
import {openEditorAnimation} from './src/utils/animations';
|
2020-09-14 09:03:19 +05:00
|
|
|
|
2020-10-12 11:24:56 +05:00
|
|
|
let firstLoad = true;
|
|
|
|
|
let note = null;
|
2019-11-15 01:17:59 +05:00
|
|
|
const App = () => {
|
2020-09-12 14:32:06 +05:00
|
|
|
const [, dispatch] = useTracked();
|
2020-01-17 00:23:16 +05:00
|
|
|
const [init, setInit] = useState(false);
|
2020-06-11 13:02:14 +05:00
|
|
|
const netInfo = useNetInfo();
|
2020-09-07 15:55:24 +05:00
|
|
|
const colorScheme = useColorScheme();
|
2020-10-12 11:24:56 +05:00
|
|
|
let I =
|
|
|
|
|
DDS.isTab && !DDS.isSmallTab
|
|
|
|
|
? require('./index.tablet')
|
|
|
|
|
: require('./index.mobile');
|
|
|
|
|
|
2020-09-12 14:32:06 +05:00
|
|
|
const _onOrientationChange = (o) => {
|
2020-10-12 11:24:56 +05:00
|
|
|
console.log(o, 'orientation');
|
|
|
|
|
let smallTab = DDS.isSmallTab;
|
|
|
|
|
DDS.setNewValues();
|
|
|
|
|
DDS.checkSmallTab(o);
|
|
|
|
|
if (smallTab === DDS.isSmallTab) {
|
|
|
|
|
console.log('nothing changed');
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
I =
|
|
|
|
|
DDS.isTab && !DDS.isSmallTab
|
|
|
|
|
? require('./index.tablet')
|
|
|
|
|
: require('./index.mobile');
|
|
|
|
|
|
2020-02-22 20:00:57 +05:00
|
|
|
setTimeout(() => {
|
2020-10-12 11:24:56 +05:00
|
|
|
resetApp();
|
|
|
|
|
}, 1000);
|
2020-09-18 20:53:34 +05:00
|
|
|
};
|
2020-09-13 11:25:59 +05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2020-09-24 09:37:24 +05:00
|
|
|
updateTheme();
|
2020-09-13 11:25:59 +05:00
|
|
|
}, [colorScheme]);
|
|
|
|
|
|
2020-09-24 09:37:24 +05:00
|
|
|
const updateTheme = async () => {
|
2020-09-13 11:25:59 +05:00
|
|
|
let settings;
|
2020-09-14 09:03:19 +05:00
|
|
|
try {
|
|
|
|
|
settings = await MMKV.getStringAsync('settings');
|
2020-09-24 09:37:24 +05:00
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e.message);
|
|
|
|
|
} finally {
|
|
|
|
|
if (!settings) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
settings = JSON.parse(settings);
|
|
|
|
|
if (settings.useSystemTheme) {
|
|
|
|
|
let newColors = await getColorScheme(settings.useSystemTheme);
|
|
|
|
|
dispatch({type: ACTIONS.THEME, colors: newColors});
|
|
|
|
|
}
|
2020-09-14 09:03:19 +05:00
|
|
|
}
|
|
|
|
|
};
|
2020-09-07 15:55:24 +05:00
|
|
|
|
2020-06-11 13:02:14 +05:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (!netInfo.isConnected || !netInfo.isInternetReachable) {
|
2020-09-12 14:32:06 +05:00
|
|
|
db.user?.get().then((user) => {
|
2020-06-11 13:02:14 +05:00
|
|
|
if (user) {
|
2020-08-16 18:59:07 +05:00
|
|
|
ToastEvent.show('No internet connection', 'error');
|
2020-06-11 13:02:14 +05:00
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else {
|
2020-09-12 14:32:06 +05:00
|
|
|
db.user?.get().then((user) => {
|
2020-06-11 13:02:14 +05:00
|
|
|
if (user) {
|
2020-08-16 18:59:07 +05:00
|
|
|
ToastEvent.show('Internet connection restored', 'success');
|
2020-06-11 13:02:14 +05:00
|
|
|
} else {
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, [netInfo]);
|
|
|
|
|
|
2020-05-05 23:04:30 +05:00
|
|
|
const startSyncer = async () => {
|
2020-09-24 09:37:24 +05:00
|
|
|
try {
|
|
|
|
|
let user = await db.user.get();
|
|
|
|
|
if (user) {
|
|
|
|
|
db.ev.subscribe('db:refresh', syncChanges);
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e);
|
2020-05-04 18:09:33 +05:00
|
|
|
}
|
2020-05-06 22:31:35 +05:00
|
|
|
};
|
2020-09-19 16:01:57 +05:00
|
|
|
const syncChanges = async () => {
|
2020-05-09 00:32:10 +05:00
|
|
|
dispatch({type: ACTIONS.ALL});
|
2020-05-14 14:49:09 +05:00
|
|
|
};
|
2020-05-09 00:32:10 +05:00
|
|
|
|
2020-08-16 18:59:07 +05:00
|
|
|
const resetApp = () => {
|
2020-10-12 11:24:56 +05:00
|
|
|
note = getNote();
|
|
|
|
|
console.log(note, 'NOTE BEFORE RELOAD');
|
2020-08-16 18:59:07 +05:00
|
|
|
setInit(false);
|
2020-10-12 11:24:56 +05:00
|
|
|
Initialize().then(async () => {
|
2020-08-16 18:59:07 +05:00
|
|
|
setInit(true);
|
2020-10-12 11:24:56 +05:00
|
|
|
await sleep(300);
|
|
|
|
|
console.log(note, 'NOTE ON RELOAD');
|
|
|
|
|
if (note && note.id) {
|
|
|
|
|
console.log(note);
|
|
|
|
|
eSendEvent(eOnLoadNote, note);
|
|
|
|
|
if (DDS.isPhone || DDS.isSmallTab) {
|
|
|
|
|
openEditorAnimation();
|
|
|
|
|
}
|
|
|
|
|
note = null;
|
|
|
|
|
}
|
2020-08-16 18:59:07 +05:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2020-05-04 18:09:33 +05:00
|
|
|
useEffect(() => {
|
2020-05-06 22:31:35 +05:00
|
|
|
eSubscribeEvent(eStartSyncer, startSyncer);
|
2020-08-16 18:59:07 +05:00
|
|
|
eSubscribeEvent(eResetApp, resetApp);
|
2020-09-24 09:37:24 +05:00
|
|
|
Orientation.addOrientationListener(_onOrientationChange);
|
|
|
|
|
eSubscribeEvent(eDispatchAction, (type) => {
|
|
|
|
|
dispatch(type);
|
|
|
|
|
});
|
2020-05-04 18:09:33 +05:00
|
|
|
return () => {
|
2020-09-24 23:01:52 +05:00
|
|
|
db?.ev?.unsubscribe('db:refresh', syncChanges);
|
2020-05-06 22:31:35 +05:00
|
|
|
eUnSubscribeEvent(eStartSyncer, startSyncer);
|
2020-08-16 18:59:07 +05:00
|
|
|
eUnSubscribeEvent(eResetApp, resetApp);
|
2020-09-24 09:37:24 +05:00
|
|
|
eUnSubscribeEvent(eDispatchAction, (type) => {
|
|
|
|
|
dispatch(type);
|
|
|
|
|
});
|
|
|
|
|
Orientation.removeOrientationListener(_onOrientationChange);
|
2020-05-06 22:31:35 +05:00
|
|
|
};
|
2020-05-14 14:49:09 +05:00
|
|
|
}, []);
|
2020-05-04 18:09:33 +05:00
|
|
|
|
2019-12-07 12:05:15 +05:00
|
|
|
useEffect(() => {
|
2020-09-27 10:57:17 +05:00
|
|
|
let error = null;
|
|
|
|
|
let user;
|
2020-10-12 11:24:56 +05:00
|
|
|
|
2020-09-27 10:57:17 +05:00
|
|
|
Initialize().finally(async () => {
|
|
|
|
|
try {
|
|
|
|
|
await db.init();
|
|
|
|
|
user = await db.user.get();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
error = e;
|
2020-10-12 11:24:56 +05:00
|
|
|
console.log(e,"ERROR IN DB")
|
2020-09-27 10:57:17 +05:00
|
|
|
} finally {
|
|
|
|
|
if (user) {
|
|
|
|
|
dispatch({type: ACTIONS.USER, user: user});
|
|
|
|
|
startSyncer();
|
2020-09-24 09:37:24 +05:00
|
|
|
}
|
2020-09-27 10:57:17 +05:00
|
|
|
dispatch({type: ACTIONS.ALL});
|
2020-10-12 11:24:56 +05:00
|
|
|
|
2020-09-27 10:57:17 +05:00
|
|
|
setInit(true);
|
2020-10-12 11:24:56 +05:00
|
|
|
|
2020-09-27 10:57:17 +05:00
|
|
|
if (error) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
ToastEvent.show(error.message);
|
|
|
|
|
}, 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-01-17 21:26:01 +05:00
|
|
|
}, []);
|
2019-12-21 10:38:40 +05:00
|
|
|
|
2020-03-15 10:20:34 +05:00
|
|
|
async function Initialize(colors = colors) {
|
2020-09-12 14:32:06 +05:00
|
|
|
let settings;
|
2020-09-27 10:57:17 +05:00
|
|
|
scale.fontScale = 1;
|
2020-10-12 11:24:56 +05:00
|
|
|
|
|
|
|
|
if (firstLoad) {
|
|
|
|
|
if (DeviceInfo.isTablet() && getDeviceSize() > 9) {
|
|
|
|
|
Orientation.lockToLandscape();
|
|
|
|
|
_onOrientationChange('LANDSCAPE');
|
|
|
|
|
} else {
|
|
|
|
|
Orientation.lockToPortrait();
|
|
|
|
|
}
|
|
|
|
|
firstLoad = false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-24 13:05:56 +05:00
|
|
|
try {
|
2020-09-12 14:32:06 +05:00
|
|
|
settings = await MMKV.getStringAsync('settings');
|
2020-09-27 10:57:17 +05:00
|
|
|
settings = JSON.parse(settings);
|
|
|
|
|
|
|
|
|
|
if (settings.fontScale) {
|
|
|
|
|
scale.fontScale = settings.fontScale;
|
|
|
|
|
}
|
|
|
|
|
updateSize();
|
2020-09-24 09:37:24 +05:00
|
|
|
} catch (e) {
|
2020-09-24 23:01:52 +05:00
|
|
|
if (!settings || !settings.includes('fontScale')) {
|
|
|
|
|
settings = defaultState.settings;
|
|
|
|
|
await MMKV.setStringAsync('settings', JSON.stringify(settings));
|
|
|
|
|
}
|
2020-09-24 09:37:24 +05:00
|
|
|
} finally {
|
|
|
|
|
let newColors = await getColorScheme(settings.useSystemTheme);
|
|
|
|
|
dispatch({type: ACTIONS.SETTINGS, settings: {...settings}});
|
|
|
|
|
dispatch({type: ACTIONS.THEME, colors: newColors});
|
2020-02-23 11:47:14 +05:00
|
|
|
}
|
2020-01-17 21:04:21 +05:00
|
|
|
}
|
|
|
|
|
|
2020-01-17 00:23:16 +05:00
|
|
|
if (!init) {
|
2020-01-14 20:48:03 +05:00
|
|
|
return <></>;
|
2019-12-11 01:10:04 +05:00
|
|
|
}
|
2019-11-17 16:02:19 +05:00
|
|
|
return (
|
2020-05-14 14:49:09 +05:00
|
|
|
<>
|
|
|
|
|
<SafeAreaProvider>
|
|
|
|
|
<>
|
|
|
|
|
<I.Initialize />
|
|
|
|
|
</>
|
|
|
|
|
</SafeAreaProvider>
|
|
|
|
|
</>
|
2019-11-15 01:17:59 +05:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default App;
|