GoTomato/internal/pomodoro/pomodoro.go
Sebastian Mark 03ab627729 feat: simplify timer function
- remove "mode" parameter from startTimer function
- update shared.Message.Mode for each pomodoro state
- update comments

🤖
2024-10-21 13:07:19 +02:00

90 lines
1.9 KiB
Go

package pomodoro
import (
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"sync"
)
var pomodoroResetChannel = 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
// RunPomodoro iterates the Pomodoro work/break sessions.
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
mu.Lock()
shared.Message.Ongoing = true
shared.Message.Paused = false
mu.Unlock()
shared.Message.TotalSession = config.Sessions
for session := 1; session <= config.Sessions; session++ {
shared.Message.Session = session
shared.Message.Mode = "Work"
if !startTimer(config.Work) {
break
}
if session == config.Sessions {
shared.Message.Mode = "LongBreak"
if !startTimer(config.LongBreak) {
break
}
} else {
shared.Message.Mode = "ShortBreak"
if !startTimer(config.ShortBreak) {
break
}
}
}
mu.Lock()
shared.Message.Ongoing = false
mu.Unlock()
}
// ResetPomodoro resets the running Pomodoro timer.
func ResetPomodoro() {
// Send a reset signal to stop any running timers
pomodoroResetChannel <- true
mu.Lock()
shared.Message.Ongoing = false
shared.Message.Paused = false
mu.Unlock()
// Reset message
shared.Message.Mode = "none"
shared.Message.Session = 0
shared.Message.TotalSession = 0
shared.Message.TimeLeft = 0
}
func PausePomodoro() {
mu.Lock()
shared.Message.Paused = true
mu.Unlock()
pomodoroPauseChannel <- true
}
func ResumePomodoro() {
mu.Lock()
shared.Message.Paused = false
mu.Unlock()
pomodoroResumeChannel <- true
}
func IsPomodoroOngoing() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return shared.Message.Ongoing
}
func IsPomodoroPaused() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return shared.Message.Paused
}