1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-27 10:32:58 +02:00
joplin/packages/app-mobile/components/app-nav.tsx

132 lines
4.5 KiB
TypeScript
Raw Normal View History

import * as React from 'react';
import { connect } from 'react-redux';
import NotesScreen from './screens/Notes';
2020-11-05 18:58:23 +02:00
const { SearchScreen } = require('./screens/search.js');
import { Component } from 'react';
import { KeyboardAvoidingView, Keyboard, Platform, View, KeyboardEvent, Dimensions, EmitterSubscription } from 'react-native';
import { AppState } from '../utils/types';
2020-11-05 18:58:23 +02:00
const { themeStyle } = require('./global-style.js');
2017-07-14 20:49:14 +02:00
interface State {
autoCompletionBarExtraHeight: number;
floatingKeyboardEnabled: boolean;
}
interface Props {
route: any;
screens: any;
dispatch: (action: any)=> void;
themeId: number;
}
class AppNavComponent extends Component<Props, State> {
private previousRouteName_: string|null = null;
private keyboardDidShowListener: EmitterSubscription|null = null;
private keyboardDidHideListener: EmitterSubscription|null = null;
private keyboardWillChangeFrameListener: EmitterSubscription|null = null;
public constructor(props: Props) {
super(props);
this.previousRouteName_ = null;
this.state = {
autoCompletionBarExtraHeight: 0, // Extra padding for the auto completion bar at the top of the keyboard
floatingKeyboardEnabled: false,
2019-07-29 15:43:53 +02:00
};
}
public UNSAFE_componentWillMount() {
if (Platform.OS === 'ios') {
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.keyboardDidShow.bind(this));
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this.keyboardDidHide.bind(this));
this.keyboardWillChangeFrameListener = Keyboard.addListener('keyboardWillChangeFrame', this.keyboardWillChangeFrame);
}
}
public componentWillUnmount() {
this.keyboardDidShowListener?.remove();
this.keyboardDidHideListener?.remove();
this.keyboardWillChangeFrameListener?.remove();
this.keyboardDidShowListener = null;
this.keyboardDidHideListener = null;
this.keyboardWillChangeFrameListener = null;
}
public keyboardDidShow() {
2019-07-29 15:43:53 +02:00
this.setState({ autoCompletionBarExtraHeight: 30 });
}
public keyboardDidHide() {
2019-07-29 15:43:53 +02:00
this.setState({ autoCompletionBarExtraHeight: 0 });
}
2017-08-01 19:59:01 +02:00
private keyboardWillChangeFrame = (evt: KeyboardEvent) => {
const windowWidth = Dimensions.get('window').width;
// If the keyboard isn't as wide as the window, the floating keyboard is diabled.
// See https://github.com/facebook/react-native/issues/29473#issuecomment-696658937
this.setState({
floatingKeyboardEnabled: evt.endCoordinates.width < windowWidth,
});
};
public render() {
2017-07-14 20:49:14 +02:00
if (!this.props.route) throw new Error('Route must not be null');
// Note: certain screens are kept into memory, in particular Notes and Search
// so that the scroll position is not lost when the user navigate away from them.
const route = this.props.route;
2017-07-31 22:03:12 +02:00
let Screen = null;
let notesScreenVisible = false;
let searchScreenVisible = false;
2017-07-31 22:03:12 +02:00
if (route.routeName === 'Notes') {
2017-07-31 22:03:12 +02:00
notesScreenVisible = true;
} else if (route.routeName === 'Search') {
searchScreenVisible = true;
2017-07-31 22:03:12 +02:00
} else {
Screen = this.props.screens[route.routeName].screen;
}
2017-07-14 20:49:14 +02:00
// Keep the search screen loaded if the user is viewing a note from that search screen
// so that if the back button is pressed, the screen is still loaded. However, unload
// it if navigating away.
const searchScreenLoaded = searchScreenVisible || (this.previousRouteName_ === 'Search' && route.routeName === 'Note');
this.previousRouteName_ = route.routeName;
2020-09-15 15:01:07 +02:00
const theme = themeStyle(this.props.themeId);
2019-07-29 15:43:53 +02:00
const style = { flex: 1, backgroundColor: theme.backgroundColor };
// When the floating keybaord is enabled, the KeyboardAvoidingView can have a very small
// height. Don't use the KeyboardAvoidingView when the floating keyboard is enabled.
// See https://github.com/facebook/react-native/issues/29473
const keyboardAvoidingViewEnabled = !this.state.floatingKeyboardEnabled;
2017-07-14 20:49:14 +02:00
return (
<KeyboardAvoidingView
enabled={keyboardAvoidingViewEnabled}
behavior={Platform.OS === 'ios' ? 'padding' : null}
style={style}
>
2017-07-31 22:03:12 +02:00
<NotesScreen visible={notesScreenVisible} navigation={{ state: route }} />
2019-07-29 15:43:53 +02:00
{searchScreenLoaded && <SearchScreen visible={searchScreenVisible} navigation={{ state: route }} />}
{!notesScreenVisible && !searchScreenVisible && <Screen navigation={{ state: route }} themeId={this.props.themeId} dispatch={this.props.dispatch} />}
<View style={{ height: this.state.autoCompletionBarExtraHeight }} />
</KeyboardAvoidingView>
2017-07-14 20:49:14 +02:00
);
}
}
const AppNav = connect((state: AppState) => {
2019-07-29 15:43:53 +02:00
return {
route: state.route,
2020-09-15 15:01:07 +02:00
themeId: state.settings.theme,
2019-07-29 15:43:53 +02:00
};
})(AppNavComponent);
2017-07-14 20:49:14 +02:00
2019-07-29 15:43:53 +02:00
module.exports = { AppNav };