Very basic workout player

This commit is contained in:
2025-07-18 10:06:24 +02:00
parent 39006435b2
commit ad98db7f7c
7 changed files with 190 additions and 109 deletions

63
assets/js/play.js Normal file
View File

@@ -0,0 +1,63 @@
document.addEventListener('alpine:init', () => {
Alpine.data('exercisePlayer', () => {
return {
timer: null,
counter: 0,
timeLeft: 0,
currentPosition: 0,
exercises: [],
isPaused: false,
init() {
this.exercises = window.exerciseData;
},
start() {
if (!this.isPaused) {
this.timeLeft = this.exercises[this.currentPosition].time;
this.counter = 0;
}
this.timer = setInterval(() => {
if (this.timeLeft === 0) {
this.next()
};
this.counter++;
this.timeLeft--;
}, 1000);
},
destroy() {
// Detach the handler, avoiding memory and side-effect leakage
if (this.timer) {
clearInterval(this.timer);
}
if (!this.isPaused) {
this.counter = 0;
this.timeLeft = 0;
}
},
next() {
this.destroy()
this.currentPosition++;
if (this.exercises[this.currentPosition].exercise_type === "Time") {
this.start()
}
},
previous() {
this.destroy();
this.currentPosition--;
},
pause() {
if (this.isPaused) {
this.start();
this.isPaused = false;
} else {
this.isPaused = true;
this.destroy()
}
}
}
})
})