Files
notesnook/apps/mobile/initializer.root.js

575 lines
16 KiB
JavaScript
Raw Normal View History

2021-01-08 12:30:05 +05:00
import {
activateKeepAwake,
2021-02-22 13:06:19 +05:00
deactivateKeepAwake,
2021-01-08 12:30:05 +05:00
} from '@sayem314/react-native-keep-awake';
2021-06-03 18:48:44 +05:00
import React, {
createRef,
useCallback,
useEffect,
useRef,
useState,
} from 'react';
import {Component} from 'react';
import {FlatList} from 'react-native';
2021-02-22 13:06:19 +05:00
import {Dimensions, View} from 'react-native';
2021-06-03 18:48:44 +05:00
import Animated, {useValue} from 'react-native-reanimated';
2020-11-23 11:30:55 +05:00
import ScrollableTabView from 'react-native-scrollable-tab-view';
2021-02-22 13:06:19 +05:00
import {notesnook} from './e2e/test.ids';
2020-11-23 11:30:55 +05:00
import ContextMenu from './src/components/ContextMenu';
2021-02-22 13:06:19 +05:00
import {DialogManager} from './src/components/DialogManager';
import {DummyText} from './src/components/DummyText';
import {Menu} from './src/components/Menu';
2021-02-08 12:55:12 +05:00
import Splash from './src/components/SplashScreen';
2021-02-22 13:06:19 +05:00
import {Toast} from './src/components/Toast';
import {NavigationStack} from './src/navigation/Drawer';
import {NavigatorStack} from './src/navigation/NavigatorStack';
import {useTracked} from './src/provider';
import {Actions} from './src/provider/Actions';
import {DDS} from './src/services/DeviceDetection';
2020-11-23 11:30:55 +05:00
import {
eSendEvent,
eSubscribeEvent,
2021-02-22 13:06:19 +05:00
eUnSubscribeEvent,
2020-11-23 11:30:55 +05:00
} from './src/services/EventManager';
2021-02-22 13:06:19 +05:00
import {editing, setWidthHeight} from './src/utils';
import {updateStatusBarColor} from './src/utils/Colors';
2020-11-23 11:30:55 +05:00
import {
eClearEditor,
2020-11-23 11:30:55 +05:00
eCloseFullscreenEditor,
eCloseSideMenu,
eOnLoadNote,
eOpenFullscreenEditor,
2021-02-22 13:06:19 +05:00
eOpenSideMenu,
2020-11-23 11:30:55 +05:00
} from './src/utils/Events';
2021-02-22 13:06:19 +05:00
import {editorRef, tabBarRef} from './src/utils/Refs';
import {sleep} from './src/utils/TimeUtils';
import {EditorWrapper} from './src/views/Editor/EditorWrapper';
import {EditorWebView, getNote} from './src/views/Editor/Functions';
2021-02-04 14:39:02 +05:00
import tiny from './src/views/Editor/tiny/tiny';
2020-11-23 11:30:55 +05:00
let {width, height} = Dimensions.get('window');
let layoutTimer = null;
2020-12-01 17:32:01 +05:00
let currentTab = 0;
2021-01-08 12:30:05 +05:00
2021-06-03 18:48:44 +05:00
const onChangeTab = async obj => {
2020-11-23 11:30:55 +05:00
if (obj.i === 1) {
eSendEvent(eCloseSideMenu);
editing.movedAway = false;
2020-12-01 17:32:01 +05:00
currentTab = 1;
2021-01-03 12:39:22 +05:00
activateKeepAwake();
eSendEvent('navigate');
eSendEvent(eClearEditor, 'addHandler');
2021-06-03 18:48:44 +05:00
if (
!editing.isRestoringState &&
(!editing.currentlyEditing || !getNote())
) {
2020-11-23 11:30:55 +05:00
eSendEvent(eOnLoadNote, {type: 'new'});
editing.currentlyEditing = true;
}
2021-02-22 13:06:19 +05:00
sleep(1000).then(() => {
updateStatusBarColor();
});
2020-11-23 11:30:55 +05:00
} else {
if (obj.from === 1) {
2021-02-22 13:06:19 +05:00
updateStatusBarColor();
2021-01-03 12:39:22 +05:00
deactivateKeepAwake();
eSendEvent(eClearEditor, 'removeHandler');
if (getNote()?.locked) {
eSendEvent(eClearEditor);
}
2021-02-22 13:06:19 +05:00
eSendEvent('showTooltip');
editing.movedAway = true;
2021-02-04 14:39:02 +05:00
tiny.call(EditorWebView, tiny.blur);
2020-11-23 11:30:55 +05:00
}
2021-01-14 14:25:36 +05:00
editing.isFocused = false;
2020-12-01 17:32:01 +05:00
currentTab = 0;
2020-11-23 11:30:55 +05:00
eSendEvent(eOpenSideMenu);
}
};
2021-01-08 12:30:05 +05:00
export const RootView = React.memo(
() => {
return (
<>
2021-06-03 18:48:44 +05:00
<AppStack />
2021-01-08 12:30:05 +05:00
<Toast />
<ContextMenu />
<DummyText />
<DialogManager />
</>
);
},
() => true,
);
2020-11-23 11:30:55 +05:00
2020-12-01 17:32:01 +05:00
let updatedDimensions = {
width: width,
height: height,
};
2020-12-05 11:57:55 +05:00
let currentScroll = 0;
let startLocation = 0;
2020-12-06 11:49:40 +05:00
let startLocationX = 0;
2021-06-03 18:48:44 +05:00
const _responder = e => {
2020-12-06 09:52:52 +05:00
startLocation = e.nativeEvent.pageY;
2020-12-07 10:13:30 +05:00
startLocationX = e.nativeEvent.pageX;
2020-12-06 09:52:52 +05:00
_handleTouch();
return false;
};
2021-06-03 18:48:44 +05:00
const _moveResponder = e => {
2020-12-06 09:52:52 +05:00
_handleTouch();
return false;
};
2020-12-12 11:37:53 +05:00
let touchEndTimer = null;
2020-12-06 09:52:52 +05:00
const _handleTouch = () => {
{
2021-02-04 14:39:02 +05:00
let heightCheck = !editing.tooltip
? updatedDimensions.height - 70
: updatedDimensions.height - 140;
2020-12-12 11:37:53 +05:00
if (
2021-02-04 14:39:02 +05:00
(currentTab === 1 && startLocation > heightCheck) ||
(currentTab === 1 && startLocationX > 50) ||
2020-12-12 11:37:53 +05:00
(currentTab === 0 && startLocationX < 150)
) {
2020-12-06 09:52:52 +05:00
if (currentScroll === 0 || currentScroll === 1) {
tabBarRef.current?.setScrollEnabled(false);
}
2020-12-08 10:42:26 +05:00
} else {
2020-12-06 09:52:52 +05:00
tabBarRef.current?.setScrollEnabled(true);
}
}
};
2021-06-03 18:48:44 +05:00
const _onTouchEnd = e => {
2020-12-06 09:52:52 +05:00
startLocation = 0;
2020-12-12 11:37:53 +05:00
clearTimeout(touchEndTimer);
touchEndTimer = null;
touchEndTimer = setTimeout(() => {
tabBarRef.current?.setScrollEnabled(true);
}, 200);
2020-12-06 09:52:52 +05:00
};
2020-11-23 11:30:55 +05:00
const AppStack = React.memo(
() => {
const [state, dispatch] = useTracked();
2021-02-06 13:28:27 +05:00
const {colors, deviceMode} = state;
2020-11-23 11:30:55 +05:00
const [dimensions, setDimensions] = useState({width, height});
2021-06-03 18:48:44 +05:00
const animatedOpacity = useValue(0);
const animatedZIndex = useValue(-10);
2020-11-23 11:30:55 +05:00
const showFullScreenEditor = () => {
dispatch({type: Actions.FULLSCREEN, state: true});
editorRef.current?.setNativeProps({
style: {
position: 'absolute',
width: dimensions.width,
zIndex: 999,
paddingHorizontal: dimensions.width * 0.15,
backgroundColor: colors.bg,
},
});
};
const closeFullScreenEditor = () => {
dispatch({type: Actions.FULLSCREEN, state: false});
editorRef.current?.setNativeProps({
style: {
position: 'relative',
width: dimensions.width * 0.55,
zIndex: null,
paddingHorizontal: 0,
},
});
};
useEffect(() => {
eSubscribeEvent(eOpenFullscreenEditor, showFullScreenEditor);
eSubscribeEvent(eCloseFullscreenEditor, closeFullScreenEditor);
return () => {
eUnSubscribeEvent(eOpenFullscreenEditor, showFullScreenEditor);
eUnSubscribeEvent(eCloseFullscreenEditor, closeFullScreenEditor);
};
}, []);
2021-06-03 18:48:44 +05:00
const _onLayout = async event => {
2020-11-23 11:30:55 +05:00
if (layoutTimer) {
clearTimeout(layoutTimer);
layoutTimer = null;
}
2020-12-01 17:32:01 +05:00
2020-11-23 11:30:55 +05:00
let size = event?.nativeEvent?.layout;
2020-12-01 17:32:01 +05:00
updatedDimensions = size;
2021-02-06 13:28:27 +05:00
if (!size || (size.width === dimensions.width && deviceMode !== null)) {
2020-12-19 13:15:34 +05:00
DDS.setSize(size);
2021-02-15 11:06:12 +05:00
//console.log(deviceMode, 'MODE__');
2021-02-06 13:28:27 +05:00
dispatch({type: Actions.DEVICE_MODE, state: deviceMode});
2020-11-23 11:30:55 +05:00
return;
}
2021-02-06 13:28:27 +05:00
2020-11-23 11:30:55 +05:00
layoutTimer = setTimeout(async () => {
checkDeviceType(size);
}, 500);
};
function checkDeviceType(size) {
setDimensions({
width: size.width,
height: size.height,
});
2020-12-01 17:32:01 +05:00
2020-11-23 11:30:55 +05:00
setWidthHeight(size);
DDS.setSize(size);
2021-02-15 11:06:12 +05:00
//console.log(DDS.isLargeTablet(), size, DDS.isSmallTab);
2020-11-23 11:30:55 +05:00
if (DDS.isLargeTablet()) {
2021-02-15 11:06:12 +05:00
//console.log('setting large tab');
2020-11-23 11:30:55 +05:00
setDeviceMode('tablet', size);
2021-06-03 18:48:44 +05:00
sleep(300).then(r => eSendEvent(eOpenSideMenu));
2020-11-23 11:30:55 +05:00
} else if (DDS.isSmallTab) {
2021-02-15 11:06:12 +05:00
//console.log('setting small tab');
2020-11-23 11:30:55 +05:00
setDeviceMode('smallTablet', size);
2021-06-03 18:48:44 +05:00
sleep(300).then(r => eSendEvent(eOpenSideMenu));
2020-11-23 11:30:55 +05:00
} else {
setDeviceMode('mobile', size);
2021-06-03 18:48:44 +05:00
sleep(300).then(r => eSendEvent(eOpenSideMenu));
2020-11-23 11:30:55 +05:00
}
}
function setDeviceMode(current, size) {
eSendEvent(current !== 'mobile' ? eCloseSideMenu : eOpenSideMenu);
dispatch({type: Actions.DEVICE_MODE, state: current});
dispatch({type: Actions.FULLSCREEN, state: false});
2020-12-29 17:21:45 +05:00
2020-11-23 11:30:55 +05:00
editorRef.current?.setNativeProps({
style: {
position: 'relative',
width: current === 'tablet' ? size.width * 0.55 : size.width,
zIndex: null,
paddingHorizontal: 0,
},
});
if (!editing.movedAway && current !== 'tablet') {
2020-11-23 11:30:55 +05:00
tabBarRef.current?.goToPage(1);
}
}
2021-06-03 18:48:44 +05:00
const onScroll = scroll => {
currentScroll = scroll;
if (scroll === 0) {
eSendEvent(eOpenSideMenu);
} else {
eSendEvent(eCloseSideMenu);
}
};
2021-02-04 14:39:02 +05:00
const renderTabBar = useCallback(() => <></>, []);
2021-06-03 18:48:44 +05:00
const toggleView = show => {
//animatedZIndex.setValue(show? 999 : -10)
};
2020-11-23 11:30:55 +05:00
return (
<View
onLayout={_onLayout}
2020-11-30 16:16:03 +05:00
testID={notesnook.ids.default.root}
2020-11-23 11:30:55 +05:00
style={{
width: '100%',
height: '100%',
backgroundColor: colors.bg,
}}
2021-06-03 18:48:44 +05:00
//onMoveShouldSetResponderCapture={_moveResponder}
// onTouchEnd={_onTouchEnd}
//onStartShouldSetResponderCapture={_responder}
>
2021-02-06 13:28:27 +05:00
{deviceMode && (
2021-06-03 18:48:44 +05:00
<CustomTabs
2020-11-23 11:30:55 +05:00
ref={tabBarRef}
style={{
zIndex: 1,
}}
2021-06-03 18:48:44 +05:00
onDrawerStateChange={state => {
console.log(state);
}}
offsets={{
a: 300,
b: dimensions.width + 300,
c: dimensions.width * 2 + 300,
}}
items={[
<View
style={{
height: '100%',
width: 300,
}}>
<Menu />
</View>,
<View
style={{
height: '100%',
width: dimensions.width,
}}>
<Animated.View
onTouchEnd={() => {
tabBarRef.current?.listRef?.current?.scrollToIndex({
animated: true,
index: 1,
viewOffset: 0,
viewPosition: 0,
});
}}
style={{
position: 'absolute',
width: dimensions.width,
backgroundColor: 'rgba(0,0,0,0.2)',
opacity: animatedOpacity,
height: '100%',
zIndex: -10,
}}
/>
<NavigatorStack />
</View>,
<EditorWrapper dimensions={dimensions} />,
]}
onScroll={scrollOffset => {
if (scrollOffset > 300) {
animatedOpacity.setValue(0);
toggleView(false);
} else {
let o = scrollOffset / 300;
let op = 0;
if (o < 0) {
op = 1;
} else {
op = 1 - o;
}
animatedOpacity.setValue(op);
toggleView(true)
}
}}
2020-11-23 11:30:55 +05:00
onChangeTab={onChangeTab}
2021-06-03 18:48:44 +05:00
/>
)}
{/* {deviceMode !== 'tablet' && (
2020-11-23 11:30:55 +05:00
<View
style={{
width: dimensions.width,
height: '100%',
borderRightColor: colors.nav,
borderRightWidth: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'flex-start',
}}>
2021-02-06 13:28:27 +05:00
{deviceMode === 'smallTablet' && (
2020-11-23 11:30:55 +05:00
<View
style={{
height: '100%',
width: dimensions.width * 0.35,
}}>
<Menu />
</View>
)}
<View
style={{
height: '100%',
width:
2021-02-06 13:28:27 +05:00
deviceMode === 'mobile'
2020-11-23 11:30:55 +05:00
? dimensions.width
: dimensions.width * 0.65,
}}>
<NavigatorStack />
</View>
</View>
)}
<View
style={{
width: '100%',
height: '100%',
flexDirection: 'row',
backgroundColor: colors.bg,
}}>
2021-02-06 13:28:27 +05:00
{deviceMode === 'tablet' && (
2020-11-23 11:30:55 +05:00
<View
style={{
width: dimensions.width * 0.45,
height: '100%',
borderRightColor: colors.nav,
borderRightWidth: 1,
flexDirection: 'row',
alignItems: 'center',
}}>
<View
style={{
height: '100%',
width: dimensions.width * 0.15,
}}>
<Menu />
</View>
<View
style={{
height: '100%',
width: dimensions.width * 0.3,
}}>
<NavigatorStack />
</View>
</View>
)}
<EditorWrapper dimensions={dimensions} />
2021-06-03 18:48:44 +05:00
</View> */}
2020-11-23 11:30:55 +05:00
</View>
);
},
() => true,
);
2021-06-03 18:48:44 +05:00
let OFFSET_A = 300;
let OFFSET_B = 300 + Dimensions.get('window').width;
let OFFSET_C = 300 + Dimensions.get('window').width * 2;
class CustomTabs extends Component {
constructor(props) {
super(props);
this.listRef = createRef();
this.scrollOffset = 300;
this.page = 0;
}
renderItem = ({item, index}) => this.props.items[index];
onMoveShouldSetResponder = event => {
let x = event.nativeEvent.locationX;
let y = event.nativeEvent.locationY;
if (this.scrollOffset.toFixed(0) === this.props.offsets.b.toFixed(0)) {
if (x > 50 && y < Dimensions.get('window').height - 70) {
this.listRef.current?.getNativeScrollRef().setNativeProps({
scrollEnabled: false,
});
this.listRef.current?.scrollToIndex({
animated: true,
index: 2,
viewOffset: 0,
viewPosition: 0,
});
} else {
this.listRef.current?.getNativeScrollRef().setNativeProps({
scrollEnabled: true,
});
}
}
};
openDrawer = () => {
console.log('open');
if (this.page === 0) {
this.listRef.current?.scrollToIndex({
animated: true,
index: 0,
viewOffset: 0,
viewPosition: 0,
});
}
};
closeDrawer = () => {
console.log('close');
if (this.page === 0) {
this.listRef.current?.scrollToIndex({
animated: true,
index: 1,
viewOffset: 0,
viewPosition: 0,
});
}
};
setScrollEnabled = () => {};
onTouchEnd = () => {
this.listRef.current?.getNativeScrollRef().setNativeProps({
scrollEnabled: true,
});
};
onScroll = event => {
this.scrollOffset = event.nativeEvent.contentOffset.x;
this.props.onScroll(this.scrollOffset);
};
goToPage = page => {
if (page === 0) {
this.listRef.current?.scrollToIndex({
animated: true,
index: 1,
viewOffset: 0,
viewPosition: 0,
});
} else if (page === 1) {
this.listRef.current?.scrollToIndex({
animated: true,
index: 2,
viewOffset: 0,
viewPosition: 0,
});
}
};
keyExtractor = (item, index) => item;
onScrollEnd = event => {
this.page = 0;
if (this.scrollOffset.toFixed(0) === this.props.offsets.b.toFixed(0)) {
this.page = 1;
}
this.props.onDrawerStateChange(this.page === 0 && this.scrollOffset < 10);
this.props.onChangeTab(this.page);
};
render() {
return (
<View
onTouchEnd={this.onTouchEnd}
onMoveShouldSetResponder={this.props.onMoveShouldSetResponder}
style={{
flex: 1,
}}>
<FlatList
ref={this.listRef}
horizontal
onMomentumScrollEnd={this.onScrollEnd}
onScrollAnimationEnd={this.onScrollEnd}
keyExtractor={this.keyExtractor}
onScroll={this.onScroll}
bounces={false}
bouncesZoom={false}
initialNumToRender={100}
alwaysBounceHorizontal={false}
scrollToOverflowEnabled={false}
overScrollMode="never"
decelerationRate="fast"
maxToRenderPerBatch={100}
removeClippedSubviews={false}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="always"
showsHorizontalScrollIndicator={false}
disableIntervalMomentum={true}
snapToOffsets={[
this.props.offsets.a,
this.props.offsets.b,
this.props.offsets.c,
]}
initialScrollIndex={1}
data={['drawer', 'navigation', 'editor']}
renderItem={this.renderItem}
/>
</View>
);
}
}