Skip to content

Instantly share code, notes, and snippets.

@shawn-kb
Created February 23, 2018 21:40
Show Gist options
  • Save shawn-kb/358c60d345980c89ff1dcdf91ef5cc00 to your computer and use it in GitHub Desktop.
Save shawn-kb/358c60d345980c89ff1dcdf91ef5cc00 to your computer and use it in GitHub Desktop.
root navigator
import {AppLoading, Notifications } from 'expo';
import React from 'react';
import { StackNavigator, NavigateActions} from 'react-navigation';
// importing login here instead of in MainTabNavigator.js because it should never go into the tabNav
import LoginScreen from '../screens/LoginScreen';
import MainTabNavigator from './MainTabNavigator';
import SiteTabNavigator from './SiteTabNavigator';
import registerForPushNotificationsAsync from '../api/registerForPushNotificationsAsync';
// Mobx state stores
import { inject, observer, Provider } from 'mobx-react';
import { observable, action } from "mobx";
import stores from '../stores/stores';
const LoginStackNavigator = StackNavigator(
{
Main: { screen: MainTabNavigator,},
Site: { screen: SiteTabNavigator,},
Login: { screen: LoginScreen,},
},
{
navigationOptions: ({navigation}) => ({
headerTitleStyle: {
fontWeight: 'normal',
},
}),
}
);
const RootStackNavigator = StackNavigator(
{
Login: { screen: LoginScreen,},
Main: { screen: MainTabNavigator,},
Site: { screen: SiteTabNavigator,},
},
{
navigationOptions: ({navigation}) => ({
headerTitleStyle: {
fontWeight: 'normal',
},
}),
}
);
export default class RootNavigator extends React.Component {
componentWillMount(){
console.log(`Root Navigator will mount and stores loggedin = ${stores.systemStore.loggedIn} `);
}
componentDidMount() {
this._notificationSubscription = this._registerForPushNotifications();
}
componentWillUnmount() {
this._notificationSubscription && this._notificationSubscription.remove();
}
state = {
isLoadingComplete: false,
}
_asyncPrep() {
// little delay for my own testing
return new Promise(resolve => setTimeout(resolve, 500));
}
_handleFinishLoading = () => {
this.setState({ isLoadingComplete: true });
}
render() {
if (!this.state.isLoadingComplete) {
return (
<AppLoading
startAsync={this._asyncPrep}
onFinish={this._handleFinishLoading}
/>
);
}else{
if (stores.systemStore.loggedIn == true){
return (
<LoginStackNavigator />
);
}else{
return (
<RootStackNavigator />
)
}
}
}
_registerForPushNotifications() {
// Send our push token over to our backend so we can receive notifications
// You can comment the following line out if you want to stop receiving
// a notification every time you open the app. Check out the source
// for this function in api/registerForPushNotificationsAsync.js
registerForPushNotificationsAsync();
// Watch for incoming notifications
this._notificationSubscription = Notifications.addListener(
this._handleNotification
);
}
_handleNotification = ({ origin, data }) => {
console.log(
`Push notification ${origin} with data: ${JSON.stringify(data)}`
);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment