/** * 应用设置状态管理 * 使用 Zustand + AsyncStorage 持久化 */ import { create } from 'zustand'; import { useShallow } from 'zustand/react/shallow'; import storageManager, { STORAGE_KEYS } from '@/utils/storageManager'; /** * 主题类型 */ export type Theme = 'light' | 'dark' | 'orange' | 'auto'; /** * 语言类型 */ export type Language = 'zh-CN' | 'en-US'; /** * 设置状态接口 */ interface SettingsState { // 状态 theme: Theme; language: Language; notificationsEnabled: boolean; soundEnabled: boolean; hapticsEnabled: boolean; // 操作 setTheme: (theme: Theme) => void; setLanguage: (language: Language) => void; setNotificationsEnabled: (enabled: boolean) => void; setSoundEnabled: (enabled: boolean) => void; setHapticsEnabled: (enabled: boolean) => void; resetSettings: () => void; } /** * 默认设置 */ const DEFAULT_SETTINGS = { theme: 'auto' as Theme, language: 'zh-CN' as Language, notificationsEnabled: true, soundEnabled: true, hapticsEnabled: true, }; /** * 设置状态 Store */ export const useSettingsStore = create()((set, get) => ({ // 初始状态 ...DEFAULT_SETTINGS, // 设置主题 setTheme: (theme) => { set({ theme }); // 手动持久化 storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get()); if (__DEV__) { console.log('🎨 Theme changed:', theme); } }, // 设置语言 setLanguage: (language) => { set({ language }); // 手动持久化 storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get()); if (__DEV__) { console.log('🌐 Language changed:', language); } }, // 设置通知开关 setNotificationsEnabled: (enabled) => { set({ notificationsEnabled: enabled }); // 手动持久化 storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get()); if (__DEV__) { console.log('🔔 Notifications:', enabled ? 'enabled' : 'disabled'); } }, // 设置声音开关 setSoundEnabled: (enabled) => { set({ soundEnabled: enabled }); // 手动持久化 storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get()); if (__DEV__) { console.log('🔊 Sound:', enabled ? 'enabled' : 'disabled'); } }, // 设置触觉反馈开关 setHapticsEnabled: (enabled) => { set({ hapticsEnabled: enabled }); // 手动持久化 storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get()); if (__DEV__) { console.log('📳 Haptics:', enabled ? 'enabled' : 'disabled'); } }, // 重置所有设置 resetSettings: () => { set(DEFAULT_SETTINGS); // 手动持久化 storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get()); if (__DEV__) { console.log('🔄 Settings reset to default'); } }, })); // 从 AsyncStorage 恢复状态的函数 export const restoreSettingsState = async () => { try { const stored = await storageManager.local.getItem(STORAGE_KEYS.SETTINGS_STORE); if (stored) { useSettingsStore.setState(stored); if (__DEV__) { console.log('✅ Settings state restored from storage'); } } } catch (error) { console.error('Failed to restore settings state:', error); } }; /** * 选择器 Hooks */ // 获取主题 export const useTheme = () => useSettingsStore((state) => state.theme); // 获取语言 export const useLanguage = () => useSettingsStore((state) => state.language); // 获取通知状态 export const useNotificationsEnabled = () => useSettingsStore((state) => state.notificationsEnabled); // 获取声音状态 export const useSoundEnabled = () => useSettingsStore((state) => state.soundEnabled); // 获取触觉反馈状态 export const useHapticsEnabled = () => useSettingsStore((state) => state.hapticsEnabled);