import React, { useState, useEffect, useCallback } from 'react'; import { View, Text, StyleSheet, TouchableOpacity, FlatList, Alert, StatusBar, Platform, SafeAreaView, TextInput, Modal } from 'react-native'; import { useNavigation } from '@react-navigation/native'; import MaterialIcons from 'react-native-vector-icons/MaterialIcons'; import { useTheme } from '../contexts/ThemeContext'; import { useTraktContext } from '../contexts/TraktContext'; import AsyncStorage from '@react-native-async-storage/async-storage'; const ANDROID_STATUSBAR_HEIGHT = StatusBar.currentHeight || 0; const PROFILE_STORAGE_KEY = 'user_profiles'; interface Profile { id: string; name: string; avatar?: string; isActive: boolean; createdAt: number; } const ProfilesScreen: React.FC = () => { const navigation = useNavigation(); const { currentTheme } = useTheme(); const { isAuthenticated, userProfile, refreshAuthStatus } = useTraktContext(); const [profiles, setProfiles] = useState([]); const [showAddModal, setShowAddModal] = useState(false); const [newProfileName, setNewProfileName] = useState(''); const [isLoading, setIsLoading] = useState(true); // Load profiles from AsyncStorage const loadProfiles = useCallback(async () => { try { setIsLoading(true); const storedProfiles = await AsyncStorage.getItem(PROFILE_STORAGE_KEY); if (storedProfiles) { setProfiles(JSON.parse(storedProfiles)); } else { // If no profiles exist, create a default one with the Trakt username const defaultProfile: Profile = { id: new Date().getTime().toString(), name: userProfile?.username || 'Default', isActive: true, createdAt: new Date().getTime() }; setProfiles([defaultProfile]); await AsyncStorage.setItem(PROFILE_STORAGE_KEY, JSON.stringify([defaultProfile])); } } catch (error) { console.error('Error loading profiles:', error); Alert.alert('Error', 'Failed to load profiles'); } finally { setIsLoading(false); } }, [userProfile]); // Add a focus listener to refresh authentication status useEffect(() => { const unsubscribe = navigation.addListener('focus', () => { // Refresh the auth status when the screen comes into focus refreshAuthStatus().then(() => { if (isAuthenticated) { loadProfiles(); } }); }); return unsubscribe; }, [navigation, refreshAuthStatus, isAuthenticated, loadProfiles]); // Save profiles to AsyncStorage const saveProfiles = useCallback(async (updatedProfiles: Profile[]) => { try { await AsyncStorage.setItem(PROFILE_STORAGE_KEY, JSON.stringify(updatedProfiles)); } catch (error) { console.error('Error saving profiles:', error); Alert.alert('Error', 'Failed to save profiles'); } }, []); useEffect(() => { // Only authenticated users can access profiles if (!isAuthenticated) { navigation.goBack(); return; } loadProfiles(); }, [isAuthenticated, loadProfiles, navigation]); const handleAddProfile = useCallback(() => { if (!newProfileName.trim()) { Alert.alert('Error', 'Please enter a profile name'); return; } const newProfile: Profile = { id: new Date().getTime().toString(), name: newProfileName.trim(), isActive: false, createdAt: new Date().getTime() }; const updatedProfiles = [...profiles, newProfile]; setProfiles(updatedProfiles); saveProfiles(updatedProfiles); setNewProfileName(''); setShowAddModal(false); }, [newProfileName, profiles, saveProfiles]); const handleSelectProfile = useCallback((id: string) => { const updatedProfiles = profiles.map(profile => ({ ...profile, isActive: profile.id === id })); setProfiles(updatedProfiles); saveProfiles(updatedProfiles); }, [profiles, saveProfiles]); const handleDeleteProfile = useCallback((id: string) => { // Prevent deleting the active profile const isActiveProfile = profiles.find(p => p.id === id)?.isActive; if (isActiveProfile) { Alert.alert('Error', 'Cannot delete the active profile. Switch to another profile first.'); return; } // Prevent deleting the last profile if (profiles.length <= 1) { Alert.alert('Error', 'Cannot delete the only profile'); return; } Alert.alert( 'Delete Profile', 'Are you sure you want to delete this profile? This action cannot be undone.', [ { text: 'Cancel', style: 'cancel' }, { text: 'Delete', style: 'destructive', onPress: () => { const updatedProfiles = profiles.filter(profile => profile.id !== id); setProfiles(updatedProfiles); saveProfiles(updatedProfiles); } } ] ); }, [profiles, saveProfiles]); const handleBack = () => { navigation.goBack(); }; const renderItem = ({ item }: { item: Profile }) => ( handleSelectProfile(item.id)} > {item.name} {item.isActive && ( Active )} {!item.isActive && ( handleDeleteProfile(item.id)} > )} ); return ( Profiles item.id} contentContainerStyle={styles.listContent} ListHeaderComponent={ MANAGE PROFILES } ListFooterComponent={ setShowAddModal(true)} > Add New Profile } /> {/* Modal for adding a new profile */} setShowAddModal(false)} > Create New Profile { setNewProfileName(''); setShowAddModal(false); }} > Cancel Create ); }; const styles = StyleSheet.create({ container: { flex: 1, }, header: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 16, paddingTop: Platform.OS === 'android' ? ANDROID_STATUSBAR_HEIGHT + 16 : 16, paddingBottom: 8, }, backButton: { padding: 8, marginRight: 16, borderRadius: 20, }, headerTitle: { fontSize: 20, fontWeight: '600', }, content: { flex: 1, paddingHorizontal: 16, }, sectionTitle: { fontSize: 13, fontWeight: '600', marginTop: 24, marginBottom: 12, letterSpacing: 0.5, }, listContent: { paddingBottom: 24, }, profileItem: { marginBottom: 12, }, profileContent: { flexDirection: 'row', alignItems: 'center', padding: 12, borderRadius: 12, borderWidth: 1, borderColor: 'rgba(255,255,255,0.1)', }, avatarContainer: { marginRight: 16, }, profileInfo: { flex: 1, }, profileName: { fontSize: 16, fontWeight: '500', }, activeLabel: { fontSize: 12, marginTop: 4, fontWeight: '500', }, deleteButton: { padding: 8, }, addButton: { flexDirection: 'row', alignItems: 'center', justifyContent: 'center', padding: 16, borderRadius: 12, marginTop: 12, }, addButtonText: { fontSize: 16, fontWeight: '500', marginLeft: 8, }, modalOverlay: { flex: 1, backgroundColor: 'rgba(0,0,0,0.7)', justifyContent: 'center', alignItems: 'center', padding: 24, }, modalContent: { width: '100%', borderRadius: 16, padding: 24, }, modalTitle: { fontSize: 18, fontWeight: '600', marginBottom: 24, textAlign: 'center', }, input: { width: '100%', height: 50, borderRadius: 8, paddingHorizontal: 16, marginBottom: 24, borderWidth: 1, }, modalButtons: { flexDirection: 'row', justifyContent: 'space-between', }, modalButton: { flex: 1, height: 44, borderRadius: 8, alignItems: 'center', justifyContent: 'center', }, cancelButton: { marginRight: 8, }, createButton: { marginLeft: 8, }, }); export default ProfilesScreen;