mirror of
https://github.com/status-im/react-navigation.git
synced 2025-02-24 17:18:09 +00:00
122 lines
2.7 KiB
JavaScript
122 lines
2.7 KiB
JavaScript
/**
|
|
* @flow
|
|
*/
|
|
|
|
import React from 'react';
|
|
import {
|
|
ActivityIndicator,
|
|
AsyncStorage,
|
|
StatusBar,
|
|
StyleSheet,
|
|
View,
|
|
} from 'react-native';
|
|
import { createStackNavigator, createSwitchNavigator } from 'react-navigation';
|
|
import { Button } from './commonComponents/ButtonWithMargin';
|
|
|
|
class SignInScreen extends React.Component<any, any> {
|
|
static navigationOptions = {
|
|
title: 'Please sign in',
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<View style={styles.container}>
|
|
<Button title="Sign in!" onPress={this._signInAsync} />
|
|
<Button
|
|
title="Go back to other examples"
|
|
onPress={() => this.props.navigation.goBack(null)}
|
|
/>
|
|
<StatusBar barStyle="default" />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
_signInAsync = async () => {
|
|
await AsyncStorage.setItem('userToken', 'abc');
|
|
this.props.navigation.navigate('App');
|
|
};
|
|
}
|
|
|
|
class HomeScreen extends React.Component<any, any> {
|
|
static navigationOptions = {
|
|
title: 'Welcome to the app!',
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<View style={styles.container}>
|
|
<Button title="Show me more of the app" onPress={this._showMoreApp} />
|
|
<Button title="Actually, sign me out :)" onPress={this._signOutAsync} />
|
|
<StatusBar barStyle="default" />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
_showMoreApp = () => {
|
|
this.props.navigation.navigate('Other');
|
|
};
|
|
|
|
_signOutAsync = async () => {
|
|
await AsyncStorage.clear();
|
|
this.props.navigation.navigate('Auth');
|
|
};
|
|
}
|
|
|
|
class OtherScreen extends React.Component<any, any> {
|
|
static navigationOptions = {
|
|
title: 'Lots of features here',
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<View style={styles.container}>
|
|
<Button title="I'm done, sign me out" onPress={this._signOutAsync} />
|
|
<StatusBar barStyle="default" />
|
|
</View>
|
|
);
|
|
}
|
|
|
|
_signOutAsync = async () => {
|
|
await AsyncStorage.clear();
|
|
this.props.navigation.navigate('Auth');
|
|
};
|
|
}
|
|
|
|
class LoadingScreen extends React.Component<any, any> {
|
|
componentDidMount() {
|
|
this._bootstrapAsync();
|
|
}
|
|
|
|
_bootstrapAsync = async () => {
|
|
const userToken = await AsyncStorage.getItem('userToken');
|
|
let initialRouteName = userToken ? 'App' : 'Auth';
|
|
this.props.navigation.navigate(initialRouteName);
|
|
};
|
|
|
|
render() {
|
|
return (
|
|
<View style={styles.container}>
|
|
<ActivityIndicator />
|
|
<StatusBar barStyle="default" />
|
|
</View>
|
|
);
|
|
}
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
container: {
|
|
flex: 1,
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
},
|
|
});
|
|
|
|
const AppStack = createStackNavigator({ Home: HomeScreen, Other: OtherScreen });
|
|
const AuthStack = createStackNavigator({ SignIn: SignInScreen });
|
|
|
|
export default createSwitchNavigator({
|
|
Loading: LoadingScreen,
|
|
App: AppStack,
|
|
Auth: AuthStack,
|
|
});
|