koel/resources/assets/js/stores/preferenceStore.ts

82 lines
2.1 KiB
TypeScript
Raw Normal View History

import { reactive, ref } from 'vue'
import { http, localStorageService } from '@/services'
2022-04-15 14:24:30 +00:00
interface Preferences extends Record<string, any> {
volume: number
show_now_playing_notification: boolean
repeat_mode: RepeatMode
confirm_before_closing: boolean
2022-04-15 14:24:30 +00:00
equalizer: EqualizerPreset,
artists_view_mode: ArtistAlbumViewMode | null,
albums_view_mode: ArtistAlbumViewMode | null,
transcode_on_mobile: boolean
support_bar_no_bugging: boolean
show_album_art_overlay: boolean
lyrics_zoom_level: number | null
theme?: Theme['id'] | null
2022-11-06 17:09:06 +00:00
visualizer?: Visualizer['id'] | null
active_extra_panel_tab: ExtraPanelTab | null
make_uploads_public: boolean
2022-04-15 14:24:30 +00:00
}
const preferenceStore = {
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,
show_now_playing_notification: true,
repeat_mode: 'NO_REPEAT',
confirm_before_closing: false,
2022-04-15 14:24:30 +00:00
equalizer: {
2022-11-02 19:25:22 +00:00
name: 'Default',
2022-04-15 14:24:30 +00:00
preamp: 0,
gains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
},
artists_view_mode: null,
albums_view_mode: null,
transcode_on_mobile: false,
support_bar_no_bugging: false,
show_album_art_overlay: true,
lyrics_zoom_level: 1,
2022-11-06 17:09:06 +00:00
theme: null,
visualizer: 'default',
active_extra_panel_tab: null,
make_uploads_public: false
2022-04-20 15:57:53 +00:00
}),
2022-04-15 14:24:30 +00:00
init (preferences: Preferences): void {
Object.assign(this.state, preferences)
2022-04-15 14:24:30 +00:00
this.setupProxy()
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
})
})
},
set (key: keyof Preferences, value: any) {
if (this.state[key] === value) return
this.state[key] = value
http.silently.patch('me/preferences', { key, value })
2022-04-15 14:24:30 +00:00
},
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-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 }