|
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)}` |
|
); |
|
}; |
|
} |