Skip to content

Commit 3d833ed

Browse files
Frontend/fix media controls and flow (#30)
* readable code.. fixed some bugs on the way * fixes audio play bug * base sleep timer implementation * keep track of current song, time, playlist, shuffeled, repeat * moved functions * chore: bump version --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 94676e1 commit 3d833ed

21 files changed

+643
-602
lines changed

MyMusicClientSveltePwa/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

MyMusicClientSveltePwa/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mymusicclientsveltepwa",
33
"private": true,
4-
"version": "0.1.5",
4+
"version": "0.1.6",
55
"type": "module",
66
"scripts": {
77
"dev": "vite --host",

MyMusicClientSveltePwa/src/App.svelte

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,32 @@
11
<!-- App.svelte -->
22
<script>
3-
import { onDestroy, onMount } from "svelte";
4-
import { initializeRoute, pathName, navigateTo, component, componentParams } from "./lib/scripts/route.js";
5-
import { initPlaybackAudio } from "./lib/scripts/playback.js";
6-
import { initializeMediaSession } from "./lib/scripts/mediasession.js";
3+
import { onMount, onDestroy } from "svelte";
4+
import { initializeRouteService, pathName, navigateTo, component, componentParams } from "./lib/scripts/routeService.js";
75
import PlayerBarComponent from "./lib/components/PlayerBarComponent.svelte";
86
import Modals from "./lib/components/Modals.svelte";
9-
import { initPlaylist } from "./lib/scripts/playlist.js";
10-
11-
// TODO remove this import when manual refresh logic is no longer needed
12-
import { clearStorage } from "./lib/scripts/storage.js";
13-
import { playlistsStore, initStores, updateStores } from "./lib/scripts/api.js";
7+
import { initializePlaylistService } from "./lib/scripts/playlistService.js";
8+
import { initializePlaybackService } from "./lib/scripts/playbackService.js";
9+
import { initializeMediaSessionService } from "./lib/scripts/mediasessionService.js";
1410
1511
$: $pathName;
1612
$: $component;
1713
18-
let intervalId;
19-
2014
// @ts-ignore
2115
const version = __APP_VERSION__;
2216
2317
onMount(() => {
24-
async function async() {
25-
await initStores();
26-
initPlaylist();
27-
initPlaybackAudio();
28-
initializeMediaSession();
29-
initializeRoute();
30-
backgroundFetch();
18+
async function initializeServices() {
19+
initializeRouteService();
20+
await initializePlaylistService();
21+
initializePlaybackService();
22+
initializeMediaSessionService();
3123
}
32-
async();
24+
initializeServices();
3325
});
3426
35-
async function backgroundFetch() {
36-
const fetchInterval = 1000 * 15; // 15 seconds
37-
let isRunning = false;
38-
39-
intervalId = setInterval(async () => {
40-
if (isRunning) return; // Prevent concurrent executions
41-
42-
isRunning = true;
43-
44-
try {
45-
await updateStores();
46-
} catch (error) {
47-
console.error("Error during background fetch:", error);
48-
} finally {
49-
isRunning = false;
50-
}
51-
}, fetchInterval);
52-
}
53-
5427
// This is a temporary function to handle refresh logic.
5528
// It can be replaced with a more specific implementation later.
56-
async function refresh() {
57-
clearInterval(intervalId);
58-
clearStorage();
59-
playlistsStore.set([]);
60-
await initStores();
61-
backgroundFetch();
62-
}
29+
async function refresh() {}
6330
</script>
6431

6532
<div class="app-layout bg-dark">
@@ -93,7 +60,7 @@
9360

9461
<Modals />
9562

96-
<audio id="audio-player" style="display: none;"></audio>
63+
<audio id="audio-player" preload="none" style="display: none;"></audio>
9764

9865
<style>
9966
.app-layout {

MyMusicClientSveltePwa/src/lib/components/Modals.svelte

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
<script>
2-
import { isPlaying, currentSong, playOrPauseAudio, playPercentage, toggleRepeat, isRepeatEnabled, setCurrentTime } from "../scripts/playback.js";
3-
import { getImageUrl } from "../scripts/api.js";
4-
import { nextSong, previousSong, shufflePlaylist, isShuffleEnabled } from "../scripts/playlist.js";
2+
// @ts-nocheck
3+
4+
import { isPlaying, currentSong,
5+
playPercentage, setCurrentTime,
6+
nextSong, previousSong,
7+
isShuffledEnabled, isLoopingEnabled,
8+
toggleShuffle, playOrPauseSong,
9+
toggleLoop } from "../scripts/playbackService";
10+
import { getImageUrl } from "../scripts/api";
11+
import { get } from "svelte/store";
12+
import { isTimerEnabled, timeLeft, toggleSleepTimer } from "../scripts/sleeptimerService";
513
6-
$: $isPlaying;
714
$: $currentSong;
15+
$: $isPlaying;
816
$: $playPercentage;
9-
$: $isShuffleEnabled;
10-
$: $isRepeatEnabled;
11-
12-
function next() {
13-
playOrPauseAudio(nextSong());
14-
}
15-
function prev() {
16-
playOrPauseAudio(previousSong());
17-
}
18-
17+
$: $isShuffledEnabled;
18+
$: $isLoopingEnabled;
19+
$: $isTimerEnabled;
20+
$: $timeLeft;
21+
1922
function togglePlay() {
20-
playOrPauseAudio(null);
23+
playOrPauseSong(get(currentSong).id);
2124
}
2225
2326
function seekEvent(event) {
@@ -34,8 +37,8 @@
3437
</script>
3538

3639
<!-- Modal -->
37-
{#if $currentSong}
38-
<div class="modal fade" id="songControlModal" tabindex="-1" aria-labelledby="songControlModalLabel" aria-hidden="true">
40+
{#if $currentSong && $currentSong.id !== -999} <!-- Ensure currentSong is valid -->
41+
<div class="modal fade" id="songControlModal" tabindex="-1" aria-labelledby="songControlModalLabel" aria-hidden="false">
3942
<div class="modal-dialog modal-fullscreen-sm-down">
4043
<div class="modal-content bg-dark">
4144
<div class="modal-body">
@@ -55,7 +58,7 @@
5558
<div class="col-12">
5659
<div class="row mt-4">
5760
<div class="col-4">
58-
<button aria-label="previous song" on:click={prev} class="btn btn-dark w-100">
61+
<button aria-label="previous song" on:click={previousSong} class="btn btn-dark w-100">
5962
<i class="fa-solid fa-backward fa-2xl"></i>
6063
</button>
6164
</div>
@@ -71,7 +74,7 @@
7174
</div>
7275

7376
<div class="col-4">
74-
<button aria-label="next song" on:click={next} class="btn btn-dark w-100">
77+
<button aria-label="next song" on:click={nextSong} class="btn btn-dark w-100">
7578
<i class="fa-solid fa-forward fa-2xl"></i>
7679
</button>
7780
</div>
@@ -81,23 +84,23 @@
8184
<div class="col-12">
8285
<div class="row mt-5">
8386
<div class="col-4">
84-
<button disabled aria-label="sleep timer" type="button" class="btn btn-dark w-100">
85-
<i class="fa-solid fa-stopwatch-20" style="color: white !important;">
86-
<span style="font-size: 0.5rem;">
87-
&nbsp;TODO
87+
<button on:click={toggleSleepTimer} aria-label="sleep timer" type="button" class="btn btn-dark w-100">
88+
<i class="fa-solid fa-stopwatch-20" style="{$isTimerEnabled ? "color: #5bbd99;" : "color:white;"}">
89+
<span style="font-size: 0.8rem;">
90+
&nbsp;{$isTimerEnabled ? $timeLeft : ""}
8891
</span>
8992
</i>
9093
</button>
9194
</div>
9295

9396
<div class="col-4">
94-
<button on:click={shufflePlaylist} aria-label="shuffle playlist" type="button" class="btn btn-dark w-100">
95-
<i class="fa-solid fa-shuffle" style="{$isShuffleEnabled ? "color: #5bbd99;" : "color:white;"}"></i>
97+
<button on:click={toggleShuffle} aria-label="shuffle playlist" type="button" class="btn btn-dark w-100">
98+
<i class="fa-solid fa-shuffle" style="{$isShuffledEnabled ? "color: #5bbd99;" : "color:white;"}"></i>
9699
</button>
97100
</div>
98101
<div class="col-4">
99-
<button on:click={toggleRepeat} aria-label="repeat song" type="button" class="btn btn-dark w-100">
100-
<i class="fa-solid fa-repeat" style="{$isRepeatEnabled ? "color: #5bbd99;" : "color:white;"}"></i>
102+
<button on:click={toggleLoop} aria-label="repeat song" type="button" class="btn btn-dark w-100">
103+
<i class="fa-solid fa-repeat" style="{$isLoopingEnabled ? "color: #5bbd99;" : "color:white;"}"></i>
101104
</button>
102105
</div>
103106
</div>

MyMusicClientSveltePwa/src/lib/components/PlayerBarComponent.svelte

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<script>
2-
import { isPlaying, currentSong, playOrPauseAudio, playPercentage } from "../scripts/playback.js";
2+
// @ts-nocheck
3+
import { get } from "svelte/store";
4+
import { isPlaying, currentSong, playOrPauseSong, playPercentage } from "../scripts/playbackService";
35
4-
$: $isPlaying;
56
$: $currentSong;
7+
$: $isPlaying;
68
$: $playPercentage;
79
810
function togglePlay() {
9-
playOrPauseAudio(null);
11+
if(get(currentSong)){
12+
playOrPauseSong(get(currentSong).id);
13+
}
1014
}
1115
</script>
1216

1317
<div class="container-fluid player-bar mb-2 rounded rounded-5">
1418
<div class="row space-between">
1519
<div class="col-9 rounded-end rounded-end-0 rounded-5 border border-1 border-white" style="background: linear-gradient(to right, gray {$playPercentage}%, #5bbd99 {$playPercentage}%);">
16-
<button type="button" class="btn clickable-text rounded-end rounded-end-0 rounded-5" data-bs-toggle="modal" data-bs-target="#songControlModal">
20+
<button type="button" class="btn clickable-text rounded-end rounded-end-0 rounded-5" data-bs-toggle="{$currentSong ? "modal" : ""}" data-bs-target="{$currentSong ? "#songControlModal" : ""}">
1721
{#if $currentSong}
1822
{$currentSong.name}
1923
{:else}
@@ -23,7 +27,7 @@
2327
</div>
2428
<div class="col-3 border-start border-2">
2529
<button on:click={togglePlay} class="btn btn-dark border border-1 border-white play-button rounded-end rounded-end-5 w-100">
26-
{#if $isPlaying}
30+
{#if $currentSong && $isPlaying}
2731
<i class="fa-solid fa-pause"></i>
2832
{:else}
2933
<i class="fa-solid fa-play"></i>
@@ -38,7 +42,7 @@
3842
font-size: 0.85rem;
3943
max-height: 2.8rem;
4044
min-height: 2.8rem;
41-
width: 100%;;
45+
width: 100%;
4246
font-weight: bold;
4347
color: white;
4448
display: -webkit-box;
@@ -50,7 +54,7 @@
5054
margin-bottom: 2px;
5155
}
5256
53-
.player-bar{
57+
.player-bar {
5458
background-color: gray !important;
5559
}
5660

MyMusicClientSveltePwa/src/lib/components/PlaylistComponent.svelte

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
<script>
2+
// @ts-nocheck
3+
import { getImageUrl } from "../scripts/api";
4+
import { navigateTo } from "../scripts/routeService.js";
25
// @ts-nocheck
36
export let playlist = null;
4-
5-
import { navigateTo } from "../scripts/route.js";
6-
import { getImageUrl } from "../scripts/api";
7-
import { currentPlaylistId } from "../scripts/playlist.js";
8-
9-
$: $currentPlaylistId;
107
118
async function viewPlaylist() {
129
navigateTo(`/Playlist`, { playlistId : playlist.id });
@@ -15,7 +12,7 @@
1512

1613
<div class="playlist-component">
1714
{#if playlist}
18-
<button on:click={viewPlaylist} class="playlist-item btn w-100 border border-3" style="--url: url({getImageUrl(playlist.thumbnailPath)}); {$currentPlaylistId && $currentPlaylistId === playlist.id ? "border-color: #5bbd99 !important;" : ""}">
15+
<button on:click={viewPlaylist} class="playlist-item btn w-100 border border-3" style="--url: url({getImageUrl(playlist.thumbnailPath)}); {true ? "border-color: #5bbd99 !important;" : ""}">
1916
<h3>{playlist.name}</h3>
2017
<p>{playlist.description}</p>
2118
</button>

MyMusicClientSveltePwa/src/lib/components/SongComponent.svelte

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,33 @@
11
<script>
2-
import { playOrPauseAudio, currentSong, isPlaying } from "../scripts/playback.js";
3-
import { setSongs, setCurrentSong, getCurrentSong } from "../scripts/playlist.js"
2+
// @ts-nocheck
43
5-
export let song;
6-
export let playlistId;
4+
import { getContext, onMount, setContext } from "svelte";
5+
import { currentSong, isPlaying } from "../scripts/playbackService";
76
8-
$: $currentSong;
9-
$: $isPlaying;
7+
export let song;
108
11-
function playSong() {
12-
setSongs(playlistId);
13-
setCurrentSong(song);
14-
playOrPauseAudio(getCurrentSong());
15-
}
9+
let playOrPauseSong;
10+
11+
onMount(() => {
12+
playOrPauseSong = getContext("playOrPauseSong");
13+
});
14+
15+
$: $isPlaying;
16+
$: $currentSong;
1617
</script>
1718

1819
{#if song}
1920
<div class="row mb-3 song-component">
20-
<div class="col-10 bg-dark border border-1 rounded rounded-2" style="{$currentSong && $currentSong.id === song.id && $isPlaying ? "border-color:#5bbd99 !important;" : ""}">
21+
<div class="col-10 bg-dark border border-1 rounded rounded-2" style={$currentSong && $currentSong.id === song.id ? "border-color:#5bbd99 !important;" : ""}>
2122
<div class="text-lg-start">
22-
<p style="{$currentSong && $currentSong.id === song.id && $isPlaying ? "color:#5bbd99;" : ""}">{song.name}</p>
23+
<p style={$currentSong && $currentSong.id === song.id ? "color:#5bbd99;" : ""}>{song.name}</p>
2324
</div>
2425
</div>
2526
<div class="col-2">
26-
<button on:click={playSong} class="btn btn-dark play-button">
27+
<button
28+
on:click={() => playOrPauseSong(song.id)}
29+
class="btn btn-dark play-button"
30+
>
2731
{#if $currentSong && $currentSong.id === song.id && $isPlaying}
2832
<i class="fa-solid fa-pause"></i>
2933
{:else}
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<script>
22
import { onMount } from "svelte";
33
import PlaylistComponent from "../components/PlaylistComponent.svelte";
4-
import { playlistsStore } from "../scripts/api";
4+
import { playlistsStore } from "../scripts/playlistService.js";
55
66
$: $playlistsStore;
77
88
onMount(() => {});
99
</script>
10-
1110
{#if $playlistsStore.length > 0}
1211
{#each $playlistsStore as playlist}
1312
<PlaylistComponent {playlist} />
1413
{/each}
1514
{:else}
1615
<p class="text-center">Working.....</p>
17-
{/if}
16+
{/if}

0 commit comments

Comments
 (0)