-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.tsx
59 lines (53 loc) · 1.78 KB
/
App.tsx
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
import React, { useState } from 'react'
import { Ionicons } from '@expo/vector-icons'
import * as Font from 'expo-font'
import AppLoading from 'expo-app-loading'
import { Asset } from 'expo-asset'
import { NavigationContainer } from '@react-navigation/native'
import { ApolloProvider, useReactiveVar } from '@apollo/client'
import client, { cache, isLoggedInVar, tokenVar } from './apollo'
import LoggedInNav from './src/navigators/LoggedInNav'
import LoggedOutNav from './src/navigators/LoggedOutNav'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { persistCache, AsyncStorageWrapper } from 'apollo3-cache-persist'
export default function App() {
const [loading, setLoading] = useState(true)
const isLoggedIn = useReactiveVar(isLoggedInVar)
const onFinish = () => setLoading(false)
const preloadAssets = async () => {
const fontsToLoad = [Ionicons.font]
const fontPromises = fontsToLoad.map((font) => Font.loadAsync(font))
const imagesToLoad = [require('./assets/logo.png')]
const imagePromises = imagesToLoad.map((image) => Asset.loadAsync(image))
await Promise.all([fontPromises, imagePromises])
}
const preload = async () => {
const token = await AsyncStorage.getItem('token')
if (token) {
isLoggedInVar(true)
tokenVar(token)
}
await persistCache({
cache,
storage: new AsyncStorageWrapper(AsyncStorage),
serialize: false,
})
return preloadAssets()
}
if (loading) {
return (
<AppLoading
startAsync={preload}
onError={console.warn}
onFinish={onFinish}
/>
)
}
return (
<ApolloProvider client={client}>
<NavigationContainer>
{isLoggedIn ? <LoggedInNav /> : <LoggedOutNav />}
</NavigationContainer>
</ApolloProvider>
)
}