Advertisement
ValtyValt

Untitled

Mar 19th, 2024
621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {/*
  2. Todo:
  3. Create a stylesheet file
  4. Look into routing and understand it better
  5. Look into how to better implement multiple modals
  6. Look into and utilise SQLite
  7. */}
  8.  
  9. import {
  10.     View,
  11.     Text,
  12.     StyleSheet,
  13.     Pressable,
  14.     Modal,
  15.     Image,
  16.     FlatList, } from 'react-native'
  17. import React, { useContext, useEffect, useState } from 'react'
  18. import { SafeAreaView } from 'react-native-safe-area-context'
  19. import { TouchableOpacity } from 'react-native-gesture-handler'
  20. import { router } from 'expo-router'
  21. import AppContext from '../service/AppContext.js'
  22.  
  23. const styles = StyleSheet.create({
  24.  
  25.         // These are the styles for all of the components in the home screen
  26.         templateContainer: {
  27.                 flexDirection: 'row',
  28.                 display: 'flex',
  29.                 alignItems: 'flex-start',
  30.                 justifyContent: 'center',
  31.                 backgroundColor: 'red',
  32.                 borderColor: 'black',
  33.                 borderWidth: 5,
  34.                 width: '90%',
  35.                 height: '80%',
  36.                 marginTop: 150,
  37.                 flexWrap: 'wrap',
  38.                 gap: 10,
  39.                 paddingTop: 10,
  40.         },
  41.         templateBox: {
  42.                 display: 'flex',
  43.                 alignItems: 'flex-start',
  44.                 justifyContent: 'flex-start',
  45.                 backgroundColor: 'green',
  46.                 borderColor: 'black',
  47.                 borderWidth: 5,
  48.                 width: '45%',
  49.                 height: '25%',
  50.                 margin: 1,
  51.         },
  52.         headerText: {
  53.                 fontSize: 30,
  54.                 textAlign: 'center',
  55.                 fontWeight: 'bold'
  56.         },
  57.         templateText: {
  58.                 fontSize: 20,
  59.                 textAlign: 'center',
  60.                 fontWeight: 'bold'
  61.         },
  62.         modalView: {
  63.             display: 'flex',
  64.             marginTop: "5%",
  65.             justifyContent: "center",
  66.             alignItems: "center",
  67.             backgroundColor: "orange",
  68.             borderRadius: 5,
  69.             width: "80%",
  70.             height: "auto",
  71.         },
  72.         pressableBox: {
  73.             display: 'flex',
  74.             alignItems: 'center',
  75.             justifyContent: 'center',
  76.             width: "133px",
  77.             height: "133px",
  78.         }
  79.  
  80. })
  81.  
  82. export default function home() {
  83.    
  84.     // Initialising the modal states
  85.     // This could possibly be done in a better way and needs to be revisited
  86.     const [modalVisible1, setModalVisible1] = useState(false);
  87.     const [modalVisible2, setModalVisible2] = useState(false);
  88.     const [modalVisible3, setModalVisible3] = useState(false);
  89.     const [modalVisible4, setModalVisible4] = useState(false);
  90.     const context = useContext(AppContext);
  91.     const { template1 } = context ?? {};
  92.     const { template2 } = context ?? {};
  93.     const { isLoading } = context ?? {};
  94.  
  95.     return (
  96.         //Use SafeAreaView to avoid the notch on the most new iphone
  97.         <SafeAreaView style={{
  98.                 display: 'flex',
  99.                 alignItems: 'center',
  100.                 justifyContent: 'center'
  101.                 }}>
  102.  
  103.                 <View style={styles.templateContainer}>
  104.  
  105.                     {/*
  106.                     These are the pressable boxes for the templates.
  107.                     They are all coupled with their associated modal.
  108.                     */}
  109.                        
  110.                         <Pressable style={styles.templateBox} onPress={() => setModalVisible1(true)}>
  111.                             {/*
  112.                             !isLoading only shows the text component once isLoading is false
  113.                             template1 {data from template1}
  114.                             [0]? {the first element in the array, other elements are not needed, only templateName is needed,
  115.                             ? is used to prevent an error if the array is empty}
  116.                             templateName {the name of the template}
  117.                             Will need to reconsider how to do this when there are multiple templates
  118.                              */}
  119.                            {!isLoading && <Text style={styles.templateText}>{template1[0]?.templateName}</Text>}
  120.                         {template2 ? (
  121.                             <FlatList
  122.                                 data={template1}
  123.                                 renderItem={({ item }) => (
  124.                                     <View>
  125.                                         <Text>{item.workoutName}</Text>
  126.  
  127.                                     </View>
  128.                                 )}
  129.                                 keyExtractor={(item, index) => index}
  130.                             />
  131.                         ) : (
  132.                             <Text>Loading...</Text>
  133.                         )}
  134.                         </Pressable>
  135.  
  136.                         <Pressable style={styles.templateBox} onPress={() => setModalVisible2(true)}>
  137.                                 {!isLoading && <Text style={styles.templateText}>{template2[0]?.templateName}</Text>}
  138.                             {template2 ? (
  139.                                 <FlatList
  140.                                     data={template2}
  141.                                     renderItem={({ item }) => (
  142.                                         <View>
  143.                                             <Text>{item.workoutName}</Text>
  144.  
  145.                                         </View>
  146.                                     )}
  147.                                     keyExtractor={(item, index) => index}
  148.                                 />
  149.                             ) : (
  150.                                 <Text>Loading...</Text>
  151.                             )}
  152.                         </Pressable>
  153.  
  154.                         <Pressable style={styles.templateBox} onPress={() => setModalVisible3(true)}>
  155.                                 <Text style={styles.templateText}>Template 3</Text>
  156.                         </Pressable>
  157.  
  158.                         <Pressable style={styles.templateBox} onPress={() => setModalVisible4(true)}>
  159.                                 <Text style={styles.templateText}>Template 4</Text>
  160.                         </Pressable>
  161.  
  162.                 </View>
  163.                    
  164.                     {/*
  165.                     These are the modals for the templates.
  166.                     I need to look into a more efficient and clean way to code this part.
  167.                     For now this is the solution.
  168.                     */}
  169.  
  170.                     {/*
  171.                     onRequestClose only has effect on Andriod devices.
  172.                     This is to handle the hardware back button.
  173.                     This has no effect on iOS.
  174.                     */}
  175.                     <Modal
  176.                         animationType="slide"
  177.                         transparent={true}
  178.                         visible={modalVisible1}
  179.                         onRequestClose={() => {
  180.                         Alert.alert("Modal has been closed.");
  181.                         setModalVisible1(!modalVisible1);
  182.                         }}
  183.                     >
  184.                         <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
  185.                             <View style={styles.modalView}>
  186.  
  187.                                 <View style={
  188.                                     {
  189.                                         flexDirection: 'row',
  190.                                         gap: 75,
  191.                                         display: "flex",
  192.                                         width: "100%",
  193.                                         height: "auto",
  194.                                         marginBottom: "5%",
  195.                                         marginTop: "5%",
  196.                                         }}>
  197.  
  198.                                     <Pressable onPress={() => setModalVisible1(false)}>
  199.                                         <Image source={
  200.                                             require("../assets/images/x_icon.png")}
  201.                                             style={{height: 25, width: 25}}/>
  202.                                     </Pressable>
  203.  
  204.                                     <Text style={styles.templateText}>{template1[0]?.templateName}</Text>
  205.  
  206.                                        
  207.                                     <TouchableOpacity
  208.                                     onPress={() => {
  209.                                         setModalVisible1(false);
  210.                                         console.log("This is template1" + template1);
  211.                                         router.push({
  212.                                             pathname: '/workout/[template]',
  213.                                             params: { templateId: 1 }
  214.                                         });
  215.                                     }}>
  216.  
  217.                                         <Text style={styles.templateText}>Start</Text>
  218.  
  219.                                     </TouchableOpacity>
  220.                                    
  221.                                 </View>
  222.  
  223.                             </View>
  224.                         </View>
  225.                     </Modal>
  226.  
  227.                     <Modal
  228.                         animationType="slide"
  229.                         transparent={true}
  230.                         visible={modalVisible2}
  231.                         onRequestClose={() => {
  232.                         Alert.alert("Modal has been closed.");
  233.                         setModalVisible2(!modalVisible2);
  234.                         }}
  235.                     >
  236.                         <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
  237.                             <View style={styles.modalView}>
  238.  
  239.                                 <View style={
  240.                                     {
  241.                                         flexDirection: 'row',
  242.                                         gap: 75,
  243.                                         display: "flex",
  244.                                         width: "100%",
  245.                                         height: "auto",
  246.                                         marginBottom: "5%",
  247.                                         marginTop: "5%",
  248.                                         }}>
  249.  
  250.                                     <Pressable onPress={() => setModalVisible2(false)}>
  251.                                         <Image source={
  252.                                             require("../assets/images/x_icon.png")}
  253.                                             style={{height: 25, width: 25}}/>
  254.                                     </Pressable>
  255.  
  256.                                     <Text style={styles.templateText}>{template2[0]?.templateName}</Text>
  257.  
  258.                                     <Text style={styles.templateText}>Start</Text>
  259.                                    
  260.                                 </View>
  261.  
  262.                             </View>
  263.                         </View>
  264.                     </Modal>
  265.  
  266.                     <Modal
  267.                         animationType="slide"
  268.                         transparent={true}
  269.                         visible={modalVisible3}
  270.                         onRequestClose={() => {
  271.                         Alert.alert("Modal has been closed.");
  272.                         setModalVisible3(!modalVisible3);
  273.                         }}
  274.                     >
  275.                         <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
  276.                             <View style={styles.modalView}>
  277.  
  278.                                 <View style={
  279.                                     {
  280.                                         flexDirection: 'row',
  281.                                         gap: 75,
  282.                                         display: "flex",
  283.                                         width: "100%",
  284.                                         height: "auto",
  285.                                         marginBottom: "5%",
  286.                                         marginTop: "5%",
  287.                                         }}>
  288.  
  289.                                     <Pressable onPress={() => setModalVisible3(false)}>
  290.                                         <Image source={
  291.                                             require("../assets/images/x_icon.png")}
  292.                                             style={{height: 25, width: 25}}/>
  293.                                     </Pressable>
  294.  
  295.                                     <Text style={styles.templateText}>Template 3</Text>
  296.  
  297.                                     <Text style={styles.templateText}>Start</Text>
  298.                                    
  299.                                 </View>
  300.  
  301.                             </View>
  302.                         </View>
  303.                     </Modal>
  304.  
  305.                     <Modal
  306.                         animationType="slide"
  307.                         transparent={true}
  308.                         visible={modalVisible4}
  309.                         onRequestClose={() => {
  310.                         Alert.alert("Modal has been closed.");
  311.                         setModalVisible4(!modalVisible4);
  312.                         }}
  313.                     >
  314.                         <View style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
  315.                             <View style={styles.modalView}>
  316.  
  317.                                 <View style={
  318.                                     {
  319.                                         flexDirection: 'row',
  320.                                         gap: 75,
  321.                                         display: "flex",
  322.                                         width: "100%",
  323.                                         height: "auto",
  324.                                         marginBottom: "5%",
  325.                                         marginTop: "5%",
  326.                                         }}>
  327.  
  328.                                     <Pressable onPress={() => setModalVisible4(false)}>
  329.                                         <Image source={
  330.                                             require("../assets/images/x_icon.png")}
  331.                                             style={{height: 25, width: 25}}/>
  332.                                     </Pressable>
  333.  
  334.                                     <Text style={styles.templateText}>Template 4</Text>
  335.  
  336.                                     <Text style={styles.templateText}>Start</Text>
  337.                                    
  338.                                 </View>
  339.  
  340.                             </View>
  341.                         </View>
  342.                     </Modal>
  343.         </SafeAreaView>
  344.     )
  345. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement