Files
notesnook/apps/mobile/App.js

293 lines
7.2 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-22 02:49:52 +05:00
import {
StatusBar,
View,
Platform,
TouchableOpacity,
Dimensions,
Modal,
} from 'react-native';
2019-11-29 11:31:43 +05:00
import * as Animatable from 'react-native-animatable';
2020-01-22 02:49:52 +05:00
import {w, SideMenuEvent} 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-22 02:49:52 +05:00
import Animated, {Easing} from 'react-native-reanimated';
2020-01-17 21:04:21 +05:00
import {
DialogManager,
_recieveEvent,
_unSubscribeEvent,
} from './src/components/DialogManager';
import {getColorScheme} from './src/common/common';
2020-01-22 02:49:52 +05:00
import Editor from './src/views/Editor';
import {ModalMenu} from './src/components/ModalMenu';
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-22 02:49:52 +05:00
const {
Clock,
Value,
set,
cond,
startClock,
clockRunning,
timing,
debug,
stopClock,
block,
} = Animated;
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;
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 11:41:42 +05:00
const {colors} = state;
2020-01-22 02:49:52 +05:00
const [width, setWidth] = useState(w);
2019-12-14 21:01:39 +05:00
// Local 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);
let animatedSidebarWidth = new Value(w * 0.04);
let animatedAppContainerWidth = new Value(w * 0.96);
2019-12-11 01:10:04 +05:00
2020-01-22 02:49:52 +05:00
let isShown = true;
2019-12-14 21:01:39 +05:00
// Effects
2020-01-17 21:04:21 +05:00
const openSidebar = () => {
2020-01-22 02:49:52 +05:00
DDS.isTab ? sidebarAnimation() : sideMenuRef.openMenu(true);
2020-01-17 21:04:21 +05:00
};
const closeSidebar = () => {
2020-01-22 02:49:52 +05:00
DDS.isTab ? sidebarAnimation() : 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
};
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
2020-01-22 02:49:52 +05:00
_recieveEvent('showFullScreenEditor', showFullScreenEditor);
_recieveEvent('closeFullScreenEditor', closeFullScreenEditor);
2019-11-23 06:25:22 +05:00
return () => {
2020-01-22 02:49:52 +05:00
_unSubscribeEvent('showFullScreenEditor', showFullScreenEditor);
_unSubscribeEvent('closeFullScreenEditor', closeFullScreenEditor);
2020-01-17 21:04:21 +05:00
_unSubscribeEvent('openSidebar', openSidebar);
_unSubscribeEvent('closeSidebar', closeSidebar);
_unSubscribeEvent('disableGesture', disableGestures);
_unSubscribeEvent('enableGesture', enableGestures);
};
}, []);
2020-01-22 02:49:52 +05:00
const showFullScreenEditor = () => {
setFullscreen(true);
editorRef.setNativeProps({
style: {
position: 'absolute',
width: '100%',
zIndex: 999,
paddingHorizontal: 100,
backgroundColor: colors.bg,
},
});
};
const closeFullScreenEditor = () => {
setFullscreen(false);
editorRef.setNativeProps({
style: {
position: 'relative',
width: '68%',
zIndex: null,
paddingHorizontal: 0,
backgroundColor: 'transparent',
},
});
};
2020-01-17 21:04:21 +05:00
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});
}
2020-01-22 02:49:52 +05:00
const sidebarAnimation = () => {
let sValue;
let aValue;
if (isShown) {
sValue = w * 0.04;
aValue = w * 0.96;
isShown = false;
} else {
sValue = w * 0.25;
aValue = w * 0.75;
isShown = true;
}
let _anim = timing(animatedSidebarWidth, {
duration: 200,
toValue: sValue,
easing: Easing.in(Easing.ease),
});
let _animS = timing(animatedAppContainerWidth, {
duration: 0,
toValue: aValue,
easing: Easing.inOut(Easing.ease),
});
if (isShown) {
_animS.start();
_anim.start();
} else {
_anim.start();
_animS.start();
}
};
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
<>
2020-01-22 02:49:52 +05:00
<Animatable.View
transition="backgroundColor"
duration={300}
2019-12-14 16:00:16 +05:00
style={{
width: '100%',
height: '100%',
flexDirection: 'row',
backgroundColor: colors.bg,
2020-01-22 02:49:52 +05:00
}}
onLayout={e => {
setWidth(Dimensions.get('window').width);
2019-12-14 16:00:16 +05:00
}}>
{DDS.isTab ? (
<>
2020-01-22 02:49:52 +05:00
<ModalMenu colors={colors} />
<View
2019-12-14 16:00:16 +05:00
style={{
2020-01-22 02:49:52 +05:00
width: '4%',
2019-12-14 16:00:16 +05:00
}}>
<Menu
2019-12-14 21:01:39 +05:00
hide={false}
2020-01-22 02:49:52 +05:00
noTextMode={true}
2019-12-14 16:00:16 +05:00
colors={colors}
close={() => {
//setSidebar('0%');
}}
/>
2020-01-22 02:49:52 +05:00
</View>
<View
2019-12-14 16:00:16 +05:00
style={{
2020-01-22 02:49:52 +05:00
width: '28%',
2019-12-14 16:00:16 +05:00
height: '100%',
2020-01-22 02:49:52 +05:00
borderRightColor: colors.nav,
borderRightWidth: 2,
}}>
<AppContainer
style={{
width: '100%',
height: '100%',
}}
ref={navigatorRef => {
NavigationService.setTopLevelNavigator(navigatorRef);
}}
/>
</View>
<View
ref={ref => (editorRef = ref)}
style={{
width: '68%',
height: '100%',
backgroundColor: 'transparent',
}}>
<Editor noMenu={fullscreen ? false : true} />
</View>
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} />
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;