import React, { useEffect, useState } from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { onAuthStateChanged, User } from "firebase/auth"; import { SQLiteProvider, SQLiteDatabase } from 'expo-sqlite'; import { useAppTheme } from './hooks/colorScheme'; import Login from './app/Login'; import { TabNavigation } from './app/(tabs)/TabNavigation'; import { FIREBASE_AUTH } from './FirebaseConfig'; import Goals from './app/Goals'; import Onboarding from './app/Onboarding'; import getFoodData from './functions/SearchFood'; import Purchases from 'react-native-purchases'; import { clearMetricsCache } from './functions/assembleMetricsHistory'; import { assembleMetricsHistory } from './functions/assembleMetricsHistory'; const Stack = createNativeStackNavigator(); export default function App() { const insertTestData = async (db:SQLiteDatabase) => { try { await db.execAsync(` INSERT INTO foodhistory (name, qty, baseQty, cal, protein, carb, fat, day) VALUES ('apple', 1, 1, 95, 0, 25, 0, '2022-01-01'), ('banana', 1, 1, 105, 1, 27, 0, '2022-01-01'), ('orange', 1, 1, 62, 1, 15, 0, '2022-01-01'), ('pear', 1, 1, 101, 1, 27, 0, '2022-01-01'), ('strawberry', 1, 1, 4, 0, 1, 0, '2022-01-01'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-01'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-02'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-03'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-04'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-05'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-06'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-07'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-08'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-09'), ('acerola', 1, 1, 31, 1, 8, 0, '2022-01-10') `); } catch (error) { console.error("Error inserting test data: ", error); } } const deleteOldEntries = async (db: SQLiteDatabase) => { try { //console.log("attempting to delete old entries"); //const entries = await db.getAllAsync('SELECT * FROM foodhistory'); //console.log("Current entries:", entries); let oneWeekAgo = new Date(); oneWeekAgo.setDate(oneWeekAgo.getDate() - 7); const oneWeekAgoString = oneWeekAgo.toISOString().split("T")[0]; await db.execAsync(`DELETE FROM foodhistory WHERE day < '${oneWeekAgoString}'`); console.log("deleted old entries"); //const remainingEntries = await db.getAllAsync('SELECT * FROM foodhistory'); //console.log("Remaining entries:", remainingEntries); } catch (deleteError) { console.error("Error deleting old entries:", deleteError); } } const createDB = async (db:SQLiteDatabase) => { try { await db.execAsync(` CREATE TABLE IF NOT EXISTS foodhistory( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, qty INTEGER, baseQty INTEGER, cal INTEGER, protein INTEGER, carb INTEGER, fat INTEGER, iscustom INTEGER DEFAULT 0, day TEXT ); CREATE TABLE IF NOT EXISTS customfoods( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT UNIQUE, qty INTEGER, cal INTEGER, protein INTEGER, carb INTEGER, fat INTEGER ); `); await getFoodData("acerola"); //await insertTestData(db); await deleteOldEntries(db); } catch (error) { console.error("Error creating database:", error); } } const [user, setUser] = useState<User | null>(null); const colors = useAppTheme(); useEffect(() => { onAuthStateChanged(FIREBASE_AUTH, (user) => { setUser(user); }); clearMetricsCache(); // clear cache on app start if (FIREBASE_AUTH.currentUser) { assembleMetricsHistory(FIREBASE_AUTH.currentUser.uid); // regenerate cache on app start } }, []); if (!user) { return ( <NavigationContainer> <Stack.Navigator screenOptions={{ headerShown: false, headerTintColor: colors.accent, headerTitleStyle: { color: colors.accent } }}> <Stack.Screen name="Login" component={Login} options={{ gestureEnabled: false }}/> <Stack.Screen name="Onboarding" component={Onboarding} options={{ gestureEnabled: false }}/> </Stack.Navigator> </NavigationContainer> ); } return ( <SQLiteProvider databaseName="foodhistory.db" onInit={createDB}> <NavigationContainer> <Stack.Navigator screenOptions={{ headerTintColor: colors.accent, headerTitleStyle: { color: colors.accent } }}> <Stack.Screen name="Main" component={TabNavigation} options={{ headerShown: false }} /> <Stack.Screen name="Goals" component={Goals} options={{ headerShown: true }} /> </Stack.Navigator> </NavigationContainer> </SQLiteProvider> ); }