|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { browser } from '$app/environment'; |
|
|
import { SETTING_CONFIG_DEFAULT } from '$lib/constants/settings-config'; |
|
|
|
|
|
class SettingsStore { |
|
|
config = $state<SettingsConfigType>({ ...SETTING_CONFIG_DEFAULT }); |
|
|
theme = $state<string>('auto'); |
|
|
isInitialized = $state(false); |
|
|
|
|
|
constructor() { |
|
|
if (browser) { |
|
|
this.initialize(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
initialize() { |
|
|
try { |
|
|
this.loadConfig(); |
|
|
this.loadTheme(); |
|
|
this.isInitialized = true; |
|
|
} catch (error) { |
|
|
console.error('Failed to initialize settings store:', error); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private loadConfig() { |
|
|
if (!browser) return; |
|
|
|
|
|
try { |
|
|
const savedVal = JSON.parse(localStorage.getItem('config') || '{}'); |
|
|
|
|
|
this.config = { |
|
|
...SETTING_CONFIG_DEFAULT, |
|
|
...savedVal |
|
|
}; |
|
|
} catch (error) { |
|
|
console.warn('Failed to parse config from localStorage, using defaults:', error); |
|
|
this.config = { ...SETTING_CONFIG_DEFAULT }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private loadTheme() { |
|
|
if (!browser) return; |
|
|
|
|
|
this.theme = localStorage.getItem('theme') || 'auto'; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateConfig<K extends keyof SettingsConfigType>(key: K, value: SettingsConfigType[K]) { |
|
|
this.config[key] = value; |
|
|
this.saveConfig(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateMultipleConfig(updates: Partial<SettingsConfigType>) { |
|
|
Object.assign(this.config, updates); |
|
|
this.saveConfig(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private saveConfig() { |
|
|
if (!browser) return; |
|
|
|
|
|
try { |
|
|
localStorage.setItem('config', JSON.stringify(this.config)); |
|
|
} catch (error) { |
|
|
console.error('Failed to save config to localStorage:', error); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
updateTheme(newTheme: string) { |
|
|
this.theme = newTheme; |
|
|
this.saveTheme(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private saveTheme() { |
|
|
if (!browser) return; |
|
|
|
|
|
try { |
|
|
if (this.theme === 'auto') { |
|
|
localStorage.removeItem('theme'); |
|
|
} else { |
|
|
localStorage.setItem('theme', this.theme); |
|
|
} |
|
|
} catch (error) { |
|
|
console.error('Failed to save theme to localStorage:', error); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resetConfig() { |
|
|
this.config = { ...SETTING_CONFIG_DEFAULT }; |
|
|
this.saveConfig(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resetTheme() { |
|
|
this.theme = 'auto'; |
|
|
this.saveTheme(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resetAll() { |
|
|
this.resetConfig(); |
|
|
this.resetTheme(); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getConfig<K extends keyof SettingsConfigType>(key: K): SettingsConfigType[K] { |
|
|
return this.config[key]; |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
getAllConfig(): SettingsConfigType { |
|
|
return { ...this.config }; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
export const settingsStore = new SettingsStore(); |
|
|
|
|
|
|
|
|
export const config = () => settingsStore.config; |
|
|
export const theme = () => settingsStore.theme; |
|
|
export const isInitialized = () => settingsStore.isInitialized; |
|
|
|
|
|
|
|
|
export const updateConfig = settingsStore.updateConfig.bind(settingsStore); |
|
|
export const updateMultipleConfig = settingsStore.updateMultipleConfig.bind(settingsStore); |
|
|
export const updateTheme = settingsStore.updateTheme.bind(settingsStore); |
|
|
export const resetConfig = settingsStore.resetConfig.bind(settingsStore); |
|
|
export const resetTheme = settingsStore.resetTheme.bind(settingsStore); |
|
|
export const resetAll = settingsStore.resetAll.bind(settingsStore); |
|
|
export const getConfig = settingsStore.getConfig.bind(settingsStore); |
|
|
export const getAllConfig = settingsStore.getAllConfig.bind(settingsStore); |
|
|
|