123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- /**
- * @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('Home');
- };
- }
- 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,
- });
|