2025-02-09 20:38:38 -05:00
|
|
|
// app/(tabs)/library/exercises.tsx
|
2025-02-16 23:53:28 -05:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { View, ScrollView, SectionList } from 'react-native';
|
2025-02-09 20:38:38 -05:00
|
|
|
import { Text } from '@/components/ui/text';
|
|
|
|
import { ExerciseCard } from '@/components/exercises/ExerciseCard';
|
|
|
|
import { FloatingActionButton } from '@/components/shared/FloatingActionButton';
|
|
|
|
import { NewExerciseSheet } from '@/components/library/NewExerciseSheet';
|
|
|
|
import { Dumbbell } from 'lucide-react-native';
|
2025-02-16 23:53:28 -05:00
|
|
|
import { Exercise, BaseExercise } from '@/types/exercise';
|
|
|
|
import { useSQLiteContext } from 'expo-sqlite';
|
|
|
|
import { ExerciseService } from '@/lib/db/services/ExerciseService';
|
2025-02-09 20:38:38 -05:00
|
|
|
|
|
|
|
export default function ExercisesScreen() {
|
2025-02-16 23:53:28 -05:00
|
|
|
const db = useSQLiteContext();
|
|
|
|
const exerciseService = React.useMemo(() => new ExerciseService(db), [db]);
|
|
|
|
|
|
|
|
const [exercises, setExercises] = useState<Exercise[]>([]);
|
2025-02-09 20:38:38 -05:00
|
|
|
const [showNewExercise, setShowNewExercise] = useState(false);
|
|
|
|
|
2025-02-16 23:53:28 -05:00
|
|
|
useEffect(() => {
|
|
|
|
loadExercises();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const loadExercises = async () => {
|
|
|
|
try {
|
|
|
|
const loadedExercises = await exerciseService.getAllExercises();
|
|
|
|
setExercises(loadedExercises);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error loading exercises:', error);
|
|
|
|
}
|
2025-02-09 20:38:38 -05:00
|
|
|
};
|
|
|
|
|
2025-02-16 23:53:28 -05:00
|
|
|
const handleAddExercise = async (exerciseData: BaseExercise) => {
|
|
|
|
try {
|
|
|
|
await exerciseService.createExercise({
|
|
|
|
...exerciseData,
|
|
|
|
created_at: Date.now(),
|
|
|
|
source: 'local'
|
|
|
|
});
|
|
|
|
await loadExercises();
|
|
|
|
setShowNewExercise(false);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error adding exercise:', error);
|
|
|
|
}
|
|
|
|
};
|
2025-02-09 20:38:38 -05:00
|
|
|
|
2025-02-16 23:53:28 -05:00
|
|
|
const handleDelete = async (id: string) => {
|
|
|
|
try {
|
|
|
|
await exerciseService.deleteExercise(id);
|
|
|
|
await loadExercises();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error deleting exercise:', error);
|
|
|
|
}
|
2025-02-09 20:38:38 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleExercisePress = (exerciseId: string) => {
|
|
|
|
console.log('Selected exercise:', exerciseId);
|
|
|
|
};
|
|
|
|
|
2025-02-16 23:53:28 -05:00
|
|
|
const alphabet = '#ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
|
|
|
|
|
2025-02-09 20:38:38 -05:00
|
|
|
return (
|
|
|
|
<View className="flex-1 bg-background">
|
2025-02-16 23:53:28 -05:00
|
|
|
<View className="absolute right-0 top-0 bottom-0 w-6 z-10 justify-center bg-transparent">
|
|
|
|
{alphabet.map((letter) => (
|
|
|
|
<Text
|
|
|
|
key={letter}
|
|
|
|
className="text-xs text-muted-foreground text-center"
|
|
|
|
onPress={() => {
|
|
|
|
// TODO: Implement scroll to section
|
|
|
|
console.log('Scroll to:', letter);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{letter}
|
|
|
|
</Text>
|
|
|
|
))}
|
|
|
|
</View>
|
2025-02-09 20:38:38 -05:00
|
|
|
|
2025-02-16 23:53:28 -05:00
|
|
|
<ScrollView className="flex-1 mr-6">
|
2025-02-09 20:38:38 -05:00
|
|
|
<View className="py-4">
|
|
|
|
<Text className="text-lg font-semibold mb-4 px-4">All Exercises</Text>
|
|
|
|
<View className="gap-3">
|
2025-02-12 12:55:51 -05:00
|
|
|
{exercises.map(exercise => (
|
2025-02-09 20:38:38 -05:00
|
|
|
<ExerciseCard
|
|
|
|
key={exercise.id}
|
|
|
|
{...exercise}
|
|
|
|
onPress={() => handleExercisePress(exercise.id)}
|
|
|
|
onDelete={() => handleDelete(exercise.id)}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</View>
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
|
|
|
|
<FloatingActionButton
|
|
|
|
icon={Dumbbell}
|
|
|
|
onPress={() => setShowNewExercise(true)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<NewExerciseSheet
|
|
|
|
isOpen={showNewExercise}
|
|
|
|
onClose={() => setShowNewExercise(false)}
|
|
|
|
onSubmit={handleAddExercise}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|