diff --git a/index.html b/index.html index 58fa15c..5bde13c 100644 --- a/index.html +++ b/index.html @@ -23,12 +23,14 @@

Pomodoro Timer

Connecting to server...
- + - + + diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index 65705fe..3010244 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -13,12 +13,20 @@ const ( ) var pomodoroRunning bool +var pomodoroPaused bool + +var pomodoroStopChannel = make(chan bool, 1) +var pomodoroPauseChannel = make(chan bool, 1) +var pomodoroResumeChannel = make(chan bool, 1) + var mu sync.Mutex // to synchronize access to shared state // RunPomodoroTimer iterates the Pomodoro work/break sessions. func RunPomodoroTimer(clients map[*websocket.Conn]bool) { mu.Lock() pomodoroRunning = true + pomodoroPaused = false + mu.Unlock() for session := 1; session <= sessions; session++ { if !startTimer(clients, workDuration, "Work", session) { @@ -35,11 +43,32 @@ func RunPomodoroTimer(clients map[*websocket.Conn]bool) { } } + mu.Lock() pomodoroRunning = false mu.Unlock() } -// IsPomodoroRunning returns the status of the timer. +// StopPomodoro sends a signal to stop the running Pomodoro timer. +func StopPomodoro() { + pomodoroStopChannel <- true +} + +func PausePomodoro() { + pomodoroPauseChannel <- true +} + +func ResumePomodoro() { + pomodoroResumeChannel <- true +} + func IsPomodoroRunning() bool { + mu.Lock() + defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done return pomodoroRunning } + +func IsPomodoroPaused() bool { + mu.Lock() + defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done + return pomodoroPaused +} diff --git a/internal/pomodoro/timer.go b/internal/pomodoro/timer.go index c7d931b..28a7f09 100644 --- a/internal/pomodoro/timer.go +++ b/internal/pomodoro/timer.go @@ -7,24 +7,32 @@ import ( "time" ) -var timerStopChannel = make(chan bool, 1) - // startTimer runs the countdown and broadcasts every second. func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode string, session int) bool { for remainingSeconds > 0 { select { - case <-timerStopChannel: + case <-pomodoroStopChannel: return false // Stop the timer if a stop command is received + case <-pomodoroPauseChannel: + mu.Lock() + pomodoroPaused = true + mu.Unlock() + case <-pomodoroResumeChannel: + mu.Lock() + pomodoroPaused = false + mu.Unlock() default: // Broadcast the current state to all clients - broadcast.BroadcastMessage(clients, models.BroadcastMessage{ - Mode: mode, - Session: session, - MaxSession: sessions, - TimeLeft: remainingSeconds, - }) - time.Sleep(time.Second) - remainingSeconds-- + if !pomodoroPaused { + broadcast.BroadcastMessage(clients, models.BroadcastMessage{ + Mode: mode, + Session: session, + MaxSession: sessions, + TimeLeft: remainingSeconds, + }) + time.Sleep(time.Second) + remainingSeconds-- + } } } @@ -38,8 +46,3 @@ func startTimer(clients map[*websocket.Conn]bool, remainingSeconds int, mode str return true } - -// StopTimer sends a signal to stop the running Pomodoro timer. -func StopTimer() { - timerStopChannel <- true -} diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index 41855bf..74f2b4a 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -34,8 +34,17 @@ func handleClientCommands(ws *websocket.Conn) { } case "stop": if pomodoro.IsPomodoroRunning() { - pomodoro.StopTimer() // Stop the timer in the Pomodoro package + pomodoro.StopPomodoro() // Stop the timer in the Pomodoro package + } + case "pause": + if pomodoro.IsPomodoroRunning() && !pomodoro.IsPomodoroPaused() { + pomodoro.PausePomodoro() // Pause the timer + } + case "resume": + if pomodoro.IsPomodoroRunning() && pomodoro.IsPomodoroPaused() { + pomodoro.ResumePomodoro() // Resume the timer } } + } }