-
Notifications
You must be signed in to change notification settings - Fork 1
/
AppNavigator.js
70 lines (55 loc) · 1.73 KB
/
AppNavigator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import React from 'react';
import {connect} from 'react-redux';
import {BackHandler, NetInfo} from 'react-native';
import {addNavigationHelpers, NavigationActions} from 'react-navigation';
import Root from './Root';
import {updateNetStatus} from './actions/network';
class AppNavigator extends React.Component {
componentWillMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackPress);
}
componentDidMount() {
NetInfo.isConnected.addEventListener('connectionChange', this.handleConnectionInfoChange);
}
componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackPress);
NetInfo.isConnected.removeEventListener('connectionChange', this.handleConnectionInfoChange);
}
goBack =() => {
this.props.dispatch(NavigationActions.back());
};
handleBackPress = () => {
if (this.shouldCloseApp(this.props.nav)) {
return false;
}
this.goBack();
return true;
};
shouldCloseApp = (nav) => {
if (nav.index > 0) return false;
if (nav.routes) {
return nav.routes.every(this.shouldCloseApp);
}
return true;
};
handleConnectionInfoChange = (isConnected) => {
this.props.dispatch(updateNetStatus(isConnected));
};
render() {
return (
<Root
navigation={addNavigationHelpers({
dispatch: this.props.dispatch,
state: this.props.nav
})}
/>
);
}
}
const mapStateToProps = state => {
return {
nav: state.nav,
net: state.net
};
};
export default connect(mapStateToProps)(AppNavigator);