Files
notesnook/apps/mobile/App.js

335 lines
9.0 KiB
JavaScript
Raw Normal View History

2019-12-11 01:10:04 +05:00
import Storage from 'notes-core/api/database';
2020-01-24 23:34:33 +05:00
import React, {useEffect, useState} from 'react';
2020-01-27 00:25:19 +05:00
import {Platform, StatusBar, View, Text} from 'react-native';
2020-01-24 23:34:33 +05:00
import * as Animatable from 'react-native-animatable';
2020-01-27 00:25:19 +05:00
import {getColorScheme, WEIGHT, SIZE} from './src/common/common';
2020-01-25 23:24:01 +05:00
import {DialogManager} from './src/components/DialogManager';
2020-01-24 23:34:33 +05:00
import {Menu} from './src/components/Menu';
2020-01-22 02:49:52 +05:00
import {ModalMenu} from './src/components/ModalMenu';
2020-01-24 23:34:33 +05:00
import SideMenu from './src/components/SideMenu';
import {Toast} from './src/components/Toast';
import {useTracked} from './src/provider';
import {ACTIONS} from './src/provider/actions';
2020-01-25 19:42:11 +05:00
import {eSubscribeEvent, eUnSubscribeEvent} from './src/services/eventManager';
2020-01-25 20:12:47 +05:00
import {
2020-01-25 23:24:01 +05:00
eCloseFullscreenEditor,
2020-01-25 20:12:47 +05:00
eCloseSideMenu,
eDisableGestures,
2020-01-25 23:24:01 +05:00
eDispatchAction,
2020-01-25 20:12:47 +05:00
eEnableGestures,
eOpenFullscreenEditor,
2020-01-25 23:24:01 +05:00
eOpenSideMenu,
2020-01-25 20:12:47 +05:00
} from './src/services/events';
2020-01-25 23:24:01 +05:00
import NavigationService, {
AppContainer,
} from './src/services/NavigationService';
import {DeviceDetectionService} from './src/utils/deviceDetection';
import StorageInterface from './src/utils/storage';
import {w} from './src/utils/utils';
import Editor from './src/views/Editor';
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;
2020-01-22 02:49:52 +05:00
let editorRef;
2020-01-27 00:57:37 +05:00
let outColors;
2019-11-15 01:17:59 +05:00
const App = () => {
2020-01-17 00:23:16 +05:00
const [state, dispatch] = useTracked();
2020-01-27 00:25:19 +05:00
const {colors, loading} = state;
2020-01-22 02:49:52 +05:00
2020-01-17 00:23:16 +05:00
const [init, setInit] = useState(false);
2020-01-22 02:49:52 +05:00
const [fullscreen, setFullscreen] = useState(false);
2019-12-11 01:10:04 +05:00
2020-01-17 21:04:21 +05:00
const openSidebar = () => {
2020-01-24 22:52:34 +05:00
DDS.isTab ? null : sideMenuRef.openMenu(true);
2020-01-17 21:04:21 +05:00
};
const closeSidebar = () => {
2020-01-24 22:52:34 +05:00
DDS.isTab ? null : sideMenuRef.openMenu(false);
2020-01-17 21:04:21 +05:00
};
const disableGestures = () => {
2020-01-22 02:49:52 +05:00
DDS.isTab ? null : sideMenuRef.setGestureEnabled(false);
2020-01-17 21:04:21 +05:00
};
const enableGestures = () => {
2020-01-22 02:49:52 +05:00
DDS.isTab ? null : sideMenuRef.setGestureEnabled(true);
2020-01-17 21:04:21 +05:00
};
2020-01-22 02:49:52 +05:00
const showFullScreenEditor = () => {
setFullscreen(true);
2020-01-27 00:57:37 +05:00
2020-01-22 02:49:52 +05:00
editorRef.setNativeProps({
style: {
position: 'absolute',
width: '100%',
zIndex: 999,
paddingHorizontal: 100,
2020-01-27 00:57:37 +05:00
backgroundColor: outColors.bg,
2020-01-22 02:49:52 +05:00
},
});
};
2020-01-27 00:57:37 +05:00
useEffect(() => {
outColors = colors;
}, [colors]);
2020-01-22 02:49:52 +05:00
const closeFullScreenEditor = () => {
setFullscreen(false);
editorRef.setNativeProps({
style: {
position: 'relative',
width: '68%',
zIndex: null,
paddingHorizontal: 0,
2020-01-25 20:12:47 +05:00
backgroundColor: 'transparent',
2020-01-22 02:49:52 +05:00
},
});
};
2020-01-17 21:04:21 +05:00
useEffect(() => {
2020-01-25 19:42:11 +05:00
eSubscribeEvent(eDispatchAction, type => {
2020-01-17 21:04:21 +05:00
dispatch(type);
});
return () => {
2020-01-25 19:42:11 +05:00
eUnSubscribeEvent(eDispatchAction, type => {
2020-01-17 21:04:21 +05:00
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
}, []);
2020-01-25 23:47:17 +05:00
useEffect(() => {
eSubscribeEvent(eOpenSideMenu, openSidebar);
eSubscribeEvent(eCloseSideMenu, closeSidebar);
eSubscribeEvent(eDisableGestures, disableGestures);
eSubscribeEvent(eEnableGestures, enableGestures);
eSubscribeEvent(eOpenFullscreenEditor, showFullScreenEditor);
eSubscribeEvent(eCloseFullscreenEditor, closeFullScreenEditor);
return () => {
eUnSubscribeEvent(eOpenFullscreenEditor, showFullScreenEditor);
eUnSubscribeEvent(eCloseFullscreenEditor, closeFullScreenEditor);
eUnSubscribeEvent(eOpenSideMenu, openSidebar);
eUnSubscribeEvent(eCloseSideMenu, closeSidebar);
eUnSubscribeEvent(eDisableGestures, disableGestures);
eUnSubscribeEvent(eEnableGestures, enableGestures);
};
}, []);
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-24 22:52:34 +05:00
async function updateAppTheme(colors = colors) {
2020-01-17 21:04:21 +05:00
let newColors = await getColorScheme(colors);
dispatch({type: ACTIONS.THEME, colors: newColors});
}
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
<>
2020-01-27 00:25:19 +05:00
<Animatable.View
transition="backgroundColor"
duration={300}
style={{
width: '100%',
height: '100%',
flexDirection: 'row',
backgroundColor: colors.bg,
}}>
2020-01-25 00:02:13 +05:00
<Animatable.View
2020-01-27 00:25:19 +05:00
transition={['translateX']}
useNativeDriver={true}
duration={1000}
delay={2500}
2020-01-25 00:02:13 +05:00
style={{
2020-01-27 00:25:19 +05:00
width: '50%',
left: 0,
2020-01-25 00:02:13 +05:00
height: '100%',
2020-01-27 00:25:19 +05:00
position: 'absolute',
backgroundColor: colors.accent,
justifyContent: 'center',
alignItems: 'flex-end',
zIndex: 999,
transform: [
{
translateX: loading ? 0 : -w * 2,
},
],
2020-01-25 00:02:13 +05:00
}}>
2020-01-27 00:25:19 +05:00
<Animatable.Text
animation="fadeIn"
duration={300}
delay={150}
style={{
color: 'white',
fontFamily: WEIGHT.bold,
fontSize: SIZE.xxl,
}}>
notes
</Animatable.Text>
<Animatable.Text
animation="fadeIn"
duration={300}
delay={600}
style={{
color: 'white',
fontFamily: WEIGHT.regular,
fontSize: SIZE.md,
marginTop: 15,
}}>
A safe plac
</Animatable.Text>
</Animatable.View>
<Animatable.View
transition={['translateX']}
useNativeDriver={true}
duration={1000}
delay={2500}
style={{
width: '50%',
right: 0,
height: '100%',
position: 'absolute',
backgroundColor: colors.accent,
justifyContent: 'center',
alignItems: 'flex-start',
zIndex: 999,
transform: [
{
translateX: loading ? 0 : w * 2,
},
],
}}>
<Animatable.Text
animation="fadeIn"
duration={300}
delay={150}
style={{
color: 'white',
fontFamily: WEIGHT.bold,
fontSize: SIZE.xxl,
}}>
nook
</Animatable.Text>
<Animatable.Text
animation="fadeIn"
duration={300}
delay={600}
style={{
color: 'white',
fontFamily: WEIGHT.regular,
fontSize: SIZE.md,
marginTop: 15,
}}>
e to write
</Animatable.Text>
</Animatable.View>
{DDS.isTab ? (
<>
<ModalMenu colors={colors} />
<Animatable.View
animation="fadeIn"
useNativeDriver={true}
duration={500}
delay={450}
style={{
width: '4%',
}}>
<Menu
hide={false}
noTextMode={true}
colors={colors}
close={() => {
//setSidebar('0%');
}}
/>
</Animatable.View>
<Animatable.View
transition="backgroundColor"
duration={300}
style={{
width: '28%',
height: '100%',
borderRightColor: colors.nav,
borderRightWidth: 2,
}}>
2020-01-22 02:49:52 +05:00
<AppContainer
style={{
2020-01-27 00:25:19 +05:00
width: '100%',
2020-01-22 02:49:52 +05:00
height: '100%',
}}
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
2020-01-27 00:25:19 +05:00
</Animatable.View>
<View
ref={ref => (editorRef = ref)}
style={{
width: '68%',
height: '100%',
backgroundColor: 'transparent',
}}>
<Editor noMenu={fullscreen ? false : true} />
</View>
</>
) : (
<SideMenu
ref={ref => (sideMenuRef = ref)}
bounceBackOnOverdraw={false}
contentContainerStyle={{
opacity: 0,
backgroundColor: colors.bg,
}}
menu={
<Menu
hide={false}
colors={colors}
close={() => sideMenuRef.openMenu(!sideMenuRef.isOpen)}
/>
}
openMenuOffset={w / 1.3}>
<AppContainer
style={{
width: DDS.isTab ? '70%' : '100%',
height: '100%',
backgroundColor: colors.bg,
}}
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
</SideMenu>
)}
<Toast />
<DialogManager colors={colors} />
2020-01-22 02:49:52 +05:00
</Animatable.View>
2020-01-17 21:04:21 +05:00
</>
2019-11-15 01:17:59 +05:00
);
};
export default App;