You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

168 lines
4.2 KiB

1 month ago
/**
*
* 使 Zustand + AsyncStorage
*/
import { create } from 'zustand';
import { useShallow } from 'zustand/react/shallow';
import storageManager, { STORAGE_KEYS } from '@/utils/storageManager';
1 month ago
/**
*
*/
export type Theme = 'light' | 'dark' | '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<SettingsState>()((set, get) => ({
// 初始状态
...DEFAULT_SETTINGS,
// 设置主题
setTheme: (theme) => {
set({ theme });
// 手动持久化
storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get());
1 month ago
if (__DEV__) {
console.log('🎨 Theme changed:', theme);
}
},
// 设置语言
setLanguage: (language) => {
set({ language });
// 手动持久化
storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get());
1 month ago
if (__DEV__) {
console.log('🌐 Language changed:', language);
}
},
// 设置通知开关
setNotificationsEnabled: (enabled) => {
set({ notificationsEnabled: enabled });
// 手动持久化
storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get());
1 month ago
if (__DEV__) {
console.log('🔔 Notifications:', enabled ? 'enabled' : 'disabled');
}
},
// 设置声音开关
setSoundEnabled: (enabled) => {
set({ soundEnabled: enabled });
// 手动持久化
storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get());
1 month ago
if (__DEV__) {
console.log('🔊 Sound:', enabled ? 'enabled' : 'disabled');
}
},
// 设置触觉反馈开关
setHapticsEnabled: (enabled) => {
set({ hapticsEnabled: enabled });
// 手动持久化
storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get());
1 month ago
if (__DEV__) {
console.log('📳 Haptics:', enabled ? 'enabled' : 'disabled');
}
},
// 重置所有设置
resetSettings: () => {
set(DEFAULT_SETTINGS);
// 手动持久化
storageManager.local.setItem(STORAGE_KEYS.SETTINGS_STORE, get());
1 month ago
if (__DEV__) {
console.log('🔄 Settings reset to default');
}
},
}));
// 从 AsyncStorage 恢复状态的函数
export const restoreSettingsState = async () => {
try {
const stored = await storageManager.local.getItem(STORAGE_KEYS.SETTINGS_STORE);
1 month ago
if (stored) {
const state = JSON.parse(stored);
useSettingsStore.setState(state);
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);
// 获取设置操作方法
// 使用 useShallow 避免每次渲染都返回新对象
export const useSettingsActions = () =>
useSettingsStore(
useShallow((state) => ({
setTheme: state.setTheme,
setLanguage: state.setLanguage,
setNotificationsEnabled: state.setNotificationsEnabled,
setSoundEnabled: state.setSoundEnabled,
setHapticsEnabled: state.setHapticsEnabled,
resetSettings: state.resetSettings,
}))
);