Advertisement
ValtyValt

Untitled

Mar 19th, 2024
418
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { View, Text, StyleSheet } from 'react-native'
  2. import React, { useEffect, useState } from 'react'
  3. import { Link } from 'expo-router'
  4. import databaseHelper from '../service/databasehelper.js'
  5. import AppContext from '../service/AppContext.js'
  6.  
  7. const styles = StyleSheet.create({
  8.   container: {
  9.     flex: 1,
  10.     display: 'flex',
  11.     alignItems: 'center',
  12.     justifyContent: 'center'
  13.   },
  14.   linkContainer: {
  15.     display: 'flex',
  16.     flexDirection: 'column',
  17.     justifyContent: 'space-around',
  18.     width: '50%',
  19.     margin: 100,
  20.   },
  21.   linkText: {
  22.     fontSize: 20,
  23.     textAlign: 'center',
  24.     fontWeight: 'bold'
  25.   }
  26. })
  27.  
  28.  
  29.  
  30. export default function Index() {
  31.  
  32.   //Github copilot gave me the knowledge to fix my error regarding using async functions in useEffect
  33.   //The previous error was that await functions returned a promise, and useEffect does not support promises/returning anything (except clean up)
  34.   //Now the initiliasing of the db is wrapped in an async function, this async function does not return anything.
  35.   //asyncInitDb is also local to the scope of UseEffect.
  36.   //The [] at the end of useEffect is an empty dependency array, this means that the useEffect will only run once, when the component mounts.
  37.   useEffect(() => {
  38.     async function asyncInitDb() {
  39.  
  40.       const dbInitVal = await databaseHelper.readOperatingValues("SELECT value FROM operatingValues WHERE name = 'dbInit'");
  41.  
  42.       try {
  43.         if (dbInitVal === 0) {
  44.           await databaseHelper.initDatabase()
  45.           databaseHelper.readDb()
  46.         }
  47.       } catch (TypeError) {
  48.         console.log("DB not initialised")
  49.         await databaseHelper.initDatabase()
  50.         databaseHelper.readDb()
  51.       }
  52.     }
  53.  
  54.       asyncInitDb();
  55.   }, []);
  56.  
  57.   const [template1, setTemplate1] = useState([]);
  58.   const [template2, setTemplate2] = useState([]);
  59.   const [isLoading, setIsLoading] = useState(true);  //Prevents data from rendering before data is fetched from the database
  60.  
  61.   useEffect(() => {
  62.       /**
  63.        * Inserts workout templates into the database and reads all templates.
  64.        * @returns {Promise<void>}
  65.        */
  66.       async function testTemplatesAndRead() {
  67.           const templatesInit = await databaseHelper.readOperatingValues("SELECT value FROM operatingValues WHERE name = 'templatesInit'");
  68.  
  69.           if (templatesInit[0].value === 0) {
  70.  
  71.               //Create template the "pull" template.
  72.               await databaseHelper.insertWorkoutTemplate(1, "Push", 1, 0, 20, 1, 1);
  73.               await databaseHelper.insertWorkoutTemplate(1, "Push", 1, 0, 15, 2, 1);
  74.               await databaseHelper.insertWorkoutTemplate(1, "Push", 1, 0, 10, 3, 1);
  75.  
  76.               await databaseHelper.insertWorkoutTemplate(1, "Push", 7, 55, 8, 1, 1);
  77.               await databaseHelper.insertWorkoutTemplate(1, "Push", 7, 55, 6, 2, 1);
  78.               await databaseHelper.insertWorkoutTemplate(1, "Push", 7, 55, 4, 3, 1);
  79.  
  80.               await databaseHelper.insertWorkoutTemplate(1, "Push", 9, 25, 8, 1, 1);
  81.               await databaseHelper.insertWorkoutTemplate(1, "Push", 9, 25, 6, 2, 1);
  82.               await databaseHelper.insertWorkoutTemplate(1, "Push", 9, 25, 4, 3, 1);
  83.  
  84.               await databaseHelper.insertWorkoutTemplate(1, "Push", 14, 25, 10, 1, 1);
  85.               await databaseHelper.insertWorkoutTemplate(1, "Push", 14, 25, 8, 2, 1);
  86.               await databaseHelper.insertWorkoutTemplate(1, "Push", 14, 25, 6, 3, 1);
  87.  
  88.               await databaseHelper.insertWorkoutTemplate(1, "Push", 12, 30, 8, 1, 1);
  89.               await databaseHelper.insertWorkoutTemplate(1, "Push", 12, 30, 6, 2, 1);
  90.  
  91.               //The "push" template.
  92.               await databaseHelper.insertWorkoutTemplate(2, "Pull", 4, 12, 8, 1, 1);
  93.               await databaseHelper.insertWorkoutTemplate(2, "Pull", 4, 12, 6, 2, 1);
  94.  
  95.               await databaseHelper.insertWorkoutTemplate(2, "Pull", 6, 120, 8, 1, 1);
  96.               await databaseHelper.insertWorkoutTemplate(2, "Pull", 6, 120, 6, 2, 1);
  97.  
  98.               await databaseHelper.insertWorkoutTemplate(2, "Pull", 11, 0, 8, 1, 1);
  99.               await databaseHelper.insertWorkoutTemplate(2, "Pull", 11, 0, 6, 2, 1);
  100.  
  101.               await databaseHelper.readOperatingValues("UPDATE operatingValues SET value = 1 WHERE name = 'templatesInit'");
  102.           }
  103.  
  104.           const tempTemplate1 = await databaseHelper.readTemplates("SELECT * FROM Templates INNER JOIN Workouts ON Templates.workoutId = Workouts.workoutId WHERE Templates.templateId = 1");
  105.           console.log("This is template1" + tempTemplate1);
  106.           setTemplate1(tempTemplate1);
  107.  
  108.           const tempTemplate2 = await databaseHelper.readTemplates("SELECT * FROM Templates INNER JOIN Workouts ON Templates.workoutId = Workouts.workoutId WHERE Templates.templateId = 2");
  109.           console.log("This is template2" + tempTemplate2);
  110.           setTemplate2(tempTemplate2);
  111.  
  112.           setIsLoading(false); //Indicates that the data has been fetched from the database
  113.      
  114.   }
  115.  
  116.   testTemplatesAndRead();
  117. }, []);
  118.  
  119.   return (
  120.     <AppContext.Provider value={{ template1, template2, isLoading }}>
  121.       <View style={styles.container}>
  122.  
  123.         <View style={styles.linkContainer}>
  124.           <Link push href="/home">
  125.             <Text style={styles.linkText}>Home</Text>
  126.           </Link>
  127.         </View>
  128.  
  129.         <View style={styles.linkContainer}>
  130.           <Link push href="/trainer">
  131.             <Text style={styles.linkText}>Trainer</Text>
  132.           </Link>
  133.         </View>
  134.        
  135.       </View>
  136.     </AppContext.Provider>
  137.   )
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement