2018-03-11 16:32:13 +01:00
|
|
|
import React from 'react';
|
|
|
|
import DiscoverPage from '../page/discover';
|
|
|
|
import FilePage from '../page/file';
|
2018-04-09 20:39:35 +02:00
|
|
|
import SearchPage from '../page/search';
|
2018-03-23 00:03:05 +01:00
|
|
|
import SettingsPage from '../page/settings';
|
2018-04-23 06:10:18 +02:00
|
|
|
import AboutPage from '../page/about';
|
2018-03-11 16:32:13 +01:00
|
|
|
import SplashScreen from '../page/splash';
|
2018-04-09 20:39:35 +02:00
|
|
|
import SearchInput from '../component/searchInput';
|
|
|
|
import {
|
|
|
|
addNavigationHelpers,
|
|
|
|
DrawerNavigator,
|
|
|
|
StackNavigator,
|
|
|
|
NavigationActions
|
|
|
|
} from 'react-navigation';
|
2018-03-11 16:32:13 +01:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { addListener } from '../utils/redux';
|
2018-04-09 20:39:35 +02:00
|
|
|
import { AppState, BackHandler, NativeModules, TextInput } from 'react-native';
|
2018-03-23 00:03:05 +01:00
|
|
|
import { SETTINGS } from 'lbry-redux';
|
2018-04-09 20:39:35 +02:00
|
|
|
import { makeSelectClientSetting } from '../redux/selectors/settings';
|
2018-03-11 16:32:13 +01:00
|
|
|
import Feather from 'react-native-vector-icons/Feather';
|
|
|
|
import discoverStyle from '../styles/discover';
|
2018-04-09 20:39:35 +02:00
|
|
|
import searchStyle from '../styles/search';
|
2018-03-11 16:32:13 +01:00
|
|
|
|
|
|
|
const discoverStack = StackNavigator({
|
|
|
|
Discover: {
|
|
|
|
screen: DiscoverPage,
|
|
|
|
navigationOptions: ({ navigation }) => ({
|
|
|
|
title: 'Discover',
|
2018-04-09 20:39:35 +02:00
|
|
|
headerLeft: <Feather name="menu" size={24} style={discoverStyle.drawerHamburger} onPress={() => navigation.navigate('DrawerOpen')} />,
|
|
|
|
headerRight: <Feather name="search" size={24} style={discoverStyle.rightHeaderIcon} onPress={() => navigation.navigate('Search')} />
|
2018-03-11 16:32:13 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
File: {
|
|
|
|
screen: FilePage,
|
|
|
|
navigationOptions: {
|
2018-03-22 07:26:04 +01:00
|
|
|
header: null,
|
|
|
|
drawerLockMode: 'locked-closed'
|
2018-03-11 16:32:13 +01:00
|
|
|
}
|
2018-04-09 20:39:35 +02:00
|
|
|
},
|
|
|
|
Search: {
|
|
|
|
screen: SearchPage,
|
|
|
|
navigationOptions: ({ navigation }) => ({
|
2018-04-23 11:34:45 +02:00
|
|
|
drawerLockMode: 'locked-closed',
|
2018-04-09 20:39:35 +02:00
|
|
|
headerTitle: <SearchInput style={searchStyle.searchInput} />,
|
|
|
|
headerRight: <Feather name="x" size={24} style={discoverStyle.rightHeaderIcon} onPress={() => navigation.dispatch(NavigationActions.back())} />
|
|
|
|
})
|
2018-03-11 16:32:13 +01:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
headerMode: 'screen',
|
|
|
|
});
|
|
|
|
|
|
|
|
const drawer = DrawerNavigator({
|
|
|
|
Discover: { screen: discoverStack },
|
2018-04-23 06:10:18 +02:00
|
|
|
Settings: { screen: SettingsPage, navigationOptions: { drawerLockMode: 'locked-closed' } },
|
|
|
|
About: { screen: AboutPage, navigationOptions: { drawerLockMode: 'locked-closed' } }
|
2018-03-11 16:32:13 +01:00
|
|
|
}, {
|
|
|
|
drawerWidth: 300,
|
|
|
|
headerMode: 'none'
|
|
|
|
});
|
|
|
|
|
|
|
|
export const AppNavigator = new StackNavigator({
|
|
|
|
Splash: {
|
2018-04-09 20:39:35 +02:00
|
|
|
screen: SplashScreen,
|
|
|
|
navigationOptions: {
|
|
|
|
drawerLockMode: 'locked-closed'
|
|
|
|
}
|
2018-03-11 16:32:13 +01:00
|
|
|
},
|
|
|
|
Main: {
|
|
|
|
screen: drawer
|
|
|
|
}
|
|
|
|
}, {
|
|
|
|
headerMode: 'none'
|
|
|
|
});
|
|
|
|
|
|
|
|
class AppWithNavigationState extends React.Component {
|
|
|
|
componentWillMount() {
|
2018-03-23 00:03:05 +01:00
|
|
|
AppState.addEventListener('change', this._handleAppStateChange);
|
2018-03-11 16:32:13 +01:00
|
|
|
BackHandler.addEventListener('hardwareBackPress', function() {
|
|
|
|
const { dispatch, navigation, nav } = this.props;
|
2018-04-23 06:01:33 +02:00
|
|
|
// There should be a better way to check this
|
|
|
|
if (nav.routes.length > 1) {
|
|
|
|
const subRoutes = nav.routes[1].routes[0].routes;
|
|
|
|
const lastRoute = subRoutes[subRoutes.length - 1];
|
2018-04-23 06:10:18 +02:00
|
|
|
if (['About', 'Settings'].indexOf(lastRoute.key) > -1) {
|
2018-03-11 16:32:13 +01:00
|
|
|
dispatch({ type: 'Navigation/BACK' });
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-23 06:01:33 +02:00
|
|
|
if (nav.routes[1].routeName === 'Main') {
|
|
|
|
if (nav.routes[1].routes[0].routes[0].index > 0) {
|
|
|
|
dispatch({ type: 'Navigation/BACK' });
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 16:32:13 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2018-03-23 00:03:05 +01:00
|
|
|
AppState.removeEventListener('change', this._handleAppStateChange);
|
2018-03-11 16:32:13 +01:00
|
|
|
BackHandler.removeEventListener('hardwareBackPress');
|
|
|
|
}
|
2018-03-23 00:03:05 +01:00
|
|
|
|
|
|
|
_handleAppStateChange = (nextAppState) => {
|
2018-04-23 06:01:33 +02:00
|
|
|
// this is properly handled in native code at the moment
|
|
|
|
/*const { keepDaemonRunning } = this.props;
|
2018-03-23 00:03:05 +01:00
|
|
|
if (AppState.currentState &&
|
|
|
|
AppState.currentState.match(/inactive|background/) &&
|
|
|
|
NativeModules.DaemonServiceControl) {
|
|
|
|
if (!keepDaemonRunning) {
|
|
|
|
// terminate the daemon background service when is suspended / inactive
|
2018-04-23 06:01:33 +02:00
|
|
|
//NativeModules.DaemonServiceControl.stopService();
|
2018-03-23 00:03:05 +01:00
|
|
|
}
|
2018-04-23 06:01:33 +02:00
|
|
|
}*/
|
2018-03-23 00:03:05 +01:00
|
|
|
}
|
2018-03-11 16:32:13 +01:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { dispatch, nav } = this.props;
|
|
|
|
return (
|
|
|
|
<AppNavigator
|
|
|
|
navigation={addNavigationHelpers({
|
|
|
|
dispatch,
|
|
|
|
state: nav,
|
|
|
|
addListener,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
nav: state.nav,
|
2018-03-23 00:03:05 +01:00
|
|
|
keepDaemonRunning: makeSelectClientSetting(SETTINGS.KEEP_DAEMON_RUNNING)(state),
|
|
|
|
showNsfw: makeSelectClientSetting(SETTINGS.SHOW_NSFW)(state)
|
2018-03-11 16:32:13 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(AppWithNavigationState);
|