From daf33b51086e3cece1213976522e60d69509765c Mon Sep 17 00:00:00 2001 From: Bellisario <72039923+Bellisario@users.noreply.github.com> Date: Fri, 26 Apr 2024 19:36:48 +0200 Subject: [PATCH] feat: initial previous/next buttons support --- .../PlayNextView/PlayNextController.ts | 51 ++++++------ .../PlayerControls/PlayerControls.svelte | 77 +++++++++++++++++-- src/lib/player.ts | 14 +++- 3 files changed, 111 insertions(+), 31 deletions(-) diff --git a/src/components/PlayNextView/PlayNextController.ts b/src/components/PlayNextView/PlayNextController.ts index 131079c..64f5b0c 100644 --- a/src/components/PlayNextView/PlayNextController.ts +++ b/src/components/PlayNextView/PlayNextController.ts @@ -4,6 +4,7 @@ import { currentID, albumsAddedToPlayNext, favoritesPlayStatus, + playNextIndex, } from '$lib/player'; import { wantPlay } from '$lib/wantPlay'; @@ -65,7 +66,7 @@ playNextList.subscribe((list) => { && get(currentID) === '' // 2. ) playNext(); - + playNextWasEmpty = list.length === 0; }); @@ -78,35 +79,35 @@ playNextList.subscribe((list) => { favoritesPlayStatus.set(-1); }); -if ('mediaSession' in navigator) { - navigator.mediaSession.setActionHandler('previoustrack', () => { - // if there is no song in playNextList, return - if (get(playNextList).length === 0) return; +export function playPreviousSong() { + const index = get(playNextIndex); + // if there is no song in playNextList, return + if (index === null) return; - // find the index of the last Play Next song - const index = get(playNextList).findIndex((item) => { - return item.id === lastPlayNextID; - }); + // if the index is the first index, it means the song is already the first song + if (index === 0) return; - // if the index is the first index, it means the song is the first song - if (index === 0) return; + // play the previous song + wantPlay(get(playNextList)[index - 1]); +} - // play the previous song - wantPlay(get(playNextList)[index - 1]); - }); - navigator.mediaSession.setActionHandler('nexttrack', () => { - // if there is no song in playNextList, return - if (get(playNextList).length === 0) return; +export function playNextSong() { + const index = get(playNextIndex); + // if there is no song in playNextList, return + if (index === null) return; - // find the index of the last Play Next song - const index = get(playNextList).findIndex((item) => { - return item.id === lastPlayNextID; - }); + // if the index is the last index, it means the song is the last song + if (index === get(playNextList).length - 1) return; - // if the index is the last index, it means the song is the last song - if (index === get(playNextList).length - 1) return; + // play the next song + wantPlay(get(playNextList)[index + 1]); +} - // play the next song - wantPlay(get(playNextList)[index + 1]); +if ('mediaSession' in navigator) { + navigator.mediaSession.setActionHandler('previoustrack', () => { + playPreviousSong(); + }); + navigator.mediaSession.setActionHandler('nexttrack', () => { + playNextSong(); }); } diff --git a/src/components/PlayerControls/PlayerControls.svelte b/src/components/PlayerControls/PlayerControls.svelte index 09f325f..a0a20ae 100644 --- a/src/components/PlayerControls/PlayerControls.svelte +++ b/src/components/PlayerControls/PlayerControls.svelte @@ -12,12 +12,20 @@ artist, poster, volume, + playNextList, } from '$lib/player'; import { play, pause } from '$lib/AudioPlayer.svelte'; import { fade } from 'svelte/transition'; import clickOutside from '$lib/clickOutside'; + import { tweened } from 'svelte/motion'; + import { cubicOut } from 'svelte/easing'; + import { + playNextSong, + playPreviousSong, + } from '$components/PlayNextView/PlayNextController'; + let smallScreen = false; window.addEventListener('resize', () => { @@ -32,6 +40,18 @@ let resetStatus: 'none' | 'working' | 'done' = 'none'; let resetTimeout: NodeJS.Timeout; + // expressed in REMs + let playerButtonsWidth = tweened(1.75, { + duration: 300, + easing: cubicOut, + }); + + playNextList.subscribe((val) => { + if (val.length === 0) return playerButtonsWidth.set(1.75); + + playerButtonsWidth.set(1.75 /*rem*/ * 3 /* buttons*/ + 2 /*rem for gap*/); + }); + paused.subscribe(() => { if ('mediaSession' in navigator) { navigator.mediaSession.metadata = new MediaMetadata({ @@ -177,10 +197,26 @@ : 'Keep pressing to reset playing time...'} {/if} - - -