2022-10-30 23:13:57 +00:00
|
|
|
import { reactive, ref } from 'vue'
|
2022-06-10 10:47:46 +00:00
|
|
|
import { userStore } from '@/stores'
|
|
|
|
import { localStorageService } from '@/services'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
interface Preferences extends Record<string, any> {
|
|
|
|
volume: number
|
|
|
|
notify: boolean
|
|
|
|
repeatMode: RepeatMode
|
|
|
|
confirmClosing: boolean
|
|
|
|
equalizer: EqualizerPreset,
|
|
|
|
artistsViewMode: ArtistAlbumViewMode | null,
|
|
|
|
albumsViewMode: ArtistAlbumViewMode | null,
|
|
|
|
selectedPreset: number
|
|
|
|
transcodeOnMobile: boolean
|
|
|
|
supportBarNoBugging: boolean
|
|
|
|
showAlbumArtOverlay: boolean
|
2022-10-26 19:24:24 +00:00
|
|
|
lyricsZoomLevel: number | null
|
2022-09-14 12:12:06 +00:00
|
|
|
theme?: Theme['id'] | null
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const preferenceStore = {
|
|
|
|
storeKey: '',
|
2022-10-30 23:13:57 +00:00
|
|
|
initialized: ref(false),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-04-20 15:57:53 +00:00
|
|
|
state: reactive<Preferences>({
|
2022-04-15 14:24:30 +00:00
|
|
|
volume: 7,
|
|
|
|
notify: true,
|
|
|
|
repeatMode: 'NO_REPEAT',
|
|
|
|
confirmClosing: false,
|
|
|
|
equalizer: {
|
|
|
|
preamp: 0,
|
|
|
|
gains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
|
|
|
|
},
|
|
|
|
artistsViewMode: null,
|
|
|
|
albumsViewMode: null,
|
|
|
|
selectedPreset: -1,
|
|
|
|
transcodeOnMobile: false,
|
|
|
|
supportBarNoBugging: false,
|
|
|
|
showAlbumArtOverlay: true,
|
2022-10-26 19:24:24 +00:00
|
|
|
lyricsZoomLevel: 1,
|
2022-04-15 14:24:30 +00:00
|
|
|
theme: null
|
2022-04-20 15:57:53 +00:00
|
|
|
}),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
|
|
|
init (user?: User): void {
|
|
|
|
const initUser = user || userStore.current
|
|
|
|
this.storeKey = `preferences_${initUser.id}`
|
2022-04-24 08:50:45 +00:00
|
|
|
Object.assign(this.state, localStorageService.get(this.storeKey, this.state))
|
2022-04-15 14:24:30 +00:00
|
|
|
this.setupProxy()
|
2022-10-30 23:13:57 +00:00
|
|
|
|
|
|
|
this.initialized.value = true
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Proxy the state properties, so that each can be directly accessed using the key.
|
|
|
|
*/
|
2022-04-20 15:57:53 +00:00
|
|
|
setupProxy () {
|
2022-04-15 14:24:30 +00:00
|
|
|
Object.keys(this.state).forEach(key => {
|
|
|
|
Object.defineProperty(this, key, {
|
|
|
|
get: (): any => this.get(key),
|
|
|
|
set: (value: any): void => this.set(key, value),
|
|
|
|
configurable: true
|
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2022-04-20 15:57:53 +00:00
|
|
|
set (key: keyof Preferences, val: any) {
|
2022-04-15 14:24:30 +00:00
|
|
|
this.state[key] = val
|
|
|
|
this.save()
|
|
|
|
},
|
|
|
|
|
2022-04-20 15:57:53 +00:00
|
|
|
get (key: string) {
|
2022-10-31 14:55:24 +00:00
|
|
|
return this.state?.[key]
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2022-04-20 15:57:53 +00:00
|
|
|
save () {
|
2022-04-24 08:50:45 +00:00
|
|
|
localStorageService.set(this.storeKey, this.state)
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-31 14:55:24 +00:00
|
|
|
const exported = preferenceStore as unknown as Omit<typeof preferenceStore, 'setupProxy'> & Preferences
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-10-31 14:55:24 +00:00
|
|
|
export { exported as preferenceStore }
|