Sebastian Mark
31179d4af4
- change mode from "Work" to "Focus" in server messages
- modify pomodoro configuration to use "Focus" instead of "Work"
- adjust default settings to reflect new terminology
- ensure all references to work duration are updated to focus duration
- update variable names in HTML and JavaScript for clarity
🤖
116 lines
2.2 KiB
Go
116 lines
2.2 KiB
Go
package pomodoro
|
|
|
|
import (
|
|
"sync"
|
|
"time"
|
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
)
|
|
|
|
var mu sync.Mutex // to synchronize access to shared state
|
|
var timer Timer
|
|
|
|
// Update `State` with the remaining seconds of the passed timer
|
|
// until the timer sends an End or Abort signal
|
|
func waitForTimer(t Timer) bool {
|
|
for {
|
|
select {
|
|
case <-t.End:
|
|
return true
|
|
case <-t.Abort:
|
|
return false
|
|
case shared.State.TimeLeft = <-t.TimeLeft:
|
|
// do nothing, just let the timer update the message
|
|
}
|
|
}
|
|
}
|
|
|
|
// RunPomodoro iterates the Pomodoro focus/break sessions
|
|
func RunPomodoro() {
|
|
mu.Lock()
|
|
shared.State.Ongoing = true
|
|
shared.State.Paused = false
|
|
mu.Unlock()
|
|
|
|
pomodoroConfig := shared.State.Settings
|
|
|
|
for session := 1; session <= pomodoroConfig.Sessions; session++ {
|
|
timer = timer.Init()
|
|
|
|
shared.State.Session = session
|
|
|
|
// Focus
|
|
shared.State.Mode = "Focus"
|
|
timer.StartAsync(pomodoroConfig.Focus)
|
|
if !waitForTimer(timer) {
|
|
break
|
|
}
|
|
|
|
// Breaks
|
|
if session < pomodoroConfig.Sessions {
|
|
shared.State.Mode = "ShortBreak"
|
|
timer.StartAsync(pomodoroConfig.ShortBreak)
|
|
if !waitForTimer(timer) {
|
|
break
|
|
}
|
|
} else { // last phase, prepare for finish
|
|
shared.State.Mode = "LongBreak"
|
|
timer.StartAsync(pomodoroConfig.LongBreak)
|
|
if !waitForTimer(timer) {
|
|
break
|
|
}
|
|
|
|
// send "End" state for one second
|
|
shared.State.Mode = "End"
|
|
time.Sleep(time.Second)
|
|
}
|
|
}
|
|
|
|
mu.Lock()
|
|
shared.State.Ongoing = false
|
|
shared.State.Paused = false
|
|
mu.Unlock()
|
|
|
|
shared.State.Mode = "Idle"
|
|
shared.State.Session = 0
|
|
shared.State.TimeLeft = shared.State.Settings.Focus
|
|
}
|
|
|
|
func ResetPomodoro() {
|
|
timer.Stop()
|
|
}
|
|
|
|
func PausePomodoro() {
|
|
mu.Lock()
|
|
shared.State.Paused = true
|
|
mu.Unlock()
|
|
|
|
timer.Pause()
|
|
}
|
|
|
|
func ResumePomodoro() {
|
|
mu.Lock()
|
|
shared.State.Paused = false
|
|
mu.Unlock()
|
|
timer.Resume()
|
|
}
|
|
|
|
func IsPomodoroOngoing() bool {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return shared.State.Ongoing
|
|
}
|
|
|
|
func IsPomodoroPaused() bool {
|
|
mu.Lock()
|
|
defer mu.Unlock()
|
|
return shared.State.Paused
|
|
}
|
|
|
|
func UpdateSettings(settings models.PomodoroConfig) {
|
|
if settings != (models.PomodoroConfig{}) {
|
|
shared.State.Settings = settings
|
|
shared.State.TimeLeft = settings.Focus
|
|
}
|
|
}
|