2016-02-05 22:25:17 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
* @providesModule NavigationRootContainer
|
|
|
|
* @flow
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2016-02-09 04:02:45 +00:00
|
|
|
const AsyncStorage = require('AsyncStorage');
|
|
|
|
const React = require('React');
|
|
|
|
const BackAndroid = require('BackAndroid');
|
|
|
|
const Platform = require('Platform');
|
2016-02-05 22:25:17 +00:00
|
|
|
|
|
|
|
import type {
|
2016-02-23 00:15:35 +00:00
|
|
|
NavigationAction,
|
2016-02-05 22:25:17 +00:00
|
|
|
NavigationState,
|
|
|
|
NavigationReducer
|
2016-02-19 09:24:07 +00:00
|
|
|
} from 'NavigationStateUtils';
|
2016-02-05 22:25:17 +00:00
|
|
|
|
2016-02-09 04:02:45 +00:00
|
|
|
export type NavigationRenderer = (
|
2016-02-05 22:25:17 +00:00
|
|
|
navigationState: NavigationState,
|
|
|
|
onNavigate: Function
|
|
|
|
) => ReactElement;
|
|
|
|
|
2016-02-09 04:02:45 +00:00
|
|
|
export type BackAction = {
|
|
|
|
type: 'BackAction';
|
|
|
|
};
|
|
|
|
|
|
|
|
function getBackAction(): BackAction {
|
|
|
|
return { type: 'BackAction' };
|
|
|
|
}
|
|
|
|
|
2016-02-05 22:25:17 +00:00
|
|
|
type Props = {
|
|
|
|
renderNavigation: NavigationRenderer;
|
|
|
|
reducer: NavigationReducer;
|
|
|
|
persistenceKey: ?string;
|
2016-02-23 00:15:35 +00:00
|
|
|
initialAction: NavigationAction;
|
2016-02-05 22:25:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class NavigationRootContainer extends React.Component {
|
|
|
|
props: Props;
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
this.handleNavigation = this.handleNavigation.bind(this);
|
|
|
|
let navState = null;
|
|
|
|
if (!this.props.persistenceKey) {
|
2016-02-23 00:15:35 +00:00
|
|
|
navState = this.props.reducer(null, props.initialAction);
|
2016-02-05 22:25:17 +00:00
|
|
|
}
|
|
|
|
this.state = { navState };
|
|
|
|
}
|
|
|
|
componentDidMount() {
|
|
|
|
if (this.props.persistenceKey) {
|
|
|
|
AsyncStorage.getItem(this.props.persistenceKey, (err, storedString) => {
|
|
|
|
if (err || !storedString) {
|
|
|
|
this.setState({
|
2016-02-23 00:15:35 +00:00
|
|
|
navState: this.props.reducer(null, this.props.initialAction),
|
2016-02-05 22:25:17 +00:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({
|
|
|
|
navState: JSON.parse(storedString),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
getChildContext(): Object {
|
|
|
|
return {
|
|
|
|
onNavigate: this.handleNavigation,
|
|
|
|
};
|
|
|
|
}
|
2016-02-09 04:02:45 +00:00
|
|
|
handleNavigation(action: Object): boolean {
|
2016-02-05 22:25:17 +00:00
|
|
|
const navState = this.props.reducer(this.state.navState, action);
|
2016-02-09 04:02:45 +00:00
|
|
|
if (navState === this.state.navState) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-02-05 22:25:17 +00:00
|
|
|
this.setState({
|
|
|
|
navState,
|
|
|
|
});
|
|
|
|
if (this.props.persistenceKey) {
|
|
|
|
AsyncStorage.setItem(this.props.persistenceKey, JSON.stringify(navState));
|
|
|
|
}
|
2016-02-09 04:02:45 +00:00
|
|
|
return true;
|
2016-02-05 22:25:17 +00:00
|
|
|
}
|
|
|
|
render(): ReactElement {
|
2016-02-09 04:02:45 +00:00
|
|
|
const navigation = this.props.renderNavigation(
|
2016-02-05 22:25:17 +00:00
|
|
|
this.state.navState,
|
|
|
|
this.handleNavigation
|
|
|
|
);
|
|
|
|
return navigation;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NavigationRootContainer.childContextTypes = {
|
|
|
|
onNavigate: React.PropTypes.func,
|
|
|
|
};
|
|
|
|
|
2016-02-23 00:15:35 +00:00
|
|
|
NavigationRootContainer.defaultProps = {
|
|
|
|
initialAction: {
|
|
|
|
type: 'NavigationRootContainerInitialAction',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2016-02-09 04:02:45 +00:00
|
|
|
NavigationRootContainer.getBackAction = getBackAction;
|
|
|
|
|
2016-02-05 22:25:17 +00:00
|
|
|
module.exports = NavigationRootContainer;
|