-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
168 lines (139 loc) · 5.07 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
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
import { useFonts } from "expo-font";
import { React, useState, useCallback, useEffect } from 'react';
import { StatusBar } from 'expo-status-bar';
import { useFocusEffect } from '@react-navigation/native';
import { StyleSheet, Text, View, Image } from 'react-native';
// Removed the import { Link } from 'expo-router' since it's not used here
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import HomeScreen from './components/HomeScreen';
import ShowPhoto from "./components/ShowPhoto";
import Feedback from "./components/Feedback";
import MacroPage from "./components/macroPage";
import LoginScreen from "./components/LoginScreen";
import Tabs from './components/Tabs';
import EnterInformation from "./components/EnterInformation";
import Onboarding from "./components/Onboarding";
import { FIREBASE_AUTH, FIREBASE_DATABASE } from './FirebaseConfig';
import SignUpScreen from "./components/SignUpScreen";
import { get, ref, update } from "firebase/database";
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import AsyncStorage from "@react-native-async-storage/async-storage";
import CameraPage from "./components/Camera";
const dayjs = require('dayjs')
const Stack = createNativeStackNavigator();
// App resets at 3 am or later
const resetHour = 3
export default function App() {
const auth = FIREBASE_AUTH;
const db = FIREBASE_DATABASE;
// AsyncStorage.clear()
// AsyncStorage.removeItem("@onboardingDone")
// AsyncStorage.removeItem('@todayMacros')
const [onboardingDone, setOnboardingDone] = useState(null);
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const checkIfReset = async () => {
// Get last logged day
let snapshot = await get(ref(db, auth.currentUser.uid))
let loggedDay = null
if (snapshot.exists()) {
loggedDay = snapshot.val()?.profile?.loggedDay;
loggedDay = loggedDay ? dayjs(JSON.parse(loggedDay)) : null
}
if (loggedDay) {
// Get current date
// let now = dayjs().set('date', 25)
let now = dayjs();
// If it's been a day + 3 hours or longer, reset & add new day to Firebase
let difference = now.diff(loggedDay, 'hour');
if (difference >= 24 + resetHour) {
await AsyncStorage.removeItem('@todayMacros');
let newDate = dayjs().set('hour', 0).set('minute', 0).set('second', 0).set('millisecond', 0);
await update(ref(db, auth.currentUser.uid + "/profile"), {loggedDay: JSON.stringify(newDate)})
console.log("Reset");
}
} }
useEffect(() => {
async function onAuthStateChanged(user) {
if (user) {
setUser(user);
console.log(user)
await checkIfReset();
}
else
setUser(false)
getOnboarding();
setLoading(false);
}
const subscriber = auth.onAuthStateChanged(onAuthStateChanged);
getOnboarding();
return subscriber;
}, [])
async function getOnboarding() {
let onboardingDone = await AsyncStorage.getItem("@onboardingDone");
if (onboardingDone == "true")
setOnboardingDone(true);
else
setOnboardingDone(false);
}
if (onboardingDone !== null && !loading && user !== null)
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<NavigationContainer>
<Stack.Navigator screenOptions={{ headerShown: false, gestureEnabled: false, }}>
{user
? <>
<Stack.Screen
name="Start"
component={Tabs}
/>
<Stack.Screen
name="ShowPhoto"
component={ShowPhoto}
options={{ gestureDirection: "horizontal" }}
/>
<Stack.Screen
name="Camera"
component={CameraPage}
options={{ gestureDirection: "vertical" }}
/>
<Stack.Screen
name="Feedback"
component={Feedback}
/>
<Stack.Screen
name="macroPage"
component={MacroPage}
/>
</>
: <>
{
!onboardingDone &&
<>
<Stack.Screen
name="Onboarding"
component={Onboarding}
/>
<Stack.Screen
name="EnterInformation"
component={EnterInformation}
/>
</>
}
<Stack.Screen
name="LoginScreen"
component={LoginScreen}
/>
<Stack.Screen
name="SignUpScreen"
component={SignUpScreen}
/>
</>
}
</Stack.Navigator>
</NavigationContainer>
</GestureHandlerRootView>
);
}