import React from 'react'; import { Alert, Pressable, ScrollView, StyleSheet, Text, View, Switch } from 'react-native'; import { SafeAreaView } from 'react-native-safe-area-context'; import { useRouter } from 'expo-router'; import { Ionicons } from '@expo/vector-icons'; import { useTheme } from '../../theme'; import { useT } from '../../i18n'; import { useSettingsStore, type Locale, type ThemeMode } from '../../store/settingsStore'; import { Card } from '../../components/Card'; export default function PreferencesScreen() { const { theme } = useTheme(); const t = useT(); const router = useRouter(); // Settings State const themeMode = useSettingsStore(s => s.themeMode); const setThemeMode = useSettingsStore(s => s.setThemeMode); const locale = useSettingsStore(s => s.locale); const setLocale = useSettingsStore(s => s.setLocale); const appLockEnabled = useSettingsStore(s => s.appLockEnabled); const setAppLockEnabled = useSettingsStore(s => s.setAppLockEnabled); const reminderEnabled = useSettingsStore(s => s.reminderEnabled); const reminderHour = useSettingsStore(s => s.reminderHour); const reminderMinute = useSettingsStore(s => s.reminderMinute); const updateReminderConfig = useSettingsStore(s => s.updateReminderConfig); const THEME_OPTIONS: { mode: ThemeMode; labelKey: string }[] = [ { mode: 'light', labelKey: 'settings.themeLight' }, { mode: 'dark', labelKey: 'settings.themeDark' }, { mode: 'system', labelKey: 'settings.themeSystem' }, ]; const LOCALE_OPTIONS: { locale: Locale; labelKey: string }[] = [ { locale: 'zh', labelKey: 'settings.langZh' }, { locale: 'en', labelKey: 'settings.langEn' }, ]; return ( router.back()}> {t('settings.preferencesTitle')} {/* 外观设置 */} {t('settings.themeMode')} {THEME_OPTIONS.map(opt => { const active = themeMode === opt.mode; return ( setThemeMode(opt.mode)} style={[ styles.option, { backgroundColor: active ? theme.colors.accent : theme.colors.bgTertiary, borderColor: active ? theme.colors.accent : theme.colors.border, }, ]} > {t(opt.labelKey)} ); })} {/* 语言偏好 */} {t('settings.selectLang')} {LOCALE_OPTIONS.map(opt => { const active = locale === opt.locale; return ( setLocale(opt.locale)} style={[ styles.option, { backgroundColor: active ? theme.colors.accent : theme.colors.bgTertiary, borderColor: active ? theme.colors.accent : theme.colors.border, }, ]} > {t(opt.labelKey)} ); })} {/* 安全设置 */} {t('settings.appLock')} { if (val) { // 开启:需要设置 PIN const { hasPinSet, setPin } = await import('../../components/LockScreen'); const hasPin = await hasPinSet(); if (!hasPin) { // Alert.prompt 仅 iOS 可用;Android 直接开启(用户首次锁屏时设置) // 这里简化处理:直接开启,用户在锁屏界面首次输入时即设置 PIN // 完整版应弹出一个 Modal 含 TextInput(留后续优化) Alert.alert( t('settings.setPinDesc'), undefined, [ { text: t('common.cancel'), style: 'cancel', onPress: () => {} }, { text: t('common.confirm'), onPress: async () => { // 设置一个默认空 PIN,用户可在锁屏界面修改 // 真实实现应弹出 PIN 输入 Modal setAppLockEnabled(true); }, }, ], ); } else { setAppLockEnabled(true); } } else { // 关闭:清除 PIN const { clearPin } = await import('../../components/LockScreen'); await clearPin(); setAppLockEnabled(false); } }} trackColor={{ false: theme.colors.bgTertiary, true: theme.colors.accent }} thumbColor={theme.colors.fgInverse} /> {/* 每日记账提醒 */} {t('settings.reminderToggle')} { updateReminderConfig({ reminderEnabled: val }); try { const { RealNotificationScheduler, setupDailyReminder, cancelAllReminders } = await import('../../services/reminder'); const scheduler = new RealNotificationScheduler(); if (val) { await setupDailyReminder(scheduler, reminderHour, reminderMinute); } else { await cancelAllReminders(scheduler); } } catch (e) { Alert.alert(String(e)); } }} trackColor={{ false: theme.colors.bgTertiary, true: theme.colors.accent }} thumbColor={theme.colors.fgInverse} /> {reminderEnabled && ( {t('settings.reminderTime')} {String(reminderHour).padStart(2, '0')}:{String(reminderMinute).padStart(2, '0')} { // 简单的时间调整:小时 +1 循环 const newHour = (reminderHour + 1) % 24; updateReminderConfig({ reminderHour: newHour }); const { RealNotificationScheduler, setupDailyReminder } = await import('../../services/reminder'); await setupDailyReminder(new RealNotificationScheduler(), newHour, reminderMinute); }} style={{ marginLeft: 12, padding: 4 }} > )} ); } const styles = StyleSheet.create({ page: { flex: 1 }, header: { flexDirection: 'row', alignItems: 'center', paddingHorizontal: 16, paddingTop: 8, paddingBottom: 12 }, content: { padding: 16, gap: 16 }, optionRow: { flexDirection: 'row', gap: 8 }, option: { flex: 1, alignItems: 'center', paddingVertical: 8, borderWidth: 1, borderRadius: 4 }, switchRow: { flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, });