Skip to content

Commit

Permalink
navigation: Split AppWithNavigation
Browse files Browse the repository at this point in the history
The component had two purposes that are now split in two components:
* handle back navigation in Android - now extracted to BackNavigationHandler
* integrate Redux with RN - kept in AppWithNavigation
  • Loading branch information
borisyankov committed Jul 24, 2018
1 parent e4050f4 commit 1860ead
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/ZulipMobile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import StylesProvider from './boot/StylesProvider';
import CompatibilityChecker from './boot/CompatibilityChecker';
import AppEventHandlers from './boot/AppEventHandlers';
import AppDataFetcher from './boot/AppDataFetcher';
import BackNavigationHandler from './nav/BackNavigationHandler';
import AppWithNavigation from './nav/AppWithNavigation';

require('./i18n/locale');
Expand All @@ -23,7 +24,9 @@ export default () => (
<AppDataFetcher>
<TranslationProvider>
<StylesProvider>
<AppWithNavigation />
<BackNavigationHandler>
<AppWithNavigation />
</BackNavigationHandler>
</StylesProvider>
</TranslationProvider>
</AppDataFetcher>
Expand Down
22 changes: 1 addition & 21 deletions src/nav/AppWithNavigation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,19 @@
import { connect } from 'react-redux';

import React, { PureComponent } from 'react';
import { BackHandler } from 'react-native';
import { addNavigationHelpers } from 'react-navigation';
import { createReduxBoundAddListener } from 'react-navigation-redux-helpers';

import type { Dispatch } from '../types';
import { getCanGoBack, getNav } from '../selectors';
import { getNav } from '../selectors';
import AppNavigator from './AppNavigator';
import { navigateBack } from '../actions';

type Props = {
canGoBack: boolean,
dispatch: Dispatch,
nav: Object,
};

class AppWithNavigation extends PureComponent<Props> {
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonPress);
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonPress);
}

handleBackButtonPress = () => {
const { canGoBack, dispatch } = this.props;
if (canGoBack) {
dispatch(navigateBack());
}
return canGoBack;
};

render() {
const { dispatch, nav } = this.props;
const addListener = createReduxBoundAddListener('root');
Expand All @@ -52,5 +33,4 @@ class AppWithNavigation extends PureComponent<Props> {

export default connect(state => ({
nav: getNav(state),
canGoBack: getCanGoBack(state),
}))(AppWithNavigation);
41 changes: 41 additions & 0 deletions src/nav/BackNavigationHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* @flow */
import { connect } from 'react-redux';

import { PureComponent } from 'react';
import { BackHandler } from 'react-native';

import type { Dispatch } from '../types';
import { getCanGoBack } from '../selectors';
import { navigateBack } from '../actions';

type Props = {
children?: ChildrenArray<*>,
canGoBack: boolean,
dispatch: Dispatch,
};

class BackNavigationHandler extends PureComponent<Props> {
componentDidMount() {
BackHandler.addEventListener('hardwareBackPress', this.handleBackButtonPress);
}

componentWillUnmount() {
BackHandler.removeEventListener('hardwareBackPress', this.handleBackButtonPress);
}

handleBackButtonPress = () => {
const { canGoBack, dispatch } = this.props;
if (canGoBack) {
dispatch(navigateBack());
}
return canGoBack;
};

render() {
return this.props.children;
}
}

export default connect(state => ({
canGoBack: getCanGoBack(state),
}))(BackNavigationHandler);

0 comments on commit 1860ead

Please sign in to comment.