-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
228 lines (212 loc) · 6.29 KB
/
App.js
File metadata and controls
228 lines (212 loc) · 6.29 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import { NavigationContainer } from "@react-navigation/native";
import { createNativeStackNavigator } from "@react-navigation/native-stack";
import * as Font from "expo-font";
import { useEffect, useState } from "react";
import { Asset } from "expo-asset";
import { onAuthStateChanged } from "firebase/auth";
import { FIREBASE_AUTH, FIRESTORE } from "./firebaseConfig";
import { useUserStore } from "./util/userStore";
import * as SplashScreen from "expo-splash-screen";
import HomeScreen from "./screens/HomeScreen";
import ProductScreen from "./screens/ProductScreen";
import ProfileScreen from "./screens/ProfileScreen";
import EditAllergiesScreen from "./screens/EditAllergiesScreen";
import LoginScreen from "./screens/LoginScreen";
import SignupScreen from "./screens/SignupScreen";
import AllergiesSetupMessageScreen from "./screens/AllergiesSetupMessageScreen";
import FinishSetupScreen from "./screens/FinishSetupScreen";
import SettingsScreen from "./screens/SettingsScreen";
import { doc, getDoc } from "firebase/firestore";
// Stop splash screen from being hidden while the app is loading
SplashScreen.preventAutoHideAsync();
const Stack = createNativeStackNavigator();
function cacheImages(images) {
return images.map((image) => {
if (typeof image === "string") {
return Image.prefetch(image);
} else {
return Asset.fromModule(image).downloadAsync();
}
});
}
const App = () => {
const [appIsReady, setAppIsReady] = useState(false);
const [noUserLoggedIn, setNoUserLoggedIn] = useState(false);
const [initialLoad, setInitialLoad] = useState(true);
const { user, userInfo, setUser, setUserInfo } = useUserStore((state) => ({
user: state.user,
userInfo: state.userInfo,
setUser: state.setUser,
setUserInfo: state.setUserInfo,
}));
// Load any resources or data that you need p rior to rendering the app
useEffect(() => {
async function loadResourcesAndDataAsync() {
try {
await Font.loadAsync({
"Inter-Bold": require("./assets/fonts/Inter-Bold.ttf"),
"Inter-ExtraBold": require("./assets/fonts/Inter-ExtraBold.ttf"),
"Inter-Light": require("./assets/fonts/Inter-Light.ttf"),
Inter: require("./assets/fonts/Inter-Regular.ttf"),
"Inter-Medium": require("./assets/fonts/Inter-Medium.ttf"),
"Inter-Semi": require("./assets/fonts/Inter-SemiBold.ttf"),
});
// Cache image icons for faster load in-app
const imageAssets = cacheImages([
require("./assets/img/check_icon.png"),
require("./assets/img/x_icon.png"),
require("./assets/img/save_icon.png"),
require("./assets/img/save_icon_pressed.png"),
require("./assets/img/home_icon.png"),
require("./assets/img/home_icon_pressed.png"),
require("./assets/img/profile_icon.png"),
require("./assets/img/profile_icon_pressed.png"),
require("./assets/img/scan_icon.png"),
require("./assets/img/back_icon.png"),
require("./assets/img/default_profile_pic.png"),
require("./assets/img/settings_icon.png"),
require("./assets/img/x_icon_gray.png"),
require("./assets/img/x_icon_navy.png"),
require("./assets/img/plus_icon.png"),
]);
await Promise.all([...imageAssets]);
} catch (e) {
console.warn(e);
} finally {
setAppIsReady(true);
}
}
loadResourcesAndDataAsync();
}, []);
// Hide the splash screen once appIsReady is true
useEffect(() => {
const userInfoReady = userInfo !== null || noUserLoggedIn;
if (appIsReady && userInfoReady && initialLoad) {
setInitialLoad(false);
SplashScreen.hideAsync();
}
}, [appIsReady, userInfo, noUserLoggedIn]);
useEffect(() => {
onAuthStateChanged(FIREBASE_AUTH, async (new_user) => {
setUser(new_user);
if (new_user) {
try {
const docRef = doc(FIRESTORE, "users", new_user.uid);
const docSnap = await getDoc(docRef);
if (!docSnap.exists())
throw new Error("User record not found.");
setUserInfo(docSnap.data());
} catch (err) {
// If for some reason the user was not found in the DB
console.error(err);
setUser(null);
setNoUserLoggedIn(true);
}
} else {
setNoUserLoggedIn(true);
}
});
}, []);
const userInfoReady = userInfo !== null || noUserLoggedIn;
if (!appIsReady || !userInfoReady) {
return null;
}
return (
<NavigationContainer>
<Stack.Navigator
screenOptions={{
headerShown: false,
}}
>
{(!user || !userInfo) && (
<>
<Stack.Screen
name="Login"
component={LoginScreen}
options={{
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="Signup"
component={SignupScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="AllergiesSetupMessage"
component={AllergiesSetupMessageScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="SetupAllergies"
component={EditAllergiesScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="FinishSetup"
component={FinishSetupScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
</>
)}
{user && userInfo && (
<>
<Stack.Screen
name="Home"
component={HomeScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="Profile"
component={ProfileScreen}
options={{
gestureEnabled: "false",
animation: "none",
}}
/>
<Stack.Screen
name="Settings"
component={SettingsScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="Product"
component={ProductScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
<Stack.Screen
name="EditAllergies"
component={EditAllergiesScreen}
options={{
animation: "slide_from_right",
orientation: "portrait_up",
}}
/>
</>
)}
</Stack.Navigator>
</NavigationContainer>
);
};
export default App;