Skip to content

Commit 5a2040b

Browse files
authored
Merge branch 'main' into dev/prod
2 parents 97dd72c + ca12d4a commit 5a2040b

File tree

11 files changed

+253
-95
lines changed

11 files changed

+253
-95
lines changed

.github/workflows/main.yml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Auto Bump Version
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
types:
8+
- opened
9+
- synchronize
10+
11+
permissions:
12+
contents: write
13+
pull-requests: write
14+
15+
jobs:
16+
bump:
17+
if: contains(github.head_ref, 'frontend')
18+
runs-on: ubuntu-latest
19+
20+
steps:
21+
- name: Checkout code
22+
uses: actions/checkout@v4
23+
24+
- name: Setup Node.js
25+
uses: actions/setup-node@v4
26+
with:
27+
node-version: '18'
28+
29+
- name: Install Node dependencies
30+
working-directory: MyMusicClientSveltePwa
31+
run: npm ci
32+
33+
- name: Bump version (no commit)
34+
working-directory: MyMusicClientSveltePwa
35+
run: npm version patch --no-git-tag-version
36+
37+
- name: Commit version bump to PR branch
38+
working-directory: MyMusicClientSveltePwa
39+
run: |
40+
git config user.name "github-actions[bot]"
41+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
42+
git fetch origin ${{ github.head_ref }}
43+
git checkout -b temp-pr-branch origin/${{ github.head_ref }}
44+
45+
git add package.json package-lock.json
46+
git commit -m "chore: bump version"
47+
48+
git push origin temp-pr-branch:${{ github.head_ref }}
49+
env:
50+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

MyMusicClientSveltePwa/package-lock.json

