import React, { useEffect, useState } from 'react'; import { View, Text, StyleSheet, FlatList, TouchableOpacity, RefreshControl } from 'react-native'; import { useRouter } from 'expo-router'; import { withObservables } from '@nozbe/watermelondb/react'; import { database } from '../../src/db'; import Challenge from '../../src/db/models/Challenge'; import { syncChallenges } from '../../src/db/sync'; import { Colors } from '@/constants/theme'; const ChallengeItem = ({ challenge, onPress }: { challenge: Challenge; onPress: () => void }) => ( {challenge.name} {challenge.difficulty} {challenge.description} {challenge.durationDays} Days ); const getDifficultyColor = (difficulty: string) => { switch (difficulty?.toLowerCase()) { case 'beginner': return '#4CAF50'; case 'moderate': return '#FF9800'; case 'hard': return '#F44336'; case 'extreme': return '#9C27B0'; default: return '#757575'; } }; const ChallengeList = ({ challenges }: { challenges: Challenge[] }) => { const router = useRouter(); const [refreshing, setRefreshing] = useState(false); const onRefresh = async () => { setRefreshing(true); await syncChallenges(); setRefreshing(false); }; useEffect(() => { // Initial sync syncChallenges(); }, []); return ( Explore Challenges item.id} renderItem={({ item }) => ( router.push(`/challenge/${item.id}`)} /> )} contentContainerStyle={styles.listContent} refreshControl={ } ListEmptyComponent={ No challenges found. Pull to refresh to load challenges. } /> ); }; const enhance = withObservables([], () => ({ challenges: database.get('challenges').query(), })); export default enhance(ChallengeList); const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#f5f5f5', paddingTop: 60, // Status bar spacing }, title: { fontSize: 28, fontWeight: 'bold', paddingHorizontal: 20, marginBottom: 16, color: '#333', }, listContent: { paddingHorizontal: 20, paddingBottom: 20, }, card: { backgroundColor: 'white', borderRadius: 12, padding: 16, marginBottom: 16, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 3, }, cardHeader: { flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', marginBottom: 8, }, cardTitle: { fontSize: 18, fontWeight: '600', color: '#333', flex: 1, marginRight: 8, }, badge: { paddingHorizontal: 8, paddingVertical: 4, borderRadius: 4, }, badgeText: { color: 'white', fontSize: 12, fontWeight: 'bold', textTransform: 'capitalize', }, cardDescription: { fontSize: 14, color: '#666', marginBottom: 12, lineHeight: 20, }, cardFooter: { flexDirection: 'row', alignItems: 'center', }, durationText: { fontSize: 14, color: '#888', fontWeight: '500', }, emptyContainer: { padding: 40, alignItems: 'center', }, emptyText: { fontSize: 18, color: '#666', marginBottom: 8, }, emptySubText: { fontSize: 14, color: '#999', }, });