/
index.js
64 lines (55 loc) · 1.58 KB
/
index.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
import { NavigationContainer } from '@react-navigation/native';
import { useEffect, useState } from 'react';
import { onAuthStateChanged } from '../api/auth';
import { useUserState } from '../contexts/UserContext';
import AuthStack from './AuthStack';
import MainStack from './MainStack';
import { Asset } from 'expo-asset';
import { initFirebase } from '../api/firebase';
import * as SplashScreen from 'expo-splash-screen';
const ImageAssets = [
require('../../assets/cover.png'),
require('../../assets/home-clock.png'),
require('../../assets/home-map.png'),
require('../../assets/icon.png'),
];
const Navigation = () => {
const [user, setUser] = useUserState();
const [isReady, setIsReady] = useState(false);
useEffect(() => {
(async () => {
try {
await SplashScreen.preventAutoHideAsync();
await Promise.all(
ImageAssets.map((image) => Asset.fromModule(image).downloadAsync())
);
initFirebase();
const unsubscribe = onAuthStateChanged((user) => {
if (user) {
setUser(user);
}
setIsReady(true);
unsubscribe();
});
} catch (e) {
// eslint-disable-next-line no-console
console.log(e);
setIsReady(true);
}
})();
}, [setUser]);
const onReady = async () => {
if (isReady) {
await SplashScreen.hideAsync();
}
};
if (!isReady) {
return null;
}
return (
<NavigationContainer onReady={onReady}>
{user.uid ? <MainStack /> : <AuthStack />}
</NavigationContainer>
);
};
export default Navigation;