2020-09-12 14:32:06 +05:00
|
|
|
import {useNetInfo} from '@react-native-community/netinfo';
|
2020-06-11 13:02:14 +05:00
|
|
|
import React, {useEffect, useState} from 'react';
|
2020-09-13 11:25:59 +05:00
|
|
|
import {Appearance, StatusBar, useColorScheme, AppState} from 'react-native';
|
2020-03-15 10:20:34 +05:00
|
|
|
import Orientation from 'react-native-orientation';
|
2020-06-11 13:02:14 +05:00
|
|
|
import {SafeAreaProvider} from 'react-native-safe-area-context';
|
|
|
|
|
import {getColorScheme, scale, updateSize} from './src/common/common';
|
|
|
|
|
import {useTracked} from './src/provider';
|
|
|
|
|
import {ACTIONS} from './src/provider/actions';
|
|
|
|
|
import {defaultState} from './src/provider/defaultState';
|
|
|
|
|
import {eSubscribeEvent, eUnSubscribeEvent} from './src/services/eventManager';
|
2020-09-12 14:32:06 +05:00
|
|
|
import {eDispatchAction, eResetApp, eStartSyncer} from './src/services/events';
|
|
|
|
|
import {MMKV} from './src/utils/storage';
|
2020-06-11 13:02:14 +05:00
|
|
|
import {db, DDS, ToastEvent} from './src/utils/utils';
|
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-03-23 15:18:16 +05:00
|
|
|
const I = DDS.isTab ? require('./index.tablet') : require('./index.mobile');
|
2020-09-12 14:32:06 +05:00
|
|
|
const _onOrientationChange = (o) => {
|
2020-02-22 20:00:57 +05:00
|
|
|
// Currently orientation is locked on tablet.
|
|
|
|
|
/* DDS.checkOrientation();
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
forceUpdate();
|
|
|
|
|
}, 1000); */
|
|
|
|
|
};
|
2020-06-11 13:02:14 +05:00
|
|
|
|
2020-09-13 11:25:59 +05:00
|
|
|
const _onAppStateChange = () => {
|
|
|
|
|
//changeTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
AppState.addEventListener("focus", _onAppStateChange)
|
|
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
|
AppState.removeEventListener("focus", _onAppStateChange);
|
|
|
|
|
}
|
|
|
|
|
},[])
|
|
|
|
|
|
2020-09-07 15:55:24 +05:00
|
|
|
useEffect(() => {
|
2020-09-13 11:25:59 +05:00
|
|
|
//changeTheme();
|
|
|
|
|
}, [colorScheme]);
|
|
|
|
|
|
|
|
|
|
const changeTheme = async () => {
|
|
|
|
|
let settings;
|
2020-09-12 14:32:06 +05:00
|
|
|
try {
|
|
|
|
|
settings = await MMKV.getStringAsync('settings');
|
|
|
|
|
} catch (e) {}
|
|
|
|
|
if (!settings) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
settings = JSON.parse(settings);
|
|
|
|
|
|
|
|
|
|
if (settings.useSystemTheme) {
|
|
|
|
|
let newColors = await getColorScheme(settings.useSystemTheme);
|
|
|
|
|
StatusBar.setBarStyle(
|
|
|
|
|
Appearance.getColorScheme() === 'dark'
|
|
|
|
|
? 'light-content'
|
|
|
|
|
: 'dark-content',
|
|
|
|
|
);
|
2020-09-13 11:25:59 +05:00
|
|
|
|
2020-09-12 14:32:06 +05:00
|
|
|
dispatch({type: ACTIONS.THEME, colors: newColors});
|
|
|
|
|
}
|
2020-09-13 11:25:59 +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-01-17 21:04:21 +05:00
|
|
|
useEffect(() => {
|
2020-02-22 20:00:57 +05:00
|
|
|
Orientation.addOrientationListener(_onOrientationChange);
|
2020-09-12 14:32:06 +05:00
|
|
|
eSubscribeEvent(eDispatchAction, (type) => {
|
2020-01-17 21:04:21 +05:00
|
|
|
dispatch(type);
|
|
|
|
|
});
|
|
|
|
|
return () => {
|
2020-09-12 14:32:06 +05:00
|
|
|
eUnSubscribeEvent(eDispatchAction, (type) => {
|
2020-01-17 21:04:21 +05:00
|
|
|
dispatch(type);
|
2019-12-04 19:38:19 +05:00
|
|
|
});
|
2020-02-22 20:00:57 +05:00
|
|
|
Orientation.removeOrientationListener(_onOrientationChange);
|
2019-11-23 06:25:22 +05:00
|
|
|
};
|
2019-11-29 11:31:43 +05:00
|
|
|
}, []);
|
|
|
|
|
|
2020-01-25 23:47:17 +05:00
|
|
|
useEffect(() => {
|
2020-02-23 09:40:06 +05:00
|
|
|
DDS.isTab ? Orientation.lockToLandscape() : Orientation.lockToPortrait();
|
2020-01-25 23:47:17 +05:00
|
|
|
}, []);
|
|
|
|
|
|
2020-05-05 23:04:30 +05:00
|
|
|
const startSyncer = async () => {
|
2020-05-04 18:09:33 +05:00
|
|
|
let user = await db.user.get();
|
|
|
|
|
if (user) {
|
2020-05-09 00:32:10 +05:00
|
|
|
db.ev.subscribe('sync', _syncFunc);
|
2020-05-04 18:09:33 +05:00
|
|
|
}
|
2020-05-06 22:31:35 +05:00
|
|
|
};
|
2020-05-04 18:09:33 +05:00
|
|
|
|
2020-05-09 00:32:10 +05:00
|
|
|
const _syncFunc = async () => {
|
|
|
|
|
dispatch({type: ACTIONS.SYNCING, syncing: true});
|
2020-09-12 14:32:06 +05:00
|
|
|
let user = await db.user.get();
|
2020-05-20 16:38:17 +05:00
|
|
|
try {
|
|
|
|
|
await db.sync();
|
2020-06-11 13:02:14 +05:00
|
|
|
} catch (e) {}
|
2020-09-12 14:32:06 +05:00
|
|
|
user = await db.user.get();
|
|
|
|
|
dispatch({type: ACTIONS.USER, user: user});
|
2020-05-09 00:32:10 +05:00
|
|
|
dispatch({type: ACTIONS.ALL});
|
|
|
|
|
dispatch({type: ACTIONS.SYNCING, syncing: false});
|
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 = () => {
|
|
|
|
|
setInit(false);
|
|
|
|
|
Initialize().then(() => {
|
|
|
|
|
setInit(true);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
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-05-04 18:09:33 +05:00
|
|
|
return () => {
|
2020-05-09 00:32:10 +05:00
|
|
|
db.ev.unsubscribe('sync', _syncFunc);
|
2020-05-06 22:31:35 +05:00
|
|
|
eUnSubscribeEvent(eStartSyncer, startSyncer);
|
2020-08-16 18:59:07 +05:00
|
|
|
eUnSubscribeEvent(eResetApp, resetApp);
|
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-03-15 10:20:34 +05:00
|
|
|
Initialize().then(() => {
|
2020-02-12 02:09:41 +05:00
|
|
|
db.init().then(async () => {
|
|
|
|
|
let user = await db.user.get();
|
2020-04-20 09:16:01 +05:00
|
|
|
dispatch({type: ACTIONS.USER, user: user});
|
2020-06-11 13:02:14 +05:00
|
|
|
console.log(user);
|
|
|
|
|
startSyncer();
|
2020-05-08 23:43:38 +05:00
|
|
|
dispatch({type: ACTIONS.ALL});
|
2020-01-17 21:04:21 +05:00
|
|
|
setInit(true);
|
|
|
|
|
});
|
2019-12-21 10:38:40 +05:00
|
|
|
});
|
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-03-24 13:05:56 +05:00
|
|
|
try {
|
2020-09-12 14:32:06 +05:00
|
|
|
settings = await MMKV.getStringAsync('settings');
|
2020-04-20 09:16:01 +05:00
|
|
|
} catch (e) {}
|
2020-09-12 14:32:06 +05:00
|
|
|
if (!settings) {
|
|
|
|
|
settings = defaultState.settings;
|
|
|
|
|
settings = JSON.stringify(s);
|
|
|
|
|
settings.fontScale = 1;
|
2020-03-24 13:05:56 +05:00
|
|
|
|
2020-09-12 14:32:06 +05:00
|
|
|
await MMKV.setStringAsync('settings', settings);
|
2020-03-24 13:05:56 +05:00
|
|
|
|
2020-09-12 14:32:06 +05:00
|
|
|
dispatch({type: ACTIONS.SETTINGS, settings});
|
2020-02-23 11:47:14 +05:00
|
|
|
} else {
|
2020-09-12 14:32:06 +05:00
|
|
|
settings = JSON.parse(settings);
|
|
|
|
|
if (settings.fontScale) {
|
|
|
|
|
scale.fontScale = settings.fontScale;
|
2020-03-17 15:13:17 +05:00
|
|
|
} else {
|
|
|
|
|
scale.fontScale = 1;
|
|
|
|
|
}
|
2020-02-23 11:47:14 +05:00
|
|
|
updateSize();
|
2020-09-12 14:32:06 +05:00
|
|
|
dispatch({type: ACTIONS.SETTINGS, settings: {...settings}});
|
2020-02-23 11:47:14 +05:00
|
|
|
}
|
2020-04-20 10:54:58 +05:00
|
|
|
|
2020-09-13 11:25:59 +05:00
|
|
|
|
2020-09-12 14:32:06 +05:00
|
|
|
let newColors = await getColorScheme(settings.useSystemTheme);
|
2020-09-13 11:25:59 +05:00
|
|
|
|
2020-04-20 09:16:01 +05:00
|
|
|
dispatch({type: ACTIONS.THEME, colors: newColors});
|
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;
|