-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
926 lines (765 loc) · 28.3 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
// PonyvilleFM Web Interface & Mobile App
// Created by deltaryz
// This represents the current selected stream
// Default is MP3 320 (Best Quality)
let stream_url = "https://dj.bronyradio.com/streamhq.mp3";
let selected_stream = "PonyvilleFM";
let streams = {
"PonyvilleFM": {
urls: {
"Best Quality - MP3 320": "https://dj.bronyradio.com/streamhq.mp3",
"Good Quality - Opus VBR": "https://dj.bronyradio.com/pvfmopus.ogg",
"Okay Quality - Vorbis 112": "https://dj.bronyradio.com/pvfm1.ogg",
"Low Quality - AAC 64": "https://dj.bronyradio.com/pvfm1mobile.aac",
},
albumText: "PonyvilleFM",
albumart: "./pvfm1.png",
lastQuality: 2, // We modify this to keep track of what the user selected
},
"PonyvilleFM2 Chill": {
urls: {
"MP3 128": "https://luna.ponyvillefm.com/listen/pvfm2/radio.mp3",
},
albumText: "PonyvilleFM2 Chill",
albumart: "./pvfm2.png",
lastQuality: 0,
},
"PonyvilleFM3 No DJs": {
urls: {
"MP3 128": "https://dj.bronyradio.com/pvfmfree.mp3",
"Vorbis 112": "https://dj.bronyradio.com/pvfmfree.ogg",
},
albumText: "PonyvilleFM3 No DJs",
albumart: "./pvfm3.png",
lastQuality: 0,
},
"Luna Radio": {
urls: {
"MP3 128": "https://luna.ponyvillefm.com/listen/lunaradio/radio.mp3",
},
albumText: "Luna Radio",
albumart: "./lunaradio.png",
lastQuality: 0,
},
}
// Override lastQuality with local setting if present
for (const [stationName, stationData] of Object.entries(streams)) {
console.log("Checking settings for " + stationName);
let qualitySetting = localStorage.getItem(stationName);
if (qualitySetting) {
console.log("Found, default is " + qualitySetting)
streams[stationName].lastQuality = qualitySetting; // set to that
} else {
console.log("No saved quality");
streams[stationName].lastQuality = 0; // top is default
}
}
// check for selectedStream local setting and apply if necessary
let selected_stream_setting = localStorage.getItem("selectedStream");
if (selected_stream_setting) {
selected_stream = selected_stream_setting;
}
// Set stream_url to what we had last
stream_url = Object.values(streams[selected_stream].urls)[streams[selected_stream].lastQuality];
// We will manipulate these throughout operation of the program
let schedule;
let audio;
let isPlaying = false;
let songHistory = JSON.parse(localStorage.getItem("songHistory"));
if (songHistory == null) songHistory = [];
let sleepTimerCountdown = -1; // -1 means no active timer
let sleepTimerCheckID;
// Metadata for currently playing song
let songDetails = {
artist: "",
title: "",
album: "",
albumShort: "",
albumArt: "",
listeners: "",
startTime: "",
};
// TODO: these should be inside an object or something this is a mess
const playPauseButton = document.getElementById("playPauseButton");
const resetButton = document.getElementById("resetButton");
const volumeControl = document.getElementById("volumeControl");
const artistField = document.getElementById("artist");
const titleField = document.getElementById("title");
const listenersField = document.getElementById("listeners");
const stationSelector = document.getElementById("stationSelector");
const qualitySelector = document.getElementById("qualitySelector");
const installPWAButton = document.getElementById("installPWA");
const googlePlayButton = document.getElementById("googlePlayButton");
const eventDisplay = document.getElementById("eventDisplay");
const eventStatus = document.getElementById("eventStatus");
const eventName = document.getElementById("eventName");
const timeUntilEvent = document.getElementById("timeUntilEvent");
const sleepTimerButton = document.getElementById("sleepTimer");
// Initialize deferredPrompt for use later to show browser install prompt.
let deferredPrompt;
window.addEventListener("beforeinstallprompt", (e) => {
// Prevent the mini-infobar from appearing on mobile
e.preventDefault();
// Stash the event so it can be triggered later.
deferredPrompt = e;
console.log(`'beforeinstallprompt' event was fired.`);
// Make button visible
if (!window.matchMedia("(display-mode: standalone)").matches) {
installPWAButton.hidden = false;
}
});
// Show Google Play if we're not a PWA
if (!window.matchMedia("(display-mode: standalone)").matches) {
googlePlayButton.hidden = false;
}
// Click on "Install PWA"
installPWAButton.addEventListener("click", async () => {
console.log("Click");
// Show the install prompt
deferredPrompt.prompt();
// Wait for the user to respond to the prompt
const { outcome } = await deferredPrompt.userChoice;
// Optionally, send analytics event with outcome of user choice
console.log(`User response to the install prompt: ${outcome}`);
// We've used the prompt and can't use it again, throw it away
deferredPrompt = null;
});
// Click on "Google Play"
googlePlayButton.addEventListener("click", async () => {
window.open("https://play.google.com/store/apps/details?id=com.deltaryz.pvfm", "_blank", "noopener");
});
// Make sure external links don't open in the PWA
document.addEventListener("click", function (e) {
const target = e.target.closest("a"); // Find the closest anchor element
if (target && target.hostname !== window.location.hostname) {
// If the link is external, prevent default behavior
e.preventDefault();
// Open the link in the system's default browser
window.open(target.href, "_blank", "noopener");
}
});
// Set height for PWA
window.resizeTo(500, 700);
window.addEventListener("resize", function (event) {
event.preventDefault();
window.resizeTo(500, 700);
});
// Dropdown to select station
let stationSelectorDropdown = document.createElement("select");
stationSelectorDropdown.id = "select";
stationSelectorDropdown.className = "dropdown";
// Dropdown to select quality
let qualitySelectorDropdown = document.createElement("select");
qualitySelectorDropdown.id = "select";
qualitySelectorDropdown.className = "dropdown";
// Wipe and recreate quality selector based on provided station
let recreateQualityDropdown = function (station) {
qualitySelectorDropdown.options.length = 0; // remove existing options
let currentIndex = 0;
for (const [qualityName, qualityData] of Object.entries(streams[station].urls)) {
const qualityOption = document.createElement("option");
qualityOption.textContent = qualityName;
qualityOption.value = currentIndex;
currentIndex++;
qualitySelectorDropdown.appendChild(qualityOption);
}
qualitySelectorDropdown.selectedIndex = streams[station].lastQuality;
}
// Populate the station selector dropdown
for (const [stationName, stationData] of Object.entries(streams)) {
const stationOption = document.createElement("option");
stationOption.textContent = stationName;
stationSelectorDropdown.appendChild(stationOption);
// select the default one
if (selected_stream == stationName) stationOption.selected = true;
}
// Change streams and update dropdowns
// Respects default stream setting
// DOES NOT RESET / START PLAYING
let changeStreamAndQuality = function (streamName, streamQualityIndex) {
// update streams
streams[streamName].lastQuality = streamQualityIndex;
// save to localStorage
localStorage.setItem(streamName, streamQualityIndex);
localStorage.setItem("selectedStream", streamName);
recreateQualityDropdown(streamName);
const newStreamUrl = Object.values(streams[streamName].urls)[streamQualityIndex];
console.log("Selected new stream URL: " + newStreamUrl);
stopStreaming();
selected_stream = streamName;
stream_url = newStreamUrl;
}
// init dropdowns
changeStreamAndQuality(selected_stream, streams[selected_stream].lastQuality);
// Change the station
stationSelectorDropdown.addEventListener("change", function (event) {
console.log("User selected station: " + event.target.value);
// Respect last selected quality
changeStreamAndQuality(event.target.value, streams[event.target.value].lastQuality);
resetStream();
});
// Change the quality
qualitySelectorDropdown.addEventListener("change", function (event) {
let newUrl = Object.values(streams[selected_stream].urls)[event.target.value];
console.log("User selected quality index: " + event.target.value);
console.log("This becomes URL: " + newUrl)
changeStreamAndQuality(selected_stream, event.target.value);
resetStream();
});
// Add the dropdowns to the page
// document.getElementsByClassName("inner")[0].removeChild(listenersField);
stationSelector.appendChild(stationSelectorDropdown);
stationSelector.appendChild(listenersField);
stationSelector.appendChild(qualitySelectorDropdown);
console.log("Stream URL: " + stream_url);
// set volume to saved value if present
let settings_volume = localStorage.getItem("volume");
if (settings_volume) {
console.log("Saved volume setting is present:\n" + settings_volume);
if (audio != undefined) audio.volume = settings_volume;
volumeControl.value = settings_volume * 100;
}
console.log("Volume: " + volumeControl.value);
// Handle buttons
playPauseButton.addEventListener("click", () => {
playPause();
});
resetButton.addEventListener("click", () => {
resetStream();
});
// Play/pause function
function playPause() {
if (isPlaying) {
audio.pause();
} else {
audio.play();
// TODO: put this in a function and merge with the stuff in fetchSongDetails()
let changeCheck = songDetails.artist + songDetails.title; // the user never sees this
if (songHistory[0] != null) {
if (changeCheck == songHistory[0].artist + songHistory[0].title) {
// Song has not changed since our last save
} else {
songHistory.unshift(structuredClone(songDetails));
}
} else {
songHistory.unshift(structuredClone(songDetails));
}
// cap history length
if (songHistory.length >= 15) songHistory.length = 15;
console.log("History: \n", songHistory);
}
isPlaying = !isPlaying;
updateButtonText();
updateMediaSession();
audio.volume = volumeControl.value / 100;
}
// Kill everything
function stopStreaming() {
if (audio != undefined) {
audio.pause();
audio.src = "";
audio.load();
}
console.log("Stop");
isPlaying = false;
updateButtonText();
}
// Re-initialize the stream
function resetStream() {
// TODO: Change colors of button based on status
console.log("Reset");
stopStreaming();
audio = new Audio(stream_url);
resetButton.innerHTML = "Loading...";
console.log("Resetting stream - " + stream_url);
// Ensure the audio stream plays after it is loaded
audio.addEventListener("canplay", () => {
console.log("Audio can play, starting playback.");
audio.volume = volumeControl.value / 100;
audio.play().catch((error) => {
console.error("Error starting audio:", error);
// TODO: Show error on page
});
isPlaying = true;
fetchSongDetails();
updateButtonText();
resetButton.innerHTML = "↻ Reset";
});
}
// Volume control
volumeControl.addEventListener("input", (event) => {
const volume = event.target.value / 100;
audio.volume = volume;
localStorage.setItem("volume", volume);
});
// Play/pause button update based on isPlaying variable
function updateButtonText() {
playPauseIcon.src = isPlaying ? "pause.svg" : "play.svg";
}
// Update metadata info for browsers and mobile devices
function updateMediaSession() {
if ("mediaSession" in navigator) {
navigator.mediaSession.metadata = new MediaMetadata({
title: songDetails.title,
artist: songDetails.artist,
artwork: [
{
src: songDetails.albumArt,
sizes: "1000x1000",
type: "image/png",
},
],
album: songDetails.album,
});
}
}
// Respond to button controls
if ("mediaSession" in navigator) {
navigator.mediaSession.setActionHandler("play", () => playPause());
navigator.mediaSession.setActionHandler("stop", () => stopStreaming());
navigator.mediaSession.setActionHandler("pause", () => playPause());
}
// Make sure button represents current state of playback on page load
document.addEventListener("DOMContentLoaded", () => {
updateButtonText();
});
// fetch and update song details
async function fetchSongDetails() {
try {
let currentStream = streams[selected_stream];
let response;
let data;
let changeCheck = songDetails.artist + songDetails.title; // the user never sees this
// TODO: Detect unqiue albumart for PVFM2 and Luna since azuracast actually gives us that
// Pull pvfm metadata
response = await fetch("https://ponyvillefm.com/data/nowplaying");
data = await response.json();
nowPlayingData = data.one || {};
songDetails.albumShort = "PVFM";
// TODO: Maybe use a different field than albumText for this?
// Override Luna Radio
if (currentStream.albumText == "Luna Radio") {
nowPlayingData = data.lunaradio || {};
songDetails.albumShort = "Luna";
}
// Override PVFM3
if (currentStream.albumText == "PonyvilleFM3 No DJs") {
nowPlayingData = data.free || {};
songDetails.albumShort = "PVFM3";
}
if (currentStream.albumText == "PonyvilleFM2 Chill") {
nowPlayingData = data.two || {};
songDetails.albumShort = "PVFM2";
}
// Populate local metadata
songDetails.listeners = nowPlayingData.listeners || "0";
songDetails.artist = nowPlayingData.artist || "";
songDetails.title = nowPlayingData.title || "";
songDetails.album = currentStream.albumText; // Use album name associated with URL
songDetails.albumArt = currentStream.albumart; // Use album art associated with URL
// Update page
artistField.innerHTML = songDetails.artist;
titleField.innerHTML = songDetails.title;
listenersField.innerHTML = "🐴 " + songDetails.listeners;
// Check if the song has changed
if (changeCheck != songDetails.artist + songDetails.title) {
console.log("Song has changed");
songDetails.startTime = Date.now();
// TODO: put this in a function and merge with the stuff in playPause()
// only update now playing if it's actually playing
if (isPlaying) {
// put at the beginning of songHistory
if (songHistory[0] != undefined) {
// check for duplicate
if (songHistory[0].title != songDetails.title) songHistory.unshift(structuredClone(songDetails));
} else {
songHistory.unshift(structuredClone(songDetails));
}
} else {
console.log("Song is not actually playing, not saving");
}
// cap history length
if (songHistory.length >= 15) songHistory.length = 15;
console.log("History: \n", songHistory);
// save in localStorage
localStorage.setItem("songHistory", JSON.stringify(songHistory));
} else {
console.log("Song has not changed")
}
// Display time until upcoming event
if (schedule != undefined) calculateTimeUntilEvent(schedule[0]);
// Update media controls
updateMediaSession();
console.log("Current song details:", songDetails);
} catch (error) {
// TODO: Visualize error on page
console.error("Error fetching song details:", error);
}
}
// fetch schedule from PVFM endpoint
async function fetchSchedule() {
const url = "https://ponyvillefm.com/data/schedule";
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error("Network response was not ok");
}
const data = await response.json();
if (data.result && data.result.length > 0) {
schedule = data.result;
const firstEvent = data.result[0];
// Sorry ND but your name shouldnt be in the title, its also right below it
let firstEventName = firstEvent.name;
if (firstEventName == "Lantern In The Dark with Nicolas Dominique") firstEventName = "Lantern In The Dark";
// Log the event details
console.log(`First event: ${firstEvent.name}`);
console.log(
`Event starts at: ${new Date(
firstEvent.start_unix * 1000
).toLocaleString()}`
);
// Call the function to calculate and print the remaining time
calculateTimeUntilEvent(firstEvent);
eventDisplay.hidden = false;
// Put upcoming event title in box
eventName.innerHTML =
"<b>" +
firstEventName +
'</b> <br /><i class="eventPresentedBy">presented by ' +
firstEvent.host +
"</i>";
} else {
console.log("No schedule entries found");
}
} catch (error) {
console.error("There was a problem with the fetch operation:", error);
}
}
// Song history modal
let historyButton = document.getElementById("historyButton");
historyButton.onclick = function () {
console.log("Activating song history modal")
var modalElement = document.createElement('div');
modalElement.id = "historyModal";
let modalTitle = document.createElement('div');
modalTitle.id = 'modalTitle';
modalTitle.innerHTML = "Playback History";
modalElement.appendChild(modalTitle);
for (songHistoryIndex in songHistory) {
let songHistoryElement = songHistory[songHistoryIndex];
let songTime = new Date(songHistoryElement.startTime);
// container for each thing
let containerDiv = document.createElement('div');
containerDiv.id = "songHistoryContainer";
modalElement.appendChild(containerDiv);
// each station gets an icon
let stationIcon = document.createElement('img');
switch (songHistoryElement.albumShort) {
case 'PVFM':
stationIcon.src = "./pvfm1_small.png";
break;
case 'PVFM2':
stationIcon.src = "./pvfm2_small.png";
break;
case 'PVFM3':
stationIcon.src = "./pvfm3_small.png";
break;
case 'Luna':
stationIcon.src = "./lunaradio_small.png";
break;
}
stationIcon.style.height = "40px";
containerDiv.appendChild(stationIcon);
// actual metadata
let songData = document.createElement('div');
songData.id = "songHistoryData";
songData.innerHTML = "<b>" + songTime.toLocaleTimeString("en-US") + "</b><br/>" + songHistoryElement.artist + "<br/>" + songHistoryElement.title;
containerDiv.appendChild(songData);
}
modalElement.style.fontSize = 9;
mui.overlay('on', modalElement);
}
// Prep schedule modal when schedule button is clicked
let scheduleButton = document.getElementById("scheduleButton");
scheduleButton.onclick = function () {
console.log("Activating schedule modal")
var modalElement = document.createElement('div');
modalElement.id = "scheduleModal"
let modalTitle = document.createElement('div')
modalTitle.id = "modalTitle";
modalTitle.innerHTML = "Upcoming Shows";
let modalSubtitle = document.createElement('div');
modalSubtitle.id = "modalSubtitle";
modalSubtitle.innerHTML = "/ / / <a href='https://ponyvillefm.com/shows'>Full Shows List</a> / / / <a href='https://ponyvillefm.com/team'>Our Team</a> / / / <a href='https://mixes.deltaryz.com'>ƥMIX</a> / / /";
modalElement.appendChild(modalTitle);
// TODO: Unify this with calculateTimeUntilEvent() somehow?
let currentEventIndex = 1;
// construct divs for each event
for (pvfmEventIndex in schedule) {
let pvfmEvent = schedule[pvfmEventIndex];
// Calculate date
const eventDate = new Date(pvfmEvent.start_unix * 1000);
const eventEndDate = new Date(pvfmEvent.end_unix * 1000);
const now = new Date();
// Calculate the difference in milliseconds
const timeDiffMs = eventDate - now;
const timeEndDiffMs = eventEndDate - now;
// Convert the difference to hours and minutes
const hoursLeft = Math.floor(timeDiffMs / (1000 * 60 * 60));
const minutesLeft = Math.floor(
(timeDiffMs % (1000 * 60 * 60)) / (1000 * 60)
);
// Format options for time display
const options = {
timeZoneName: "short",
hour: "numeric",
minute: "numeric",
day: "numeric",
month: "short",
};
const localTimeScheduled = eventDate.toLocaleString(undefined, options);
// Container for each event
let eventContainerDiv = document.createElement('div');
eventContainerDiv.className = "eventDisplay";
// Inner metadata
let eventStatusDiv = document.createElement('div');
eventStatusDiv.className = "eventStatus";
let eventNameDiv = document.createElement('div');
eventNameDiv.className = "eventName";
let eventTimeUntilDiv = document.createElement('div');
eventTimeUntilDiv.className = "timeUntilEvent";
// Add those to the container
eventContainerDiv.appendChild(eventStatusDiv);
eventContainerDiv.appendChild(eventNameDiv);
eventContainerDiv.appendChild(eventTimeUntilDiv);
eventNameDiv.innerHTML =
"<b>" +
pvfmEvent.name +
'</b> <br /><i class="eventPresentedBy">presented by ' +
pvfmEvent.host +
"</i>";
eventTimeUntilDiv.innerHTML =
localTimeScheduled +
"<br/>" +
"Starts in " +
hoursLeft +
" hours " +
minutesLeft +
" minutes";
// Has the event started yet?
if (timeDiffMs > 0) {
// Event has not started
// cap events displayed
if (currentEventIndex <= 4) modalElement.appendChild(eventContainerDiv);
} else {
if (timeEndDiffMs > 0) {
// Event is live
} else {
// Event has passed
// Do nothing
}
}
currentEventIndex++;
console.log(pvfmEvent);
}
modalElement.appendChild(modalSubtitle);
mui.overlay('on', modalElement);
}
// calculate and print remaining time until the event
function calculateTimeUntilEvent(show) {
const eventDate = new Date(show.start_unix * 1000);
const eventEndDate = new Date(show.end_unix * 1000);
// Get the current date and time
const now = new Date();
// Calculate the difference in milliseconds
const timeDiffMs = eventDate - now;
const timeEndDiffMs = eventEndDate - now;
// Has the event started yet?
if (timeDiffMs > 0) {
// Event has not started
eventStatus.innerHTML = "Next event on PVFM:";
eventStatus.style.color = "rgba(255, 255, 255, 0.7)";
// Convert the difference to hours and minutes
const hoursLeft = Math.floor(timeDiffMs / (1000 * 60 * 60));
const minutesLeft = Math.floor(
(timeDiffMs % (1000 * 60 * 60)) / (1000 * 60)
);
// Format options for time display
const options = {
timeZoneName: "short",
hour: "numeric",
minute: "numeric",
day: "numeric",
month: "short",
};
const localTimeScheduled = eventDate.toLocaleString(undefined, options);
console.log(
`Time left until ${localTimeScheduled} "${show.name}": ${hoursLeft} hours and ${minutesLeft} minutes`
);
timeUntilEvent.hidden = false;
timeUntilEvent.innerHTML =
localTimeScheduled +
"<br/>" +
"Starts in " +
hoursLeft +
" hours " +
minutesLeft +
" minutes";
// Should this be calculated independently from the metadata update?
} else {
if (timeEndDiffMs > 0) {
// Event is live
eventStatus.innerHTML = "<b>LIVE NOW ON PVFM</b>";
eventStatus.style.color = "yellow"
timeUntilEvent.innerHTML = "";
console.log(`The event "${show.name}" is live!`);
} else {
// Event has passed
console.log(`The event "${show.name}" is over.`);
fetchSchedule();
}
}
}
// No scrolling or zooming
function disableScrollAndZoom() {
const contentElement = document.querySelector(".inner");
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
const contentWidth = contentElement.scrollWidth;
const contentHeight = contentElement.scrollHeight;
// Disable scroll and zoom only if the content fits
if (contentWidth <= viewportWidth && contentHeight <= viewportHeight) {
document.body.style.overflow = "hidden";
// Add touch listeners to prevent zoom and scroll
document.addEventListener("touchstart", preventZoom, { passive: false });
document.addEventListener("touchmove", preventScroll, { passive: false });
document.addEventListener("wheel", preventZoom, { passive: false });
} else {
// Enable scroll if the content does not fit
document.body.style.overflow = "auto";
// Remove event listeners to allow zoom and scroll
document.removeEventListener("touchstart", preventZoom);
document.removeEventListener("touchmove", preventScroll);
document.removeEventListener("wheel", preventZoom);
}
}
// No zooming
function preventZoom(e) {
if (e.touches != undefined) {
if (e.touches.length > 1) {
e.preventDefault();
}
}
}
// Prevent scroll only outside of slider elements
function preventScroll(e) {
if (document.getElementById("historyModal") == null) {
const isSlider = e.target.closest(".slider");
if (!isSlider) {
e.preventDefault();
}
} else {
document.body.style.overflow = "auto";
}
}
// Run the function on page load and resize
window.addEventListener("resize", disableScrollAndZoom);
window.addEventListener("mui.overlay.off", disableScrollAndZoom);
window.onload = () => {
disableScrollAndZoom();
audio = new Audio(stream_url);
audio.preload = "none";
};
// Keep track of sleep timer
let sleepTimerCheck = function () {
// use clearInterval(sleepTimerCheckID) to stop
console.log("Sleep timer check, current value: ", sleepTimerCountdown);
if (sleepTimerCountdown != -1) {
// timer is active
sleepTimerCountdown--;
sleepTimerButton.innerHTML = "💤 " + minutesToHHMM(sleepTimerCountdown);
// check if timer has reached 0
if (sleepTimerCountdown == 0) {
console.log("Sleep timer completed, stopping stream...");
stopStreaming();
clearInterval(sleepTimerCheckID);
sleepTimerCountdown = -1;
sleepTimerButton.innerHTML = "💤 Timer";
sleepTimerButton.style.color = "#ffffff80";
}
} else {
sleepTimerButton.innerHTML = "💤 Timer";
sleepTimerButton.style.color = "#ffffff80";
}
}
sleepTimerButton.onclick = function () {
// show sleep timer config modal
let modalElement = document.createElement('div');
modalElement.id = "sleepTimerModal";
let modalTitle = document.createElement('div');
modalTitle.id = "modalTitle";
modalTitle.innerHTML = "Sleep Timer";
let minuteBox = document.createElement('input');
minuteBox.id = "minuteBox";
minuteBox.type = "text";
minuteBox.inputMode = "numeric";
minuteBox.pattern = "[0-9]*";
minuteBox.maxLength = 4;
minuteBox.placeholder = "Minutes";
// populate box with existing timer if active
if (sleepTimerCountdown > 0) minuteBox.value = sleepTimerCountdown;
let okButton = document.createElement('button');
okButton.id = "sleepTimerOK";
okButton.innerHTML = "<b>OK</b>";
let cancelButton = document.createElement('button');
cancelButton.id = "sleepTimerCancel";
cancelButton.innerHTML = "Stop";
modalElement.appendChild(modalTitle);
modalElement.appendChild(minuteBox);
modalElement.appendChild(okButton);
modalElement.appendChild(document.createElement('div')); // <br/> breaks it for some reason
modalElement.appendChild(cancelButton);
// user has pressed the OK button
okButton.onclick = function () {
// validate input
let sleepTimerInput = parseInt(minuteBox.value);
if (Number.isNaN(sleepTimerInput) || sleepTimerInput < 1) {
// invalid input
console.log("Invalid input");
} else {
// valid input
// make sure we kill any existing timer
clearInterval(sleepTimerCheckID);
console.log("Now engaging sleep timer...", sleepTimerInput);
sleepTimerCountdown = sleepTimerInput;
sleepTimerCheckID = setInterval(sleepTimerCheck, 60000);
sleepTimerButton.innerHTML = "💤 " + minutesToHHMM(sleepTimerCountdown);
sleepTimerButton.style.color = "yellow";
mui.overlay('off', modalElement);
}
};
// user has pressed the Stop button
cancelButton.onclick = function () {
console.log("Cancelling sleep timer...");
clearInterval(sleepTimerCheckID); // stop checking the timer
sleepTimerCountdown = -1;
sleepTimerCheck();
mui.overlay('off', modalElement);
};
mui.overlay('on', modalElement);
}
// Converts minutes to HH:MM string
function minutesToHHMM(minutes) {
const hours = Math.floor(minutes / 60);
const remainingMinutes = minutes % 60;
const formattedHours = String(hours).padStart(2, '0');
const formattedMinutes = String(remainingMinutes).padStart(2, '0');
return `${formattedHours}:${formattedMinutes}`;
}
// Run the fetch schedule function
fetchSchedule();
// Call the function initially
fetchSongDetails();
// Set up the interval to call the function every 20 seconds
// TODO: Make this adjustable
setInterval(fetchSongDetails, 20000);