-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
79 lines (69 loc) · 2.57 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React, { useState, useEffect, useCallback } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { View } from 'react-native';
import navigationTheme from './app/navigation/navigationTheme';
import AppNavigator from './app/navigation/AppNavigator';
import OfflineNotice from './app/components/OfflineNotice';
import AuthNavigator from './app/navigation/AuthNavigator';
import AuthContext from './app/auth/context';
import authStorage from './app/auth/storage';
import { navigationRef } from './app/navigation/rootNavigation';
import Entypo from '@expo/vector-icons/Entypo';
import * as SplashScreen from 'expo-splash-screen';
import * as Font from 'expo-font';
export default function App() {
const [user, setUser] = useState();
const [isReady, setIsReady] = useState(false);
// Keep the splash screen visible while we fetch resources
SplashScreen.preventAutoHideAsync();
const restoreUser = async () => {
const user = await authStorage.getUser();
if (!user) return;
setUser(user);
};
useEffect(() => {
async function prepare() {
try {
// Keep the splash screen visible while fetching resources
// Pre-load fonts, make any API calls you need to do here
await Font.loadAsync(Entypo.font);
await restoreUser();
// Artificially delay for two seconds to simulate a slow loading
// experience. Please remove this if you copy and paste the code!
await new Promise((resolve) => setTimeout(resolve, 2000));
} catch (e) {
console.warn(e);
} finally {
// Tell the application to render
setIsReady(true);
}
}
prepare();
}, []);
const onLayoutRootView = useCallback(async () => {
if (isReady) {
// This tells the splash screen to hide immediately! If we call this after
// `isReady`, then we may see a blank screen while the app is
// loading its initial state and rendering its first pixels. So instead,
// we hide the splash screen once we know the root view has already
// performed layout.
await SplashScreen.hideAsync();
}
}, [isReady]);
if (!isReady) {
return (
<View
style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}
onLayout={onLayoutRootView}
></View>
);
}
return (
<AuthContext.Provider value={{ user, setUser }}>
<OfflineNotice />
<NavigationContainer ref={navigationRef} theme={navigationTheme}>
{user ? <AppNavigator /> : <AuthNavigator />}
</NavigationContainer>
</AuthContext.Provider>
);
}