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

View file

@ -1,6 +1,8 @@
package pomodoro
import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
"sync"
)
@ -15,7 +17,7 @@ const (
var pomodoroRunning bool
var pomodoroPaused bool
var pomodoroStopChannel = make(chan bool, 1)
var pomodoroResetChannel = make(chan bool, 1)
var pomodoroPauseChannel = make(chan bool, 1)
var pomodoroResumeChannel = make(chan bool, 1)
@ -48,9 +50,23 @@ func RunPomodoroTimer(clients map[*websocket.Conn]bool) {
mu.Unlock()
}
// StopPomodoro sends a signal to stop the running Pomodoro timer.
func StopPomodoro() {
pomodoroStopChannel <- true
// ResetPomodoro resets the running Pomodoro timer.
func ResetPomodoro(clients map[*websocket.Conn]bool) {
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() {

View file

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

View file

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