2019-07-29 15:43:53 +02:00
|
|
|
const React = require('react');
|
|
|
|
const Component = React.Component;
|
2017-11-03 02:09:34 +02:00
|
|
|
const { connect } = require('react-redux');
|
2020-11-05 18:58:23 +02:00
|
|
|
const { NotesScreen } = require('./screens/notes.js');
|
|
|
|
const { SearchScreen } = require('./screens/search.js');
|
2018-02-05 20:32:59 +02:00
|
|
|
const { KeyboardAvoidingView, Keyboard, Platform, View } = require('react-native');
|
2020-11-05 18:58:23 +02:00
|
|
|
const { themeStyle } = require('./global-style.js');
|
2017-07-14 20:49:14 +02:00
|
|
|
|
|
|
|
class AppNavComponent extends Component {
|
2017-08-01 21:08:38 +02:00
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.previousRouteName_ = null;
|
2018-02-05 20:32:59 +02:00
|
|
|
this.state = {
|
|
|
|
autoCompletionBarExtraHeight: 0, // Extra padding for the auto completion bar at the top of the keyboard
|
2019-07-29 15:43:53 +02:00
|
|
|
};
|
2018-02-05 20:32:59 +02:00
|
|
|
}
|
|
|
|
|
2018-04-30 18:38:19 +02:00
|
|
|
UNSAFE_componentWillMount() {
|
2018-02-05 20:32:59 +02:00
|
|
|
if (Platform.OS === 'ios') {
|
|
|
|
this.keyboardDidShowListener = Keyboard.addListener('keyboardDidShow', this.keyboardDidShow.bind(this));
|
|
|
|
this.keyboardDidHideListener = Keyboard.addListener('keyboardDidHide', this.keyboardDidHide.bind(this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.keyboardDidShowListener) this.keyboardDidShowListener.remove();
|
|
|
|
if (this.keyboardDidHideListener) this.keyboardDidHideListener.remove();
|
|
|
|
this.keyboardDidShowListener = null;
|
|
|
|
this.keyboardDidHideListener = null;
|
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
keyboardDidShow() {
|
|
|
|
this.setState({ autoCompletionBarExtraHeight: 30 });
|
2018-02-05 20:32:59 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
keyboardDidHide() {
|
|
|
|
this.setState({ autoCompletionBarExtraHeight: 0 });
|
2017-08-01 21:08:38 +02:00
|
|
|
}
|
2017-08-01 19:59:01 +02:00
|
|
|
|
2017-08-01 21:08:38 +02:00
|
|
|
render() {
|
2017-07-14 20:49:14 +02:00
|
|
|
if (!this.props.route) throw new Error('Route must not be null');
|
|
|
|
|
2017-08-01 21:08:38 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const route = this.props.route;
|
2017-07-31 22:03:12 +02:00
|
|
|
let Screen = null;
|
|
|
|
let notesScreenVisible = false;
|
2017-08-01 21:08:38 +02:00
|
|
|
let searchScreenVisible = false;
|
2017-07-31 22:03:12 +02:00
|
|
|
|
2022-07-23 09:31:32 +02:00
|
|
|
if (route.routeName === 'Notes') {
|
2017-07-31 22:03:12 +02:00
|
|
|
notesScreenVisible = true;
|
2022-07-23 09:31:32 +02:00
|
|
|
} else if (route.routeName === 'Search') {
|
2017-08-01 21:08:38 +02:00
|
|
|
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
|
|
|
|
2017-08-01 21:08:38 +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.
|
2022-07-23 09:31:32 +02:00
|
|
|
const searchScreenLoaded = searchScreenVisible || (this.previousRouteName_ === 'Search' && route.routeName === 'Note');
|
2017-08-01 21:08:38 +02:00
|
|
|
|
|
|
|
this.previousRouteName_ = route.routeName;
|
|
|
|
|
2020-09-15 15:01:07 +02:00
|
|
|
const theme = themeStyle(this.props.themeId);
|
2017-10-30 20:27:51 +02:00
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
const style = { flex: 1, backgroundColor: theme.backgroundColor };
|
2017-10-30 20:27:51 +02:00
|
|
|
|
2017-07-14 20:49:14 +02:00
|
|
|
return (
|
2019-07-29 15:43:53 +02:00
|
|
|
<KeyboardAvoidingView 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 }} />}
|
2018-02-05 20:32:59 +02:00
|
|
|
<View style={{ height: this.state.autoCompletionBarExtraHeight }} />
|
|
|
|
</KeyboardAvoidingView>
|
2017-07-14 20:49:14 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-21 10:14:33 +02:00
|
|
|
const AppNav = connect(state => {
|
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 };
|