refacor: replace shared.Message with shared.State

🤖
This commit is contained in:
Sebastian Mark 2024-10-29 18:46:00 +01:00
parent 5750ec96cb
commit b8823acc97
3 changed files with 22 additions and 22 deletions

View file

@ -18,7 +18,7 @@ func waitForTimer(t Timer) bool {
return true
case <-t.Abort:
return false
case shared.Message.TimeLeft = <-t.TimeLeft:
case shared.State.TimeLeft = <-t.TimeLeft:
// do nothing, just let the timer update the message
}
}
@ -27,48 +27,48 @@ func waitForTimer(t Timer) bool {
// RunPomodoro iterates the Pomodoro work/break sessions.
func RunPomodoro() {
mu.Lock()
shared.Message.Ongoing = true
shared.Message.Paused = false
shared.State.Ongoing = true
shared.State.Paused = false
mu.Unlock()
pomodoroConfig := shared.Message.Settings
pomodoroConfig := shared.State.Settings
for session := 1; session <= pomodoroConfig.Sessions; session++ {
timer = timer.Init()
shared.Message.Session = session
shared.State.Session = session
shared.Message.Mode = "Work"
shared.State.Mode = "Work"
go timer.Start(pomodoroConfig.Work)
if !waitForTimer(timer) {
break
}
if session < pomodoroConfig.Sessions {
shared.Message.Mode = "ShortBreak"
shared.State.Mode = "ShortBreak"
go timer.Start(pomodoroConfig.ShortBreak)
if !waitForTimer(timer) {
break
}
} else { // last phase, prepare for finish
shared.Message.Mode = "LongBreak"
shared.State.Mode = "LongBreak"
go timer.Start(pomodoroConfig.LongBreak)
if !waitForTimer(timer) {
break
}
shared.Message.Mode = "End"
shared.State.Mode = "End"
time.Sleep(time.Second)
}
}
mu.Lock()
shared.Message.Ongoing = false
shared.Message.Paused = false
shared.State.Ongoing = false
shared.State.Paused = false
mu.Unlock()
shared.Message.Mode = "Idle"
shared.Message.Session = 0
shared.Message.TimeLeft = shared.Message.Settings.Work
shared.State.Mode = "Idle"
shared.State.Session = 0
shared.State.TimeLeft = shared.State.Settings.Work
}
func ResetPomodoro() {
@ -77,7 +77,7 @@ func ResetPomodoro() {
func PausePomodoro() {
mu.Lock()
shared.Message.Paused = true
shared.State.Paused = true
mu.Unlock()
timer.Pause()
@ -85,7 +85,7 @@ func PausePomodoro() {
func ResumePomodoro() {
mu.Lock()
shared.Message.Paused = false
shared.State.Paused = false
mu.Unlock()
timer.Resume()
}
@ -93,18 +93,18 @@ func ResumePomodoro() {
func IsPomodoroOngoing() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return shared.Message.Ongoing
return shared.State.Ongoing
}
func IsPomodoroPaused() bool {
mu.Lock()
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
return shared.Message.Paused
return shared.State.Paused
}
func UpdateSettings(settings models.PomodoroConfig) {
if settings != (models.PomodoroConfig{}) {
shared.Message.Settings = settings
shared.Message.TimeLeft = settings.Work
shared.State.Settings = settings
shared.State.TimeLeft = settings.Work
}
}

View file

@ -5,7 +5,7 @@ import (
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
var Message = models.ServerMessage{
var State = models.ServerMessage{
Mode: "Idle",
Settings: DefaultPomodoroConfig,
Session: 0,

View file

@ -14,7 +14,7 @@ func SendPermanentBroadCastMessage() {
tick := time.NewTicker(time.Second)
for {
// Marshal the message into JSON format
jsonMessage, err := json.Marshal(shared.Message)
jsonMessage, err := json.Marshal(shared.State)
if err != nil {
log.Error("Error marshalling message:", "msg", err)
return