This repository has been archived by the owner on Sep 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorkoutLogScreen.js
166 lines (148 loc) · 5.71 KB
/
WorkoutLogScreen.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 React, { useState, useLayoutEffect, useCallback } from 'react';
import { SafeAreaView, View, Text, FlatList, TouchableOpacity, KeyboardAvoidingView } from 'react-native';
import { useFocusEffect, useNavigation } from '@react-navigation/native';
import Icon from 'react-native-vector-icons/FontAwesome';
import * as SQLite from 'expo-sqlite';
import { formatTimeSeconds, isoDateToLocale, isDateStringInLocaleFormat, localeDateStringToISO } from './utilities';
import styles from './styles';
const db = SQLite.openDatabase('workouts.db');
export const WorkoutLogScreen = () => {
const navigation = useNavigation();
const [savedWorkouts, setSavedWorkouts] = useState([]);
const updateDateInDatabase = (workoutId, newDate) => {
db.transaction(tx => {
tx.executeSql(
'UPDATE workouts SET date = ? WHERE id = ?',
[newDate, workoutId],
(_, result) => {
console.log(`Successfully updated date for workout id ${workoutId}`);
},
(_, err) => {
console.error(`Error updating date for workout id ${workoutId}: ${err}`);
}
);
});
};
const loadWorkouts = useCallback(() => {
try {
db.transaction(tx => {
tx.executeSql(
'CREATE TABLE IF NOT EXISTS workouts (id INTEGER PRIMARY KEY AUTOINCREMENT, date TEXT, exercises TEXT, workoutDuration INT);',
[],
() => console.log('Table created successfully')
);
tx.executeSql(
'SELECT * FROM workouts ORDER BY date DESC',
[],
(_, result) => {
const rows = result.rows;
let data = [];
for (let i = 0; i < rows.length; i++) {
let workout = rows.item(i);
let workoutDate = workout.date;
// If the date is not in ISO format, convert it
if (isDateStringInLocaleFormat(workout.date)) {
workoutDate = localeDateStringToISO(workout.date);
updateDateInDatabase(workout.id, workoutDate);
}
try {
// Try to parse the exercises string
const parsedExercises = JSON.parse(workout.exercises);
// If parsing was successful, add the workout to the data array
data.push({ id: workout.id, date: workout.date, exercises: parsedExercises, workoutDuration: workout.workoutDuration });
} catch (error) {
console.log('Failed to parse exercises string for workout id ' + workout.id);
// If parsing failed, you could add the workout with the unparsed exercises string
// or you could just skip this workout
// This example skips the workout:
continue;
}
}
setSavedWorkouts(data);
},
(_, err) => {
console.error(`Error: ${err}`);
},
);
});
} catch (err) {
console.error(err);
}
}, []);
useFocusEffect(loadWorkouts);
const deleteWorkout = async (workoutId) => {
try {
db.transaction((tx) => {
tx.executeSql('DELETE FROM workouts WHERE id = ?', [workoutId], () => {
console.log('Workout deleted:', workoutId);
loadWorkouts();
});
});
} catch (error) {
console.error('Error deleting workout:', error);
}
};
useLayoutEffect(() => {
navigation.setOptions({
headerRight: () => (
<TouchableOpacity
onPress={() => navigation.navigate('SignIn')}
>
<Icon name="user" size={37} color="#fff" style={{ marginRight: 10 }} />
</TouchableOpacity>
),
headerStyle: {
height: 91, // Set your desired height
backgroundColor: "#19162b",
},
});
}, [navigation]);
return (
<KeyboardAvoidingView
behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
style={styles.container}
>
<SafeAreaView style={styles.container}>
<FlatList
data={savedWorkouts}
renderItem={({ item }) => {
const { id, date, exercises, workoutDuration } = item;
console.log("Retrieved exercises in db", exercises, typeof (exercises));
return (
<View style={styles.workoutLogCard}>
<View style={styles.cardHeader}>
<Text style={styles.cardTitle}>Workout - {isoDateToLocale(date)}</Text>
</View>
<Text style={styles.cardItem}>Duration: {formatTimeSeconds(workoutDuration)}</Text>
{exercises.map((exercise, index) => (
<Text style={styles.cardItem} key={index}>
{exercise.name} {'\n'}
{exercise.sets !== null &&
Array.isArray(exercise.sets) &&
exercise.sets.map((setReps, index) => (
<Text key={index}>
{'\t'}Set {index + 1}: {setReps[0]} reps at {setReps[1]} lbs {'\n'}
</Text>
))}
</Text>
))}
{/* <View style={styles.deleteButtonContainer}>
<TouchableOpacity style={styles.deleteButtonCard} onPress={() => deleteWorkout(id)}>
<Text style={styles.deleteButton}>Delete</Text>
</TouchableOpacity>
</View> */}
<View
style={{
borderBottomColor: 'gray',
borderBottomWidth: 1,
}}
/>
</View>
);
}}
keyExtractor={(item) => item.id.toString()}
/>
</SafeAreaView>
</KeyboardAvoidingView>
);
};