adds user preferences store
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
audio_volume,
|
audio_volume,
|
||||||
} from '$lib/context/audio-internals';
|
} from '$lib/context/audio-internals';
|
||||||
import { episode_progress } from '$lib/context/episode-progress';
|
import { episode_progress } from '$lib/context/episode-progress';
|
||||||
|
import { user_preferences } from '$lib/context/user-preferences';
|
||||||
import type { PlayerElement } from '$lib/types/types';
|
import type { PlayerElement } from '$lib/types/types';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
|
|
||||||
@@ -44,6 +45,7 @@
|
|||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
episode_progress.load_all();
|
episode_progress.load_all();
|
||||||
|
user_preferences.load();
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import { browser } from '$app/environment';
|
||||||
|
import { info, warn } from '$lib/utility/package/log';
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
export type UserPreferences = {
|
||||||
|
playback_rate: number;
|
||||||
|
volume: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
const default_user_preferences = {
|
||||||
|
playback_rate: 1,
|
||||||
|
volume: 1,
|
||||||
|
} satisfies UserPreferences;
|
||||||
|
|
||||||
|
const user_preference_store = writable<UserPreferences>(default_user_preferences);
|
||||||
|
|
||||||
|
function set_preference(prefs: Partial<UserPreferences>) {
|
||||||
|
user_preference_store.update((prev) => ({ ...prev, ...prefs }));
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
const USER_PREFERENCE_KEY = 'USER_PREFERENCE' as const;
|
||||||
|
|
||||||
|
function save() {
|
||||||
|
if (!browser || !localStorage) {
|
||||||
|
warn('localStorage not available, skipping save');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return user_preference_store.subscribe((prefs) => {
|
||||||
|
info(`Saving user preferences to localStorage`, prefs);
|
||||||
|
localStorage.setItem(USER_PREFERENCE_KEY, JSON.stringify(prefs));
|
||||||
|
})();
|
||||||
|
}
|
||||||
|
|
||||||
|
function load() {
|
||||||
|
if (!browser || !localStorage) {
|
||||||
|
warn('localStorage not available, skipping load');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const data = localStorage.getItem(USER_PREFERENCE_KEY);
|
||||||
|
|
||||||
|
if (!data || typeof data !== 'object') {
|
||||||
|
info('No saved user preferences found');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const preferences = JSON.parse(data) as UserPreferences;
|
||||||
|
user_preference_store.update((prev) => ({ ...prev, ...preferences }));
|
||||||
|
info(`Loaded user preferences from localStorage`, data);
|
||||||
|
}
|
||||||
|
|
||||||
|
function clear() {
|
||||||
|
user_preference_store.set(default_user_preferences);
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
|
||||||
|
export const user_preferences = {
|
||||||
|
subscribe: user_preference_store.subscribe,
|
||||||
|
set: set_preference,
|
||||||
|
save,
|
||||||
|
load,
|
||||||
|
clear,
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user