break: change the "stop" command to "reset"

- change stop channel to reset channel
- create ResetPomodoro function
- broadcast reset message to all clients
- rename stop button to reset button in index.html

🤖
This commit is contained in:
Sebastian Mark 2024-10-20 10:02:00 +02:00
parent bc3a306c00
commit ffc6913344
4 changed files with 28 additions and 12 deletions

View file

@ -26,7 +26,7 @@
<!-- Buttons to start, pause/resume, and stop the timer --> <!-- Buttons to start, pause/resume, and stop the timer -->
<button id="startButton">Start</button> <button id="startButton">Start</button>
<button id="pauseResumeButton" style="display: none;">Pause</button> <button id="pauseResumeButton" style="display: none;">Pause</button>
<button id="stopButton" style="display: none;">Stop</button> <button id="resetButton" style="display: none;">Reset</button>
<script> <script>
var ws = new WebSocket("ws://localhost:8080/ws"); var ws = new WebSocket("ws://localhost:8080/ws");
@ -66,7 +66,7 @@
// Hide start button and show pause/resume and stop buttons // Hide start button and show pause/resume and stop buttons
document.getElementById("startButton").style.display = "none"; document.getElementById("startButton").style.display = "none";
document.getElementById("pauseResumeButton").style.display = "inline-block"; document.getElementById("pauseResumeButton").style.display = "inline-block";
document.getElementById("stopButton").style.display = "inline-block"; document.getElementById("resetButton").style.display = "inline-block";
// Set the pause/resume button to show "Pause" initially // Set the pause/resume button to show "Pause" initially
isPaused = false; isPaused = false;
@ -88,14 +88,14 @@
} }
}); });
// Stop Button Click Event // Reset Button Click Event
document.getElementById("stopButton").addEventListener("click", function () { document.getElementById("resetButton").addEventListener("click", function () {
ws.send(JSON.stringify({command: "stop"})); ws.send(JSON.stringify({command: "stop"}));
// Reset buttons after stopping // Reset buttons after stopping
document.getElementById("startButton").style.display = "inline-block"; document.getElementById("startButton").style.display = "inline-block";
document.getElementById("pauseResumeButton").style.display = "none"; document.getElementById("pauseResumeButton").style.display = "none";
document.getElementById("stopButton").style.display = "none"; document.getElementById("resetButton").style.display = "none";
}); });
</script> </script>
</body> </body>

View file

@ -1,6 +1,8 @@
package pomodoro package pomodoro
import ( import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"sync" "sync"
) )
@ -15,7 +17,7 @@ const (
var pomodoroRunning bool var pomodoroRunning bool
var pomodoroPaused bool var pomodoroPaused bool
var pomodoroStopChannel = make(chan bool, 1) var pomodoroResetChannel = make(chan bool, 1)
var pomodoroPauseChannel = make(chan bool, 1) var pomodoroPauseChannel = make(chan bool, 1)
var pomodoroResumeChannel = make(chan bool, 1) var pomodoroResumeChannel = make(chan bool, 1)
@ -48,9 +50,23 @@ func RunPomodoroTimer(clients map[*websocket.Conn]bool) {
mu.Unlock() mu.Unlock()
} }
// StopPomodoro sends a signal to stop the running Pomodoro timer. // ResetPomodoro resets the running Pomodoro timer.
func StopPomodoro() { func ResetPomodoro(clients map[*websocket.Conn]bool) {
pomodoroStopChannel <- true mu.Lock()
pomodoroRunning = false // Reset the running state
pomodoroPaused = false // Reset the paused state
mu.Unlock()
// Broadcast the reset message to all clients
broadcast.BroadcastMessage(clients, models.BroadcastMessage{
Mode: "none",
Session: 0,
MaxSession: 0,
TimeLeft: 0,
})
// Send a reset signal to stop any running timers
pomodoroResetChannel <- true
} }
func PausePomodoro() { func PausePomodoro() {

View file

@ -11,8 +11,8 @@ import (
func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode string, session int) bool { func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode string, session int) bool {
for remainingSeconds > 0 { for remainingSeconds > 0 {
select { select {
case <-pomodoroStopChannel: case <-pomodoroResetChannel:
return false // Stop the timer if a stop command is received return false
case <-pomodoroPauseChannel: case <-pomodoroPauseChannel:
mu.Lock() mu.Lock()
pomodoroPaused = true pomodoroPaused = true

View file

@ -34,7 +34,7 @@ func handleClientCommands(ws *websocket.Conn) {
} }
case "stop": case "stop":
if pomodoro.IsPomodoroRunning() { if pomodoro.IsPomodoroRunning() {
pomodoro.StopPomodoro() // Stop the timer in the Pomodoro package pomodoro.ResetPomodoro(Clients) // Reset Pomodoro
} }
case "pause": case "pause":
if pomodoro.IsPomodoroRunning() && !pomodoro.IsPomodoroPaused() { if pomodoro.IsPomodoroRunning() && !pomodoro.IsPomodoroPaused() {