-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
88 lines (79 loc) · 2.46 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { useContext, useEffect, useState } from "react";
import { StatusBar } from "react-native";
import { NavigationContainer, DefaultTheme } from "@react-navigation/native";
import { PaperProvider } from "react-native-paper";
import { SafeAreaProvider } from "react-native-safe-area-context";
import AsyncStorage from "@react-native-async-storage/async-storage";
import * as SplashScreen from "expo-splash-screen";
import MainTab from "@/navigation/MainTab";
import AuthStack from "@/navigation/AuthStack";
import {
FavoriteMoviesContext,
FavoriteMoviesProvider,
} from "@/context/FavoriteMoviesContext";
import { AuthContext, AuthContextProvider } from "@/context/AuthContext";
import COLORS from "@/constants/colors";
import { getCurrentUserUid, getUserDoc } from "@/utils/firebase";
import { fetchMoviesByIds } from "@/api/helper";
const MyTheme = {
...DefaultTheme,
colors: {
...DefaultTheme.colors,
background: COLORS.primary,
text: COLORS.text,
},
};
const Navigation = () => {
const { isAuthenticated } = useContext(AuthContext);
return (
<NavigationContainer theme={MyTheme}>
{isAuthenticated ? <MainTab /> : <AuthStack />}
</NavigationContainer>
);
};
const Root = () => {
const [isTryingLogin, setIsTryingLogin] = useState(true);
const authCtx = useContext(AuthContext);
const favCtx = useContext(FavoriteMoviesContext);
useEffect(() => {
const loadData = async () => {
const userDoc = await getUserDoc(getCurrentUserUid());
const cloudMovies = await fetchMoviesByIds(userDoc.moviesId);
if (cloudMovies.length > 0) {
favCtx.setFavoriteMovies(cloudMovies);
}
};
const getToken = async () => {
SplashScreen.preventAutoHideAsync();
const storedToken = await AsyncStorage.getItem("token");
if (storedToken) {
authCtx.authenticate(storedToken);
await loadData();
}
setIsTryingLogin(false);
SplashScreen.hideAsync();
};
getToken();
}, [authCtx.isAuthenticated]);
if (isTryingLogin) {
return null;
}
return <Navigation />;
};
export default function App() {
return (
<PaperProvider>
<AuthContextProvider>
<FavoriteMoviesProvider>
<StatusBar
barStyle={"light-content"}
backgroundColor={COLORS.primary}
/>
<SafeAreaProvider>
<Root />
</SafeAreaProvider>
</FavoriteMoviesProvider>
</AuthContextProvider>
</PaperProvider>
);
}