2022-10-30 23:13:57 +00:00
|
|
|
import { reactive, ref } from 'vue'
|
2024-03-15 15:09:50 +00:00
|
|
|
import { http } from '@/services'
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
export const defaultPreferences: UserPreferences = {
|
|
|
|
volume: 7,
|
|
|
|
show_now_playing_notification: true,
|
|
|
|
repeat_mode: 'NO_REPEAT',
|
|
|
|
confirm_before_closing: false,
|
|
|
|
equalizer: {
|
|
|
|
name: 'Default',
|
|
|
|
preamp: 0,
|
2024-10-13 17:37:01 +00:00
|
|
|
gains: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
|
2024-01-29 21:58:50 +00:00
|
|
|
},
|
|
|
|
artists_view_mode: null,
|
|
|
|
albums_view_mode: null,
|
|
|
|
transcode_on_mobile: false,
|
2024-09-05 10:33:20 +00:00
|
|
|
transcode_quality: 128,
|
2024-01-29 21:58:50 +00:00
|
|
|
support_bar_no_bugging: false,
|
|
|
|
show_album_art_overlay: true,
|
|
|
|
lyrics_zoom_level: 1,
|
|
|
|
theme: null,
|
|
|
|
visualizer: 'default',
|
|
|
|
active_extra_panel_tab: null,
|
2024-03-25 22:59:38 +00:00
|
|
|
make_uploads_public: false,
|
2024-10-13 17:37:01 +00:00
|
|
|
continuous_playback: false,
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const preferenceStore = {
|
2024-06-02 07:48:18 +00:00
|
|
|
_temporary: false,
|
2022-10-30 23:13:57 +00:00
|
|
|
initialized: ref(false),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
state: reactive<UserPreferences>(defaultPreferences),
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
init (preferences: UserPreferences = defaultPreferences) {
|
2024-01-23 22:50:50 +00:00
|
|
|
Object.assign(this.state, preferences)
|
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),
|
2024-10-13 17:37:01 +00:00
|
|
|
configurable: true,
|
2022-04-15 14:24:30 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2024-01-29 21:58:50 +00:00
|
|
|
set (key: keyof UserPreferences, value: any) {
|
2024-10-13 17:37:01 +00:00
|
|
|
if (this.state[key] === value) {
|
|
|
|
return
|
|
|
|
}
|
2024-01-23 22:50:50 +00:00
|
|
|
|
|
|
|
this.state[key] = value
|
2024-06-02 07:48:18 +00:00
|
|
|
|
|
|
|
if (!this._temporary) {
|
|
|
|
http.silently.patch('me/preferences', { key, value })
|
|
|
|
} else {
|
|
|
|
this._temporary = false
|
|
|
|
}
|
2022-04-15 14:24:30 +00:00
|
|
|
},
|
|
|
|
|
2024-07-05 12:20:30 +00:00
|
|
|
get (key: keyof UserPreferences) {
|
2022-10-31 14:55:24 +00:00
|
|
|
return this.state?.[key]
|
2024-06-02 07:48:18 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
// Calling preferenceStore.temporary.volume = 7 won't trigger saving.
|
|
|
|
// This is useful in tests as it doesn't create stray HTTP requests.
|
|
|
|
get temporary () {
|
|
|
|
this._temporary = true
|
|
|
|
return this as unknown as ExportedType
|
2024-10-13 17:37:01 +00:00
|
|
|
},
|
2022-04-15 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
2024-06-02 07:48:18 +00:00
|
|
|
type ExportedType = Omit<typeof preferenceStore, 'setupProxy' | '_temporary'> & UserPreferences
|
|
|
|
|
|
|
|
const exported = preferenceStore as unknown as ExportedType
|
2022-04-15 14:24:30 +00:00
|
|
|
|
2022-10-31 14:55:24 +00:00
|
|
|
export { exported as preferenceStore }
|