restructured progress

This commit is contained in:
Ollie Taylor
2023-02-25 14:46:07 +00:00
parent 49c5e4eb63
commit 81ab49d15e
6 changed files with 59 additions and 62 deletions
+4 -7
View File
@@ -42,14 +42,11 @@ const no_element = (action: string) => warn(`could not ${action} :: no audio ele
export const episode_audio = { export const episode_audio = {
subscribe: episode_attributes.subscribe, subscribe: episode_attributes.subscribe,
load: (src: string, details: EpisodeDetails) => { load: (src: string, details: EpisodeDetails) => {
podcast_progress.stash(); podcast_progress.stash_episode();
const el = get(audio_element); const el = get(audio_element);
if (!el) return no_element('load'); if (!el) return no_element('load');
const progress = podcast_progress.get(src); const progress = podcast_progress.get_episode(src);
el.src = src; el.src = src;
console.log('progress', progress);
console.log('src', src);
console.log('podcast_progress', podcast_progress.data);
el.currentTime = progress?.start_at || 0; el.currentTime = progress?.start_at || 0;
const preferences = get(user_preferences); const preferences = get(user_preferences);
@@ -59,7 +56,7 @@ export const episode_audio = {
episode_details.set(details); episode_details.set(details);
}, },
unload: () => { unload: () => {
podcast_progress.stash(); podcast_progress.stash_episode();
const el = get(audio_element); const el = get(audio_element);
if (!el) return no_element('unload'); if (!el) return no_element('unload');
el.src = ''; el.src = '';
@@ -76,7 +73,7 @@ export const episode_audio = {
} }
}, },
pause: (t?: HandleType) => { pause: (t?: HandleType) => {
podcast_progress.stash(); podcast_progress.stash_episode();
const el = get(audio_element); const el = get(audio_element);
if (!el) return no_element('pause'); if (!el) return no_element('pause');
+5 -11
View File
@@ -3,24 +3,18 @@ import clamp from 'just-clamp';
import { writable } from 'svelte/store'; import { writable } from 'svelte/store';
import { load_preferences, save_preferences } from './db-local-storage'; import { load_preferences, save_preferences } from './db-local-storage';
const _default_user_preferences = { const _default_user_preferences = { playback_rate: 1, volume: 1 } satisfies UserPreferences;
playback_rate: 1,
volume: 1,
} satisfies UserPreferences;
const _user_preferences = writable<UserPreferences>(_default_user_preferences); const _user_preferences = writable<UserPreferences>(_default_user_preferences);
function clamp_preferences(prefs: UserPreferences) { const clamp_preferences = (prefs: UserPreferences) => ({
return {
playback_rate: clamp(prefs.playback_rate, 0.5, 5), playback_rate: clamp(prefs.playback_rate, 0.5, 5),
volume: clamp(prefs.volume, 0, 1), volume: clamp(prefs.volume, 0, 1),
}; });
}
function edit(prefs: Partial<UserPreferences>) { const edit = (prefs: Partial<UserPreferences>) => {
_user_preferences.update((prev) => clamp_preferences({ ...prev, ...prefs })); _user_preferences.update((prev) => clamp_preferences({ ...prev, ...prefs }));
save_preferences(); save_preferences();
} };
const clear = () => { const clear = () => {
_user_preferences.set(_default_user_preferences); _user_preferences.set(_default_user_preferences);
@@ -5,7 +5,7 @@ import { error, info, warn } from '$lib/utility/package/log';
type SavedEpisodeProgress = { src: string; current_time: number }; type SavedEpisodeProgress = { src: string; current_time: number };
const EPISODE_PROGRESS_KEY = 'EPISODE_PROGRESS' as const; const EPISODE_PROGRESS_KEY = 'EPISODE_PROGRESS' as const;
export function save_all() { export function save_podcast_progress() {
if (!browser || !localStorage) { if (!browser || !localStorage) {
warn('localStorage not available, skipping save'); warn('localStorage not available, skipping save');
return; return;
@@ -22,7 +22,7 @@ export function save_all() {
localStorage.setItem(EPISODE_PROGRESS_KEY, JSON.stringify(episodes)); localStorage.setItem(EPISODE_PROGRESS_KEY, JSON.stringify(episodes));
} }
export function load_all() { export function load_podcast_progress() {
if (!browser || !localStorage) { if (!browser || !localStorage) {
warn('localStorage not available, skipping load'); warn('localStorage not available, skipping load');
return; return;
+43
View File
@@ -0,0 +1,43 @@
import { episode_audio, episode_progress } from '$lib/audio';
import { _episode_progress_map } from '$lib/progress/_private';
import type { AudioLoadOptions } from '$lib/types';
import { info } from '$lib/utility/package/log';
import { get } from 'svelte/store';
import { load_podcast_progress, save_podcast_progress } from './db-local-storage';
const get_src_pathname = (src: string) => {
if (src.startsWith('http')) return new URL(src).pathname;
else return new URL(src, 'https://svelte.dev').pathname;
};
const stash_episode_progress = () => {
const audio = get(episode_audio);
console.log('audio', audio);
if (!audio?.src) return;
info('saving progress: ', audio.src);
const pathname = get_src_pathname(audio.src);
_episode_progress_map.set(pathname, get(episode_progress).current_time);
save_podcast_progress();
};
const get_episode_progress = (src: string): Pick<AudioLoadOptions, 'start_at'> | null => {
const start_at = _episode_progress_map.get(src);
if (!start_at) return null;
info('found saved progress: ', src, start_at);
return { start_at: start_at };
};
const clear = () => {
_episode_progress_map.clear();
save_podcast_progress();
};
export const podcast_progress = {
stash_episode: stash_episode_progress,
get_episode: get_episode_progress,
load: load_podcast_progress,
clear,
};
+1 -39
View File
@@ -1,39 +1 @@
import { episode_audio, episode_progress } from '$lib/audio'; export * from './db-state';
import { _episode_progress_map } from '$lib/progress/_private';
import type { AudioLoadOptions } from '$lib/types';
import { info } from '$lib/utility/package/log';
import { get } from 'svelte/store';
import { load_all, save_all } from './local-storage';
const get_src_pathname = (src: string) => {
if (src.startsWith('http')) return new URL(src).pathname;
else return new URL(src, 'https://svelte.dev').pathname;
};
export const podcast_progress = {
data: _episode_progress_map,
stash() {
const audio = get(episode_audio);
console.log('audio', audio);
if (!audio?.src) return;
info('saving progress: ', audio.src);
const pathname = get_src_pathname(audio.src);
_episode_progress_map.set(pathname, get(episode_progress).current_time);
save_all();
},
get(src: string): Pick<AudioLoadOptions, 'start_at'> | null {
const start_at = _episode_progress_map.get(src);
if (!start_at) return null;
info('found saved progress: ', src, start_at);
return { start_at: start_at };
},
save: save_all,
load: load_all,
clear_all() {
_episode_progress_map.clear();
save_all();
},
};
+2 -1
View File
@@ -31,7 +31,8 @@
<h1>Demo</h1> <h1>Demo</h1>
<a href="/another-page">Another Page</a> <a href="/another-page">Another Page</a>
<button type="button" on:click={podcast_progress.save}>Save progress</button> <button type="button" on:click={podcast_progress.clear}>Clear progress for all episodes</button>
<button type="button" on:click={user_preferences.clear}>Clear all preferences</button>
<h5>Load Audio</h5> <h5>Load Audio</h5>
<button type="button" on:click={() => episode_audio.load(sources['syntax'].src, sources['knomii'])} <button type="button" on:click={() => episode_audio.load(sources['syntax'].src, sources['knomii'])}