Files
notesnook/apps/mobile/App.js

167 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';
2019-11-23 06:25:22 +05:00
import {
StatusBar,
View,
DeviceEventEmitter,
Platform,
2019-12-14 16:00:16 +05:00
Animated,
2019-11-23 06:25:22 +05:00
} 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';
2019-12-04 19:38:19 +05:00
import SideMenu from 'react-native-side-menu';
2019-12-11 01:10:04 +05:00
import Storage from 'notes-core/api/database';
import StorageInterface from './src/utils/storage';
2019-12-14 19:36:43 +05:00
import {AppProvider} from './src/provider';
2019-12-14 16:00:16 +05:00
import {DeviceDetectionService} from './src/utils/deviceDetection';
2019-12-14 19:26:44 +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);
2019-11-15 01:17:59 +05:00
const App = () => {
2019-12-14 21:01:39 +05:00
// Global State
2019-12-14 19:26:44 +05:00
const {colors} = useAppContext();
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);
2019-12-04 19:38:19 +05:00
const [isOpen, setOpen] = useState(false);
const [disableGestures, setDisableGesture] = useState(false);
2019-12-11 01:10:04 +05:00
const [isIntialized, setIsInitialized] = useState(false);
2019-12-14 21:01:39 +05:00
// Effects
useEffect(() => {
db.init().then(() => {
setIsInitialized(true);
});
}, []);
2019-12-04 11:29:40 +05:00
useEffect(() => {
2019-11-29 11:31:43 +05:00
DeviceEventEmitter.addListener('openSidebar', () => {
2019-12-14 16:00:16 +05:00
DDS.isTab ? setSidebar(w * 0.3) : setOpen(true);
2019-11-29 11:31:43 +05:00
});
DeviceEventEmitter.addListener('closeSidebar', () => {
2019-12-14 16:00:16 +05:00
DDS.isTab ? setSidebar(0) : setOpen(false);
2019-11-29 11:31:43 +05:00
});
2019-12-04 19:38:19 +05:00
DeviceEventEmitter.addListener('disableGesture', () => {
setDisableGesture(true);
});
DeviceEventEmitter.addListener('enableGesture', () => {
setDisableGesture(false);
});
2019-11-29 11:31:43 +05:00
2019-11-23 06:25:22 +05:00
return () => {
2019-11-29 11:31:43 +05:00
DeviceEventEmitter.removeListener('openSidebar', () => {
2019-12-14 16:00:16 +05:00
DDS.isTab ? setSidebar('30%') : setOpen(true);
2019-11-29 11:31:43 +05:00
});
DeviceEventEmitter.removeListener('closeSidebar', () => {
2019-12-14 16:00:16 +05:00
DDS.isTab ? setSidebar('0%') : setOpen(false);
2019-11-29 11:31:43 +05:00
});
2019-12-04 19:38:19 +05:00
DeviceEventEmitter.removeListener('disableGesture', () => {
setDisableGesture(true);
});
DeviceEventEmitter.removeListener('enableGesture', () => {
setDisableGesture(false);
});
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-14 21:01:39 +05:00
// Render
2019-12-11 01:10:04 +05:00
if (!isIntialized) {
return <View />;
}
2019-11-17 16:02:19 +05:00
return (
2019-12-14 16:00:16 +05:00
<AppProvider>
<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
isOpen={isOpen}
disableGestures={disableGestures}
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
animationFunction={(prop, value) =>
Animated.spring(prop, {
toValue: value,
friction: 8,
useNativeDriver: true,
})
}
onChange={args => {
setTimeout(() => {
setOpen(args);
}, 300);
2019-12-04 19:38:19 +05:00
}}
2019-12-14 16:00:16 +05:00
menu={
2019-12-14 21:01:39 +05:00
<Menu hide={false} colors={colors} close={() => setOpen(false)} />
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>
)}
2019-11-30 19:56:40 +05:00
2019-12-14 16:00:16 +05:00
<Toast />
</View>
</AppProvider>
2019-11-15 01:17:59 +05:00
);
};
export default App;