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

View file

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

View file

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