Lines changed: 13 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: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mymusicclientsveltepwa",
33
"private": true,
4-
"version": "0.0.0",
4+
"version": "0.1.5",
55
"type": "module",
66
"scripts": {
77
"dev": "vite --host",
@@ -14,6 +14,7 @@
1414
"prettier-plugin-svelte": "^3.4.0",
1515
"svelte": "^5.33.19",
1616
"vite": "^6.3.5",
17+
"vite-plugin-package-version": "^1.1.0",
1718
"vite-plugin-pwa": "^1.0.0"
1819
},
1920
"dependencies": {

MyMusicClientSveltePwa/src/App.svelte

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<!-- App.svelte -->
22
<script>
3-
import { onMount } from "svelte";
3+
import { onDestroy, onMount } from "svelte";
44
import { initializeRoute, pathName, navigateTo, component, componentParams } from "./lib/scripts/route.js";
5-
import { updateStores } from "./lib/scripts/api.js";
65
import { initPlaybackAudio } from "./lib/scripts/playback.js";
76
import { initializeMediaSession } from "./lib/scripts/mediasession.js";
87
import PlayerBarComponent from "./lib/components/PlayerBarComponent.svelte";
@@ -11,34 +10,63 @@
1110
1211
// TODO remove this import when manual refresh logic is no longer needed
1312
import { clearStorage } from "./lib/scripts/storage.js";
14-
import { playlistsStore } from "./lib/scripts/api.js";
13+
import { playlistsStore, initStores, updateStores } from "./lib/scripts/api.js";
1514
1615
$: $pathName;
1716
$: $component;
1817
18+
let intervalId;
19+
20+
// @ts-ignore
21+
const version = __APP_VERSION__;
22+
1923
onMount(() => {
2024
async function async() {
21-
await updateStores();
25+
await initStores();
2226
initPlaylist();
2327
initPlaybackAudio();
2428
initializeMediaSession();
2529
initializeRoute();
30+
backgroundFetch();
2631
}
2732
async();
2833
});
2934
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+
3054
// This is a temporary function to handle refresh logic.
3155
// It can be replaced with a more specific implementation later.
3256
async function refresh() {
57+
clearInterval(intervalId);
3358
clearStorage();
3459
playlistsStore.set([]);
35-
await updateStores();
60+
await initStores();
61+
backgroundFetch();
3662
}
3763
</script>
3864

3965
<div class="app-layout bg-dark">
4066
<!-- Sticky Top Bar -->
41-
<header class="top-bar"><div class="container-fluid h-100">{$pathName}</div></header>
67+
<header class="top-bar">
68+
<div class="container-fluid h-100">{$pathName} <span style="font-size: 0.8rem;">(v{version})</span></div>
69+
</header>
4270

4371
<!-- Scrollable Content -->
4472
<main class="scrollable-content">

MyMusicClientSveltePwa/src/lib/pages/Home.svelte

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
{#if $playlistsStore.length > 0}
1212
{#each $playlistsStore as playlist}
13-
<PlaylistComponent {playlist} />
14-
{/each}
13+
<PlaylistComponent {playlist} />
14+
{/each}
1515
{:else}
1616
<p class="text-center">Working.....</p>
1717
{/if}
18-

MyMusicClientSveltePwa/src/lib/pages/Playlist.svelte

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
<script>
22
// @ts-nocheck
33
4-
import { onMount } from "svelte";
4+
import { onDestroy, onMount } from "svelte";
55
import { writable } from "svelte/store";
66
import SongComponent from "../components/SongComponent.svelte";
7-
import { getPlaylistSongs } from "../scripts/api";
7+
import { getPlaylistSongs, writablePlaylistsStore } from "../scripts/api";
8+
import { setSongs } from "../scripts/playlist";
9+
import { on } from "svelte/events";
810
911
let songs = writable([]);
1012
export let playlistId = -1;
1113
12-
$: $songs;
14+
let intervalId;
15+
16+
$: writablePlaylistsStore[playlistId];
1317
1418
onMount(() => {
1519
songs.set(getPlaylistSongs(playlistId));
20+
writablePlaylistsStore[playlistId].subscribe((value) => {
21+
songs.set(value);
22+
});
23+
24+
});
25+
26+
onDestroy(() => {
27+
console.log("Cleaning up interval for playlist songs");
28+
clearInterval(intervalId);
1629
});
1730
</script>
1831

MyMusicClientSveltePwa/src/lib/scripts/api.js

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,100 @@
11
// @ts-nocheck
2-
import { writable } from "svelte/store";
2+
import { writable, get } from "svelte/store";
33
import { storePlaylists, storePlaylistSongs, getPlaylistsStore, getPlaylistSongsStore } from "./storage.js";
44

55
const baseApiUrl = import.meta.env.VITE_BASE_API_URL;
66

7-
let playlistsArray = new Array();
8-
let playlistsSongsMap = new Map();
7+
const playlistsArray = new Array();
8+
const playlistsSongsMap = new Map();
99

1010
export const playlistsStore = writable([]);
1111

12-
export async function updateStores() {
12+
export const writablePlaylistsStore = [];
13+
14+
export async function initStores() {
1315
// check localStorage for playlists and songs
1416
// if not found, fetch from API
15-
let cachedPlaylists = getPlaylistsStore();
17+
const cachedPlaylists = getPlaylistsStore();
1618

1719
if (cachedPlaylists.length > 0) {
1820
for (const playlist of cachedPlaylists) {
19-
playlistsArray.push(playlist);
21+
playlistsArray.push(playlist);
2022
let songs = getPlaylistSongsStore(playlist.id);
2123
playlistsSongsMap.set(playlist.id, songs);
24+
25+
writablePlaylistsStore[playlist.id] = writable(songs);
2226
}
2327

2428
playlistsStore.set(playlistsArray);
2529
console.log("Loaded playlists from localStorage");
2630
} else {
27-
let playlists = await fetch(`${baseApiUrl}/playlist`)
31+
console.log("Fetching playlists from API");
32+
const playlists = await fetch(`${baseApiUrl}/playlist`)
2833
.then((response) => response.json())
2934
.then((data) => data.Data)
3035
.catch((error) => console.error("Error fetching playlists:", error));
3136

32-
playlistsArray = [];
37+
playlistsArray.length = 0;;
3338
playlistsSongsMap.clear();
3439

3540
for (const playlist of playlists) {
3641
playlistsArray.push(playlist);
37-
let songs = await fetchPlaylistSongs(playlist.id);
42+
const songs = await fetchPlaylistSongs(playlist.id);
3843
playlistsSongsMap.set(playlist.id, songs);
3944

4045
storePlaylistSongs(playlist.id, songs);
46+
47+
writablePlaylistsStore[playlist.id] = writable(songs);
4148
}
4249

4350
playlistsStore.set(playlistsArray);
4451
storePlaylists(playlistsArray);
4552
}
4653
}
4754

55+
export async function updateStores() {
56+
// Update plaists
57+
const cachedPlaylists = getPlaylistsStore();
58+
const lastKnowPlaylistId = cachedPlaylists.at(-1).id;
59+
const playlists = await fetchPlaylists(lastKnowPlaylistId);
60+
61+
for (const playlist of playlists) {
62+
playlistsArray.push(playlist);
63+
}
64+
65+
playlistsStore.set(playlistsArray);
66+
storePlaylists(playlistsArray);
67+
68+
// Update songs for each playlist
69+
console.log("starting to update songs for each playlist");
70+
for (const playlist of playlistsArray) {
71+
72+
let lastKnowSongPosition = playlistsSongsMap.get(playlist.id).length;
73+
74+
if (!lastKnowSongPosition) {
75+
lastKnowSongPosition = 0; // Default to 0 if no songs are found
76+
}
77+
78+
const songs = await fetchPlaylistSongs(playlist.id, lastKnowSongPosition);
79+
80+
if (songs.length > 0) {
81+
// Add local notification for new songs?
82+
console.log(`Found (${songs.length}) new songs for playlist ID: ${playlist.id} with last known song position: ${lastKnowSongPosition}`);
83+
playlistsSongsMap.set(playlist.id, [...playlistsSongsMap.get(playlist.id), ...songs]);
84+
storePlaylistSongs(playlist.id, playlistsSongsMap.get(playlist.id));
85+
86+
const songList = get(writablePlaylistsStore[playlist.id]);
87+
88+
for (const song of songs) {
89+
songList.push(song);
90+
}
91+
92+
writablePlaylistsStore[playlist.id].set(songList);
93+
}
94+
}
95+
console.log("Finished updating songs for each playlist");
96+
}
97+
4898
export function getImageUrl(imagePath) {
4999
if (!imagePath) return null;
50100
return `${baseApiUrl}/images/${imagePath}`;
@@ -69,8 +119,16 @@ export function getPlaylistById(playlistId) {
69119
}
70120
}
71121

72-
async function fetchPlaylistSongs(playlistId) {
73-
let songs = await fetch(`${baseApiUrl}/playlist/${playlistId}`)
122+
async function fetchPlaylists(lastKnowPlaylistId) {
123+
const playlists = await fetch(`${baseApiUrl}/playlist?lastKnowPlaylistId=${lastKnowPlaylistId}`)
124+
.then((response) => response.json())
125+
.then((data) => data.Data)
126+
.catch((error) => console.error("Error fetching playlists:", error));
127+
return playlists;
128+
}
129+
130+
async function fetchPlaylistSongs(playlistId, lastKnowSongId = 0) {
131+
const songs = await fetch(`${baseApiUrl}/playlist/${playlistId}?lastKnowSongPosition=${lastKnowSongId}`)
74132
.then((response) => response.json())
75133
.then((data) => data.Data)
76134
.catch((error) => console.error("Error fetching playlist songs:", error));

MyMusicClientSveltePwa/src/lib/scripts/playlist.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,14 @@ export function getCurrentPlaylistId() {
3232
}
3333

3434
export function setSongs(playlistId) {
35-
if (get(currentPlaylistId) === playlistId) {
36-
return; // No need to update if the same playlist is set
37-
}
3835
currentPlaylistId.set(playlistId); // Update the current playlist ID
3936
originalPlaylist = getPlaylistSongs(playlistId);
4037
currentPlaylist = originalPlaylist.slice(); // Create a copy of the original playlist
41-
currentIndex = 0; // Reset index when setting new songs
42-
4338
storeCurrentPlaylist(currentPlaylist, playlistId, get(isShuffleEnabled), get(isRepeatEnabled));
4439
}
4540

4641
export function getCurrentSong() {
4742
let song = currentPlaylist[currentIndex];
48-
storeCurrentSong(currentIndex, 0);
4943
return song;
5044
}
5145

MyMusicClientSveltePwa/src/lib/scripts/storage.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export function storeCurrentSong(index, time = 0) {
3131

3232
export function storeCurrentPlaylist(playlist, id, shuffle = false, repeat = false) {
3333
let data = { playlist, id, shuffle, repeat };
34-
console.log("Storing current playlist:", data);
3534
setItem(currentPlaylistKey, data);
3635
}
3736

0 commit comments

Comments
 (0)