diff --git a/src/lib/components/icons/Pause.svelte b/src/lib/components/icons/Pause.svelte new file mode 100644 index 0000000..d920be9 --- /dev/null +++ b/src/lib/components/icons/Pause.svelte @@ -0,0 +1,10 @@ + diff --git a/src/lib/components/icons/Play.svelte b/src/lib/components/icons/Play.svelte new file mode 100644 index 0000000..d76aaf2 --- /dev/null +++ b/src/lib/components/icons/Play.svelte @@ -0,0 +1,10 @@ + diff --git a/src/lib/stores.ts b/src/lib/stores.ts index 8907acd..7ae707d 100644 --- a/src/lib/stores.ts +++ b/src/lib/stores.ts @@ -3,7 +3,7 @@ import { readable } from 'svelte/store'; export const now = readable(new Date(), set => { const interval = setInterval(() => { set(new Date()); - }, 1000); + }, 100); return () => clearInterval(interval); }); diff --git a/src/routes/(home)/SpotifyInfo.svelte b/src/routes/(home)/SpotifyInfo.svelte index 650b24e..a00f22f 100644 --- a/src/routes/(home)/SpotifyInfo.svelte +++ b/src/routes/(home)/SpotifyInfo.svelte @@ -3,8 +3,12 @@ import type { NowPlayingResponse } from '$lib/types'; import MusicalNote from '$lib/components/icons/MusicalNote.svelte'; + import Pause from '$lib/components/icons/Pause.svelte'; + import Play from '$lib/components/icons/Play.svelte'; + import { now } from '$lib/stores'; let data: NowPlayingResponse | undefined; + let lastFetched = 0; onMount(() => { fetchNowPlaying(); @@ -17,8 +21,20 @@ .then(res => res.json()) .then(res => { data = res; + lastFetched = Date.now(); }); } + + function clamp(t: number) { + return Math.max(Math.min(t, 1), 0); + } + + $: progress = data?.track + ? clamp( + (data.progessMs + ($now.getTime() - lastFetched)) / + data.track.duration_ms + ) + : 0;