Skip to content

Commit b2224ed

Browse files
Frontend/settings page (#72)
* did not know this was a bug... * base settings * bypasscache * update interval time * update sleeptimer * store default config * future me * chore: bump version --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent 35b7ec9 commit b2224ed

File tree

8 files changed

+138
-14
lines changed

8 files changed

+138
-14
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.9",
4+
"version": "0.1.10",
55
"type": "module",
66
"scripts": {
77
"dev": "vite --host",
Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,69 @@
1-
<div>
2-
Configure settings here.
3-
</div>
1+
<script>
2+
import { onMount } from "svelte";
3+
import { clearStorage, getConfiguration, setConfiguration } from "../scripts/storageService";
4+
5+
function handleSubmit(event) {
6+
event.preventDefault();
7+
const formData = new FormData(event.target);
8+
const sleepTimer = formData.get("sleepTimer");
9+
const fetchTimer = formData.get("fetchTimer");
10+
const byPassCache = formData.get("byPassCache") === "on";
11+
12+
configuarion.sleepTimer = sleepTimer;
13+
configuarion.fetchTimer = fetchTimer;
14+
configuarion.byPassCache = byPassCache;
15+
16+
setConfiguration(configuarion);
17+
}
18+
19+
let configuarion;
20+
let sleepTimer = $state(0);
21+
let fetchTimer = $state(0);
22+
let byPassCache = $state(false);
23+
24+
onMount(() => {
25+
configuarion = getConfiguration();
26+
sleepTimer = configuarion.sleepTimer;
27+
fetchTimer = configuarion.fetchTimer;
28+
byPassCache = configuarion.byPassCache;
29+
});
30+
</script>
31+
32+
<!-- svelte-ignore event_directive_deprecated -->
33+
<form on:submit|preventDefault={handleSubmit} class="p-2 rounded rounded-2 tile-bg">
34+
<div class="mb-3">
35+
<label for="sleepTimer" class="form-label">Sleep Timer (Minutes)</label>
36+
<input type="number" required class="form-control" id="sleepTimer" name="sleepTimer" value={sleepTimer} placeholder="Stop music after some time" />
37+
</div>
38+
<div class="mb-3">
39+
<label for="fetchTimer" class="form-label">Fetch Timer (Seconds)</label>
40+
<input type="number" required class="form-control" id="fetchTimer" name="fetchTimer" value={fetchTimer} placeholder="Api interval time" />
41+
</div>
42+
<div class="mt-3 mb-3 p-2 rounded rounded-2 tile-bg">
43+
<label for="byPassCache" class="form-label">Ignore Cached urls</label>
44+
<input type="checkbox" checked={byPassCache} id="byPassCache" name="byPassCache" class="form-check-input" />
45+
</div>
46+
<button type="submit" class="btn btn-primary">Save Settings</button>
47+
</form>
48+
49+
<div class="mt-3 mb-3 p-2 rounded rounded-2 tile-bg">
50+
<label for="localStorageClear" class="form-label">Local Storage !Expirmental</label>
51+
<!-- This is causing some issues, playback breaks and updating ui freezes -->
52+
<!-- svelte-ignore event_directive_deprecated -->
53+
<button id="localStorageClear" class="btn btn-danger" on:click={() => clearStorage()}>Clear Local Storage</button>
54+
</div>
55+
56+
<div class="mt-3 mb-3 p-2 rounded rounded-2 tile-bg">
57+
<button id="exportData" class="btn btn-secondary">View Server Logs</button>
58+
</div>
59+
60+
<style>
61+
label {
62+
font-weight: bold;
63+
font-size: 1.1rem;
64+
}
65+
66+
.tile-bg {
67+
background-color: #2c2c2c;
68+
}
69+
</style>

MyMusicClientSveltePwa/src/lib/scripts/api.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const baseApiUrl = import.meta.env.VITE_BASE_API_URL;
22
const staticImageUrl = import.meta.env.VITE_STATIC_IMAGE_URL;
33
const staticAudioUrl = import.meta.env.VITE_STATIC_AUDIO_URL;
44

5+
import { getConfiguration } from "./storageService";
6+
57
export async function fetchPlaylists() {
68
try {
79
const response = await fetch(`${baseApiUrl}/playlist`);
@@ -59,9 +61,23 @@ export async function fetchPlaylistSongs(playlistId) {
5961
}
6062

6163
export function getImageUrl(path) {
64+
65+
var config = getConfiguration();
66+
67+
if(config.byPassCache){
68+
return `${staticImageUrl}/${path}?cb=${new Date().getMilliseconds()}`;
69+
}
70+
6271
return `${staticImageUrl}/${path}`;
6372
}
6473

6574
export function getPlaybackUrl(source_id) {
75+
76+
var config = getConfiguration();
77+
78+
if(config.byPassCache){
79+
return `${staticAudioUrl}/${source_id}.opus?cb=${new Date().getMilliseconds()}`;
80+
}
81+
6682
return `${staticAudioUrl}/${source_id}.opus`; // Assuming all audio files are in .opus format
6783
}

MyMusicClientSveltePwa/src/lib/scripts/playlistService.js

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { writable } from "svelte/store";
2-
import { getCachedPlaylists, setPlaylists, setPlaylistSongs, getCachedPlaylistSongs } from "./storageService";
2+
import { getCachedPlaylists, setPlaylists, setPlaylistSongs, getCachedPlaylistSongs, appConfiguration, getConfiguration } from "./storageService";
33
import { fetchPlaylists, fetchPlaylistSongs, fetchNewPlaylist, fetchNewPlaylistSongs } from "./api";
44

55
export const playlistsStore = writable([]);
66

7-
const updateInterval = 1000 * 3; // 3 seconds
7+
let updateInterval;
88
let isUpdating = false;
9+
let intervalId;
910

1011
// Check storage for stored playlists, if empty fetch from API
1112
export async function initializePlaylistService() {
@@ -22,12 +23,30 @@ export async function initializePlaylistService() {
2223
}
2324
}
2425

25-
setInterval(() => {
26+
updateInterval = getConfiguration().fetchTimer * 1000; // Need to multiply by 1000 to get milliseconds
27+
// Subscribe to configuration changes
28+
// If fetchTimer is updated, clear the old interval and set a new one
29+
30+
appConfiguration.subscribe(config => {
31+
if (intervalId) {
32+
clearInterval(intervalId);
33+
}
34+
updateInterval = config.fetchTimer * 1000; // Need to multiply by 1000 to get milliseconds
35+
36+
console.log("Update interval set to:", updateInterval, "ms");
37+
38+
intervalId = setInterval(() => {
39+
2640
if (isUpdating) return; // Prevent multiple updates at the same time
41+
2742
isUpdating = true;
43+
2844
backgroundUpdate();
45+
2946
isUpdating = false;
47+
3048
}, updateInterval);
49+
});
3150
}
3251

3352
async function backgroundUpdate() {

MyMusicClientSveltePwa/src/lib/scripts/routeService.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import { getSearchParameters, createSearchParameters, searchQuery } from "../scr
88

99
const componentsPathMap = new Map([
1010
["/404", NotFound],
11-
["/Home", Home],
11+
["/home", Home],
1212
["/", Home],
13-
["/Playlists", Playlists],
14-
["/Settings", Settings],
13+
["/playlists", Playlists],
14+
["/settings", Settings],
1515
]);
1616

1717
const NotFoundRoutePath = "/404";
@@ -47,7 +47,10 @@ export function initializeRouteService() {
4747

4848
// Sets the current route and updates the component and parameters accordingly
4949
// If the route does not exist, it sets the NotFound component and parameters
50-
export function navigateTo(newRoute, parameters = null) {
50+
export function navigateTo(_newRoute, parameters = null) {
51+
52+
let newRoute = _newRoute.toLowerCase();
53+
5154
if (!componentsPathMap.has(newRoute)) {
5255
component.set(componentsPathMap.get(NotFoundRoutePath));
5356
componentParams.set({ page: newRoute });

MyMusicClientSveltePwa/src/lib/scripts/sleeptimerService.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// @ts-nocheck
22
import { writable, get } from "svelte/store";
3+
import { getConfiguration } from "./storageService";
34

45
export let timeLeft = writable(0);
56
export let isTimerEnabled = writable(false);
@@ -21,7 +22,9 @@ if (get(isTimerEnabled)) {
2122
return;
2223
}
2324

24-
const totalMinutes = 30; // Default to 30 minutes if no time is provided
25+
const config = getConfiguration();
26+
27+
const totalMinutes = config.sleepTimer; // Default to 30 minutes if no time is provided
2528
let remainingMinutes = totalMinutes;
2629

2730
timeLeft.set(remainingMinutes);

MyMusicClientSveltePwa/src/lib/scripts/storageService.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { get, writable } from "svelte/store";
2+
13
// @ts-ignore
24
const storageType = "localStorage";
35
const PlaylistsKey = "cachedPlaylists";
@@ -7,6 +9,14 @@ const CurrentPlaylistIdKey = "currentPlaylistId";
79
const CurrentSongIndexKey = "currentSongIndex";
810
const CurrentShuffeldPlaylistKey = "currentShuffledPlaylist";
911
const CurrentSongTimeKey = "currentSongTime";
12+
const ConfigKey = "appConfig";
13+
14+
export let appConfiguration = writable(getConfiguration());
15+
16+
export function setConfiguration(config) {
17+
appConfiguration.set(config);
18+
setItem(ConfigKey, config);
19+
}
1020

1121
export function setPlaylists(playlists) {
1222
setItem(PlaylistsKey, playlists);
@@ -73,6 +83,13 @@ export function getCurrentSongTime() {
7383
return getItem(CurrentSongTimeKey) || 0;
7484
}
7585

86+
export function getConfiguration() {
87+
return getItem(ConfigKey) || { sleepTimer: 15, // minutes
88+
fetchTimer: 3, // seconds
89+
byPassCache: false
90+
};
91+
}
92+
7693
export function clearStorage() {
7794
if (storageAvailable(storageType)) {
7895
try {

0 commit comments

Comments
 (0)