import React, { useCallback, useState, useEffect } from 'react'; import { View, Text, StyleSheet, TouchableOpacity, Switch, ScrollView, SafeAreaView, StatusBar, Platform, useColorScheme, Animated, Dimensions } from 'react-native'; import { useSettings } from '../hooks/useSettings'; import { LinearGradient } from 'expo-linear-gradient'; import { useTranslation } from 'react-i18next'; import { useNavigation, useFocusEffect } from '@react-navigation/native'; import { NavigationProp } from '@react-navigation/native'; import { MaterialIcons, Feather } from '@expo/vector-icons'; import { useTheme } from '../contexts/ThemeContext'; import { RootStackParamList } from '../navigation/AppNavigator'; const ANDROID_STATUSBAR_HEIGHT = StatusBar.currentHeight || 0; interface SettingsCardProps { children: React.ReactNode; isDarkMode: boolean; colors: any; } const SettingsCard: React.FC = ({ children, isDarkMode, colors }) => ( {children} ); // Restrict icon names to those available in MaterialIcons type MaterialIconName = React.ComponentProps['name']; interface SettingItemProps { title: string; description?: string; icon: MaterialIconName; renderControl: () => React.ReactNode; isLast?: boolean; onPress?: () => void; isDarkMode: boolean; colors: any; } const SettingItem: React.FC = ({ title, description, icon, renderControl, isLast = false, onPress, isDarkMode, colors }) => { const isTabletDevice = Platform.OS !== 'web' && (Dimensions.get('window').width >= 768); return ( {title} {description && ( {description} )} {renderControl()} ); }; const SectionHeader: React.FC<{ title: string; isDarkMode: boolean; colors: any }> = ({ title, isDarkMode, colors }) => ( {title} ); const HomeScreenSettings: React.FC = () => { const { t } = useTranslation(); const { settings, updateSetting } = useSettings(); const systemColorScheme = useColorScheme(); const { currentTheme } = useTheme(); const colors = currentTheme.colors; const isDarkMode = systemColorScheme === 'dark' || settings.enableDarkMode; const navigation = useNavigation>(); const [showSavedIndicator, setShowSavedIndicator] = useState(false); const fadeAnim = React.useRef(new Animated.Value(0)).current; const isTabletDevice = Platform.OS !== 'web' && (Dimensions.get('window').width >= 768); // Prevent iOS entrance flicker by restoring a non-translucent StatusBar useFocusEffect( React.useCallback(() => { try { StatusBar.setTranslucent(false); StatusBar.setBackgroundColor(isDarkMode ? colors.darkBackground : '#F2F2F7'); StatusBar.setBarStyle(isDarkMode ? 'light-content' : 'dark-content'); if (Platform.OS === 'ios') { StatusBar.setHidden(false); } } catch { } return () => { }; }, [isDarkMode, colors.darkBackground]) ); const handleBack = useCallback(() => { navigation.goBack(); }, [navigation]); // Fade in/out animation for the "Changes saved" indicator useEffect(() => { if (showSavedIndicator) { Animated.sequence([ Animated.timing(fadeAnim, { toValue: 1, duration: 300, useNativeDriver: true }), Animated.delay(1000), Animated.timing(fadeAnim, { toValue: 0, duration: 300, useNativeDriver: true }) ]).start(() => setShowSavedIndicator(false)); } }, [showSavedIndicator, fadeAnim]); const handleUpdateSetting = useCallback(( key: K, value: typeof settings[K] ) => { updateSetting(key, value); setShowSavedIndicator(true); }, [updateSetting]); // Ensure carousel is the default hero layout on tablets for all users useEffect(() => { try { if (isTabletDevice && settings.heroStyle !== 'carousel') { updateSetting('heroStyle', 'carousel' as any); } } catch { } }, [isTabletDevice, settings.heroStyle, updateSetting]); const CustomSwitch = ({ value, onValueChange }: { value: boolean, onValueChange: (value: boolean) => void }) => ( ); // Radio button component for content source selection const RadioOption = ({ selected, onPress, label }: { selected: boolean, onPress: () => void, label: string }) => ( {selected && } {label} ); // Compact segmented control for nicer toggles const SegmentedControl = ({ options, value, onChange }: { options: { label: string; value: string }[]; value: string; onChange: (val: string) => void; }) => ( {options.map((opt, idx) => { const selected = value === opt.value; return ( onChange(opt.value)} activeOpacity={0.85} style={[ styles.segment, idx === 0 && styles.segmentFirst, idx === options.length - 1 && styles.segmentLast, selected && { backgroundColor: colors.primary }, ]} > {opt.label} ); })} ); // Format selected catalogs text const getSelectedCatalogsText = useCallback(() => { if (!settings.selectedHeroCatalogs || settings.selectedHeroCatalogs.length === 0) { return t("home_screen.all_catalogs"); } else { return `${settings.selectedHeroCatalogs.length} ${t("home_screen.selected")}`; } }, [settings.selectedHeroCatalogs, t]); const ChevronRight = () => ( ); return ( navigation.goBack()} style={styles.backButton}> {t('settings.title')} {/* Empty for now, but ready for future actions */} {t('home_screen.title')} {/* Saved indicator */} {t('home_screen.changes_applied')} ( handleUpdateSetting('showHeroSection', value)} /> )} /> ( handleUpdateSetting('showThisWeekSection', value)} /> )} /> {settings.showHeroSection && ( navigation.navigate('HeroCatalogs')} isLast={true} /> )} {settings.showHeroSection && ( <> {!isTabletDevice && ( {t('home_screen.hero_layout')} handleUpdateSetting('heroStyle', val as any)} /> {t('home_screen.layout_desc')} )} {t('home_screen.featured_source')} {t('home_screen.using_catalogs')} navigation.navigate('HeroCatalogs')} style={[styles.manageLink, { backgroundColor: isDarkMode ? colors.elevation1 : 'rgba(0,0,0,0.04)' }]} activeOpacity={0.8} > {t('home_screen.manage_selected_catalogs')} {settings.heroStyle === 'carousel' && ( ( handleUpdateSetting('enableHomeHeroBackground', value)} /> )} /> {t('home_screen.performance_note')} )} )} {t('home_screen.posters')} {t('home_screen.show_titles')} handleUpdateSetting('showPosterTitles', value)} /> {t('home_screen.poster_size')} handleUpdateSetting('posterSize', val as any)} /> {t('home_screen.poster_corners')} handleUpdateSetting('posterBorderRadius', Number(val) as any)} /> {t('home_screen.about_desc')} ); }; const styles = StyleSheet.create({ container: { flex: 1, }, header: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', paddingHorizontal: 16, paddingTop: Platform.OS === 'android' ? ANDROID_STATUSBAR_HEIGHT + 8 : 8, }, backButton: { flexDirection: 'row', alignItems: 'center', padding: 8, }, backText: { fontSize: 17, marginLeft: 8, }, headerActions: { flexDirection: 'row', alignItems: 'center', }, headerButton: { padding: 8, marginLeft: 8, }, headerTitle: { fontSize: 34, fontWeight: 'bold', paddingHorizontal: 16, marginBottom: 24, }, scrollView: { flex: 1, }, scrollContent: { paddingBottom: 32, }, sectionHeader: { paddingHorizontal: 16, marginHorizontal: 16, paddingTop: 20, paddingBottom: 8, }, sectionHeaderText: { fontSize: 12, fontWeight: '600', letterSpacing: 0.8, }, card: { marginHorizontal: 16, marginTop: 12, marginBottom: 12, borderRadius: 12, overflow: 'hidden', shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.1, shadowRadius: 4, elevation: 2, }, settingItem: { flexDirection: 'row', alignItems: 'center', paddingVertical: 12, paddingHorizontal: 16, borderBottomWidth: 0.5, minHeight: 44, }, settingItemBorder: { // Border styling handled directly in the component with borderBottomWidth }, settingIconContainer: { marginRight: 12, width: 24, height: 24, alignItems: 'center', justifyContent: 'center', }, settingContent: { flex: 1, marginRight: 8, }, settingTitleRow: { flexDirection: 'column', justifyContent: 'center', gap: 4, }, settingTitle: { fontSize: 16, fontWeight: '500', }, settingDescription: { fontSize: 14, opacity: 0.7, }, settingControl: { justifyContent: 'center', alignItems: 'center', paddingLeft: 12, }, settingInlineNote: { fontSize: 12, opacity: 0.7, marginTop: 8, marginBottom: 8, textAlign: 'center', alignSelf: 'center', width: '100%', paddingHorizontal: 16, }, radioCardContainer: { marginHorizontal: 16, marginVertical: 8, borderRadius: 12, backgroundColor: 'rgba(255,255,255,0.05)', overflow: 'hidden', }, radioOption: { padding: 16, }, radioContainer: { flexDirection: 'row', alignItems: 'center', }, radio: { width: 20, height: 20, borderRadius: 10, borderWidth: 2, marginRight: 10, justifyContent: 'center', alignItems: 'center', }, radioInner: { width: 10, height: 10, borderRadius: 5, }, radioLabel: { fontSize: 16, fontWeight: '500', }, radioDescription: { paddingHorizontal: 16, paddingBottom: 16, paddingTop: 0, }, radioDescriptionText: { fontSize: 14, lineHeight: 20, }, infoCard: { marginHorizontal: 16, marginTop: 8, padding: 16, borderRadius: 12, }, infoText: { fontSize: 14, lineHeight: 20, }, segmentContainer: { flexDirection: 'row', borderRadius: 10, padding: 4, gap: 6, marginTop: 8, }, segment: { flex: 1, borderRadius: 8, paddingVertical: 10, alignItems: 'center', justifyContent: 'center', }, segmentFirst: { }, segmentLast: { }, segmentCard: { marginHorizontal: 16, marginTop: 12, marginBottom: 12, padding: 16, borderRadius: 12, backgroundColor: 'rgba(255,255,255,0.05)' }, segmentTitle: { fontSize: 12, fontWeight: '700', letterSpacing: 0.6, opacity: 0.9, }, segmentHint: { marginTop: 8, fontSize: 12, opacity: 0.7, }, manageLink: { marginTop: 10, paddingHorizontal: 12, paddingVertical: 10, borderRadius: 10, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, settingsRow: { paddingHorizontal: 16, paddingVertical: 12, gap: 8, }, settingsRowInline: { paddingHorizontal: 16, paddingVertical: 12, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between', }, rowLabel: { fontSize: 14, fontWeight: '600', marginBottom: 4, opacity: 0.9, }, cardHeader: { fontSize: 12, fontWeight: '700', letterSpacing: 0.6, paddingHorizontal: 16, paddingTop: 12, opacity: 0.9, }, savedIndicator: { position: 'absolute', top: Platform.OS === 'android' ? (StatusBar.currentHeight || 0) + 60 : 90, alignSelf: 'center', paddingHorizontal: 16, paddingVertical: 8, borderRadius: 24, flexDirection: 'row', alignItems: 'center', justifyContent: 'center', zIndex: 1000, elevation: 5, shadowColor: '#000', shadowOffset: { width: 0, height: 2 }, shadowOpacity: 0.25, shadowRadius: 3.84, }, savedIndicatorText: { color: '#FFFFFF', marginLeft: 6, fontWeight: '600', }, }); export default HomeScreenSettings;