feat: add pause and resume functionality

- implement pause and resume commands in the Pomodoro package
- modify timer logic to handle paused state
- adjust client command handling for pause and resume actions
- update HTML to include pause/resume button

🤖
This commit is contained in:
Sebastian Mark 2024-10-19 18:15:16 +02:00
parent c9501c3bbb
commit bc3a306c00
4 changed files with 94 additions and 22 deletions

View file

@ -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
}