2025-03-13 14:02:36 -04:00
|
|
|
// app/(packs)/manage.tsx
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { View, ScrollView, StyleSheet, TouchableOpacity } from 'react-native';
|
|
|
|
import { router, Stack } from 'expo-router';
|
|
|
|
import { Text } from '@/components/ui/text';
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card';
|
|
|
|
import { Separator } from '@/components/ui/separator';
|
|
|
|
import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle } from '@/components/ui/alert-dialog';
|
|
|
|
import { POWRPackWithContent } from '@/types/powr-pack';
|
|
|
|
import { usePOWRPackService } from '@/components/DatabaseProvider';
|
|
|
|
import { formatDistanceToNow } from 'date-fns';
|
2025-03-17 20:20:16 -04:00
|
|
|
import { Trash2, PackageOpen, Plus, X } from 'lucide-react-native';
|
|
|
|
import { useIconColor } from '@/lib/theme/iconUtils';
|
|
|
|
import { useColorScheme } from '@/lib/theme/useColorScheme';
|
|
|
|
import { COLORS } from '@/lib/theme/colors';
|
2025-03-17 23:37:08 -04:00
|
|
|
import { FIXED_COLORS } from '@/lib/theme/colors';
|
2025-03-13 14:02:36 -04:00
|
|
|
|
|
|
|
export default function ManagePOWRPacksScreen() {
|
|
|
|
const powrPackService = usePOWRPackService();
|
|
|
|
const [packs, setPacks] = useState<POWRPackWithContent[]>([]);
|
|
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
|
|
const [selectedPackId, setSelectedPackId] = useState<string | null>(null);
|
|
|
|
const [showDeleteDialog, setShowDeleteDialog] = useState(false);
|
2025-03-17 20:20:16 -04:00
|
|
|
const { getIconProps } = useIconColor();
|
|
|
|
const { isDarkColorScheme } = useColorScheme();
|
2025-03-13 14:02:36 -04:00
|
|
|
|
|
|
|
// Load imported packs
|
|
|
|
useEffect(() => {
|
|
|
|
loadPacks();
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
// Function to load imported packs
|
|
|
|
const loadPacks = async () => {
|
|
|
|
setIsLoading(true);
|
|
|
|
try {
|
|
|
|
const importedPacks = await powrPackService.getImportedPacks();
|
|
|
|
setPacks(importedPacks);
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error loading packs:', error);
|
|
|
|
} finally {
|
|
|
|
setIsLoading(false);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle import button click
|
|
|
|
const handleImport = () => {
|
|
|
|
router.push('/(packs)/import');
|
|
|
|
};
|
|
|
|
|
2025-03-17 20:20:16 -04:00
|
|
|
// Handle close button press
|
|
|
|
const handleClose = () => {
|
|
|
|
router.back();
|
|
|
|
};
|
|
|
|
|
2025-03-13 14:02:36 -04:00
|
|
|
// Handle delete button click
|
|
|
|
const handleDeleteClick = (packId: string) => {
|
|
|
|
setSelectedPackId(packId);
|
|
|
|
setShowDeleteDialog(true);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Handle delete confirmation
|
|
|
|
const handleDeleteConfirm = async () => {
|
|
|
|
if (!selectedPackId) return;
|
2025-03-17 20:20:16 -04:00
|
|
|
|
2025-03-13 14:02:36 -04:00
|
|
|
try {
|
2025-03-17 20:20:16 -04:00
|
|
|
// Always delete everything (we no longer need the keepItems parameter)
|
|
|
|
await powrPackService.deletePack(selectedPackId, false);
|
2025-03-13 14:02:36 -04:00
|
|
|
// Refresh the list
|
|
|
|
loadPacks();
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Error deleting pack:', error);
|
|
|
|
} finally {
|
|
|
|
setShowDeleteDialog(false);
|
|
|
|
setSelectedPackId(null);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Format import date
|
|
|
|
const formatImportDate = (timestamp: number): string => {
|
|
|
|
return formatDistanceToNow(new Date(timestamp), { addSuffix: true });
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Stack.Screen
|
|
|
|
options={{
|
|
|
|
title: 'Manage POWR Packs',
|
|
|
|
headerShown: true,
|
2025-03-17 20:20:16 -04:00
|
|
|
// Add a close button for iOS
|
|
|
|
headerRight: () => (
|
|
|
|
<TouchableOpacity onPress={handleClose} style={styles.closeButton}>
|
|
|
|
<X {...getIconProps('primary')} size={24} />
|
|
|
|
</TouchableOpacity>
|
|
|
|
),
|
2025-03-13 14:02:36 -04:00
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<ScrollView contentContainerStyle={styles.scrollContent}>
|
2025-03-17 20:20:16 -04:00
|
|
|
{/* Import button */}
|
2025-03-13 14:02:36 -04:00
|
|
|
<Button
|
|
|
|
onPress={handleImport}
|
|
|
|
className="mb-4"
|
2025-03-17 20:20:16 -04:00
|
|
|
style={{ backgroundColor: COLORS.purple.DEFAULT }}
|
2025-03-13 14:02:36 -04:00
|
|
|
>
|
2025-03-17 20:20:16 -04:00
|
|
|
<Text style={{ color: '#fff', fontWeight: '500' }}>Import New Pack</Text>
|
2025-03-13 14:02:36 -04:00
|
|
|
</Button>
|
|
|
|
|
|
|
|
{/* No packs message */}
|
|
|
|
{!isLoading && packs.length === 0 && (
|
|
|
|
<Card>
|
|
|
|
<CardContent className="py-8 items-center">
|
2025-03-17 20:20:16 -04:00
|
|
|
<PackageOpen size={48} {...getIconProps('muted')} />
|
|
|
|
<Text className="text-lg font-medium mt-4 text-center text-foreground">No POWR Packs Imported</Text>
|
|
|
|
<Text className="text-center mt-2 text-muted-foreground">
|
2025-03-13 14:02:36 -04:00
|
|
|
Import workout packs shared by the community to get started.
|
|
|
|
</Text>
|
|
|
|
<Button
|
|
|
|
onPress={handleImport}
|
|
|
|
className="mt-4"
|
2025-03-17 20:20:16 -04:00
|
|
|
style={{ backgroundColor: COLORS.purple.DEFAULT }}
|
2025-03-13 14:02:36 -04:00
|
|
|
>
|
2025-03-17 20:20:16 -04:00
|
|
|
<Text style={{ color: '#fff', fontWeight: '500' }}>Import Your First Pack</Text>
|
2025-03-13 14:02:36 -04:00
|
|
|
</Button>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{/* Pack list */}
|
|
|
|
{packs.map((packWithContent) => {
|
|
|
|
const { pack, templates, exercises } = packWithContent;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Card key={pack.id} className="mb-4">
|
|
|
|
<CardHeader>
|
|
|
|
<View style={styles.cardHeaderContent}>
|
|
|
|
<View style={styles.cardHeaderText}>
|
|
|
|
<CardTitle>
|
2025-03-17 20:20:16 -04:00
|
|
|
<Text className="text-lg font-semibold text-foreground">{pack.title}</Text>
|
2025-03-13 14:02:36 -04:00
|
|
|
</CardTitle>
|
|
|
|
{pack.description && (
|
|
|
|
<CardDescription>
|
|
|
|
<Text className="text-muted-foreground">{pack.description}</Text>
|
|
|
|
</CardDescription>
|
|
|
|
)}
|
|
|
|
</View>
|
|
|
|
<TouchableOpacity
|
|
|
|
onPress={() => handleDeleteClick(pack.id)}
|
|
|
|
style={styles.deleteButton}
|
|
|
|
>
|
2025-03-17 20:20:16 -04:00
|
|
|
<Trash2 {...getIconProps('destructive')} size={20} />
|
2025-03-13 14:02:36 -04:00
|
|
|
</TouchableOpacity>
|
|
|
|
</View>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<View style={styles.statsRow}>
|
2025-03-17 20:20:16 -04:00
|
|
|
<Text className="text-muted-foreground">
|
2025-03-13 14:02:36 -04:00
|
|
|
{templates.length} template{templates.length !== 1 ? 's' : ''} • {exercises.length} exercise{exercises.length !== 1 ? 's' : ''}
|
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
<Separator className="my-2" />
|
2025-03-17 20:20:16 -04:00
|
|
|
<Text className="text-sm text-muted-foreground">
|
2025-03-13 14:02:36 -04:00
|
|
|
Imported {formatImportDate(pack.importDate)}
|
|
|
|
</Text>
|
|
|
|
</CardContent>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</ScrollView>
|
|
|
|
|
|
|
|
{/* Delete confirmation dialog */}
|
|
|
|
<AlertDialog open={showDeleteDialog} onOpenChange={setShowDeleteDialog}>
|
|
|
|
<AlertDialogContent>
|
|
|
|
<AlertDialogHeader>
|
2025-03-17 20:20:16 -04:00
|
|
|
<AlertDialogTitle>
|
2025-03-17 23:37:08 -04:00
|
|
|
<Text className="text-xl font-semibold text-foreground">Delete Pack</Text>
|
2025-03-17 20:20:16 -04:00
|
|
|
</AlertDialogTitle>
|
2025-03-13 14:02:36 -04:00
|
|
|
<AlertDialogDescription>
|
2025-03-17 23:37:08 -04:00
|
|
|
<Text className="text-muted-foreground">
|
|
|
|
This will remove the POWR Pack and all its associated templates and exercises from your library.
|
|
|
|
</Text>
|
2025-03-13 14:02:36 -04:00
|
|
|
</AlertDialogDescription>
|
|
|
|
</AlertDialogHeader>
|
2025-03-17 23:37:08 -04:00
|
|
|
<View className="flex-row justify-end gap-3">
|
|
|
|
<AlertDialogCancel asChild>
|
|
|
|
<Button variant="outline" className="mr-2">
|
|
|
|
<Text>Cancel</Text>
|
|
|
|
</Button>
|
2025-03-17 20:20:16 -04:00
|
|
|
</AlertDialogCancel>
|
2025-03-17 23:37:08 -04:00
|
|
|
<AlertDialogAction asChild>
|
|
|
|
<Button
|
|
|
|
variant="destructive"
|
|
|
|
onPress={handleDeleteConfirm}
|
|
|
|
style={{ backgroundColor: FIXED_COLORS.destructive }}
|
|
|
|
>
|
|
|
|
<Text style={{ color: '#FFFFFF' }}>Delete Pack</Text>
|
|
|
|
</Button>
|
2025-03-17 20:20:16 -04:00
|
|
|
</AlertDialogAction>
|
|
|
|
</View>
|
2025-03-13 14:02:36 -04:00
|
|
|
</AlertDialogContent>
|
|
|
|
</AlertDialog>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
padding: 16,
|
|
|
|
},
|
|
|
|
scrollContent: {
|
|
|
|
paddingBottom: 80,
|
|
|
|
},
|
|
|
|
cardHeaderContent: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
width: '100%',
|
|
|
|
},
|
|
|
|
cardHeaderText: {
|
|
|
|
flex: 1,
|
|
|
|
},
|
|
|
|
deleteButton: {
|
|
|
|
padding: 8,
|
|
|
|
},
|
|
|
|
statsRow: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'space-between',
|
|
|
|
alignItems: 'center',
|
|
|
|
marginBottom: 4,
|
|
|
|
},
|
|
|
|
dialogActions: {
|
|
|
|
flexDirection: 'row',
|
|
|
|
justifyContent: 'flex-end',
|
2025-03-17 20:20:16 -04:00
|
|
|
},
|
|
|
|
closeButton: {
|
|
|
|
padding: 8,
|
2025-03-13 14:02:36 -04:00
|
|
|
}
|
|
|
|
});
|