Files
notesnook/apps/mobile/App.js

175 lines
4.6 KiB
JavaScript
Raw Normal View History

2019-12-14 19:36:43 +05:00
import React, {useState, useEffect} from 'react';
2019-11-17 16:02:19 +05:00
import NavigationService, {
AppContainer,
} from './src/services/NavigationService';
2020-01-17 21:04:21 +05:00
import {StatusBar, View, Platform} from 'react-native';
2019-11-29 11:31:43 +05:00
import * as Animatable from 'react-native-animatable';
2019-12-04 19:38:19 +05:00
import {h, w} from './src/utils/utils';
2019-12-04 11:29:40 +05:00
import {Toast} from './src/components/Toast';
2019-12-04 11:40:59 +05:00
import {Menu} from './src/components/Menu';
2020-01-14 12:29:02 +05:00
import SideMenu from './src/components/SideMenu';
2019-12-11 01:10:04 +05:00
import Storage from 'notes-core/api/database';
import StorageInterface from './src/utils/storage';
2020-01-17 21:04:21 +05:00
import {useTracked, ACTIONS} from './src/provider';
2019-12-14 16:00:16 +05:00
import {DeviceDetectionService} from './src/utils/deviceDetection';
2020-01-17 21:04:21 +05:00
import {
DialogManager,
_recieveEvent,
_unSubscribeEvent,
} from './src/components/DialogManager';
import {getColorScheme} from './src/common/common';
2020-01-18 00:45:37 +05:00
import {useAppContext} from './src/provider/useAppContext';
2019-12-14 21:52:14 +05:00
2019-12-14 16:00:16 +05:00
export const DDS = new DeviceDetectionService();
2019-12-11 01:10:04 +05:00
export const db = new Storage(StorageInterface);
2020-01-14 20:48:03 +05:00
2020-01-14 12:29:02 +05:00
let sideMenuRef;
2019-11-15 01:17:59 +05:00
const App = () => {
2020-01-17 00:23:16 +05:00
const [state, dispatch] = useTracked();
2020-01-18 00:45:37 +05:00
const [colors] = state;
2019-12-14 21:01:39 +05:00
// Local State
2019-12-14 16:00:16 +05:00
const [sidebar, setSidebar] = useState(w * 0.3);
2020-01-17 00:23:16 +05:00
const [init, setInit] = useState(false);
2019-12-11 01:10:04 +05:00
2019-12-14 21:01:39 +05:00
// Effects
2020-01-17 21:04:21 +05:00
const openSidebar = () => {
DDS.isTab ? setSidebar(w * 0.3) : sideMenuRef.openMenu(true);
};
const closeSidebar = () => {
DDS.isTab ? setSidebar(0) : sideMenuRef.openMenu(false);
};
const disableGestures = () => {
sideMenuRef.setGestureEnabled(false);
};
const enableGestures = () => {
sideMenuRef.setGestureEnabled(true);
};
2019-12-04 11:29:40 +05:00
useEffect(() => {
2020-01-17 21:04:21 +05:00
_recieveEvent('openSidebar', openSidebar);
_recieveEvent('closeSidebar', closeSidebar);
2020-01-14 12:29:02 +05:00
2020-01-17 21:04:21 +05:00
_recieveEvent('disableGesture', disableGestures);
_recieveEvent('enableGesture', enableGestures);
2019-11-29 11:31:43 +05:00
2019-11-23 06:25:22 +05:00
return () => {
2020-01-17 21:04:21 +05:00
_unSubscribeEvent('openSidebar', openSidebar);
_unSubscribeEvent('closeSidebar', closeSidebar);
_unSubscribeEvent('disableGesture', disableGestures);
_unSubscribeEvent('enableGesture', enableGestures);
};
}, []);
useEffect(() => {
_recieveEvent('updateEvent', type => {
dispatch(type);
});
return () => {
_unSubscribeEvent('updateEvent', type => {
dispatch(type);
2019-12-04 19:38:19 +05:00
});
2019-11-23 06:25:22 +05:00
};
2019-11-29 11:31:43 +05:00
}, []);
2019-12-07 12:05:15 +05:00
useEffect(() => {
if (Platform.OS === 'android') {
StatusBar.setBackgroundColor('transparent');
StatusBar.setTranslucent(true);
StatusBar.setBarStyle(colors.night ? 'light-content' : 'dark-content');
}
}, []);
2019-12-21 10:38:40 +05:00
useEffect(() => {
2020-01-17 21:04:21 +05:00
updateAppTheme().then(() => {
db.init().then(() => {
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-01-17 21:04:21 +05:00
async function updateAppTheme(colors = state.colors) {
let newColors = await getColorScheme(colors);
dispatch({type: ACTIONS.THEME, colors: newColors});
}
2019-12-14 21:01:39 +05:00
// Render
2020-01-17 21:26:01 +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-01-17 21:04:21 +05:00
<>
2019-12-14 16:00:16 +05:00
<View
style={{
width: '100%',
height: '100%',
flexDirection: 'row',
backgroundColor: colors.bg,
}}>
{DDS.isTab ? (
<>
<Animatable.View
transition="width"
duration={200}
style={{
width: sidebar,
}}>
<Menu
2019-12-14 21:01:39 +05:00
hide={false}
2019-12-14 16:00:16 +05:00
colors={colors}
close={() => {
//setSidebar('0%');
}}
/>
</Animatable.View>
<AppContainer
style={{
width: DDS.isTab ? '70%' : '100%',
height: '100%',
}}
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
2019-12-04 19:38:19 +05:00
}}
/>
2019-12-14 16:00:16 +05:00
</>
) : (
<SideMenu
2020-01-14 12:29:02 +05:00
ref={ref => (sideMenuRef = ref)}
2019-12-14 16:00:16 +05:00
bounceBackOnOverdraw={false}
contentContainerStyle={{
opacity: 0,
2019-12-14 19:26:44 +05:00
backgroundColor: colors.bg,
2019-12-04 19:38:19 +05:00
}}
2019-12-14 16:00:16 +05:00
menu={
2020-01-14 12:29:02 +05:00
<Menu
hide={false}
colors={colors}
close={() => sideMenuRef.openMenu(!sideMenuRef.isOpen)}
/>
2019-12-14 16:00:16 +05:00
}
openMenuOffset={w / 1.3}>
<AppContainer
style={{
width: DDS.isTab ? '70%' : '100%',
height: '100%',
2019-12-14 19:26:44 +05:00
backgroundColor: colors.bg,
2019-12-14 16:00:16 +05:00
}}
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
</SideMenu>
)}
<Toast />
2020-01-17 21:04:21 +05:00
<DialogManager colors={colors} />
2019-12-14 16:00:16 +05:00
</View>
2020-01-17 21:04:21 +05:00
</>
2019-11-15 01:17:59 +05:00
);
};
export default App;