From cb6616f400db3d0c294a25da16c89a8ac030ac3d Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Tue, 22 Oct 2024 08:51:22 +0200 Subject: [PATCH] break: add `updateSettings` command to modify Pomodoro settings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add `updateSettings` command to modify Pomodoro configuration - remove ability to set Pomodoro configuration in `start` command - update demo client - update README 🤖 --- README.md | 37 +++++++---------- index.html | 60 +++++++++++++++++---------- internal/pomodoro/pomodoro.go | 16 +++---- internal/websocket/client_commands.go | 15 ++++--- pkg/models/client.go | 6 +-- 5 files changed, 75 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index b651eb1..2b4316c 100644 --- a/README.md +++ b/README.md @@ -28,30 +28,25 @@ If the client sends a command with an incorrect password it will not be allowed Here are the available commands: -| Command | Action | Example Sent by Client | -|----------|-------------------------------------|-----------------------------------| -| `start` | Starts a new Pomodoro session | `{"command": "start", "password": ""}` | -| `pause` | Pauses the current session | `{"command": "pause", "password": ""}` | -| `resume` | Resumes a paused session | `{"command": "resume", "password": ""}` | -| `reset` | Stops and resets the current session| `{"command": "reset", "password": ""}` | +| Command | Action | Example Sent by Client | +| ---------- | ------------------------------------- | ----------------------------------- | +| `start` | Starts a new Pomodoro session | `{"command": "start", "password": ""}` | +| `pause` | Pauses the current session | `{"command": "pause", "password": ""}` | +| `resume` | Resumes a paused session | `{"command": "resume", "password": ""}` | +| `reset` | Stops and resets the current session | `{"command": "reset", "password": ""}` | +| `updateSettings` | Update Pomodoro settings | `{"command": "updateSettings", "password": "", "settings": {"work": 600, "shortBreak": 60, "longBreak": 300, "sessions": 2}}` | -#### Optional Start Parameters +#### Update Settings Command (`updateSettings`) -The Start-Command may contain an optional Pomodoro-Config, which allows you to customize the length of the work session, short break, long break, and the number of sessions. If no configuration is provided, the server will use default values. +The `updateSettings` command allows clients to modify the Pomodoro timer configuration, including the length of work sessions, short breaks, long breaks, and the total number of sessions in a cycle. This command must include a valid password to be accepted by the server. The updateSettings command can be used when the timer is stopped. -Example: -```json -{ - command: "start", - password: "foobar", - config: { - "work": 10, // Length of the work session in seconds - "shortBreak": 5, // Length of the short break in seconds - "longBreak": 10, // Length of the long break in seconds - "sessions": 2 // Number of total sessions - } -} -``` +- Fields in the `settings` Object: + - `work`: Length of the work session (in seconds). + - `shortBreak`: Length of the short break (in seconds). + - `longBreak`: Length of the long break (in seconds). + - `sessions`: Total number of work/break sessions in the Pomodoro cycle. + +All fields are mandatory and may not be ommitted. ### Server Messages diff --git a/index.html b/index.html index 24aaaf7..f04bf34 100644 --- a/index.html +++ b/index.html @@ -20,6 +20,13 @@ input { width: 75px; } + + .settings { + border: 2px solid black; + background: #e7e7e7; + padding: 10px; + display: inline-block; + } @@ -27,23 +34,27 @@

Pomodoro Timer

-

- - -

-

- - -
- - -
- - -
- - -

+
+

+ + +

+

+ + +
+ + +
+ + +
+ + +
+ +

+
Connting to server...
@@ -83,8 +94,8 @@ return minutes.toString().padStart(2, '0') + ":" + remainingSeconds.toString().padStart(2, '0'); } - // Start Button Click Event - document.getElementById("startButton").addEventListener("click", function () { + // saveButton Click Event + document.getElementById("saveButton").addEventListener("click", function () { // Get the values from the input fields var password = document.getElementById("password").value; var work = parseInt(document.getElementById("workDuration").value); @@ -94,15 +105,22 @@ // Send the start command with the custom config and password ws.send(JSON.stringify({ - command: "start", + command: "updateSettings", password: password, - config: { + settings: { work: work, shortBreak: shortBreak, longBreak: longBreak, sessions: sessions } })); + }); + + // Start Button Click Event + document.getElementById("startButton").addEventListener("click", function () { + var password = document.getElementById("password").value; + // Send the start command with the custom config and password + ws.send(JSON.stringify({command: "start", password: password})); // Hide start button and show pause/resume and reset buttons document.getElementById("startButton").style.display = "none"; diff --git a/internal/pomodoro/pomodoro.go b/internal/pomodoro/pomodoro.go index 1b994b9..65ef873 100644 --- a/internal/pomodoro/pomodoro.go +++ b/internal/pomodoro/pomodoro.go @@ -2,7 +2,7 @@ package pomodoro import ( "git.smsvc.net/pomodoro/GoTomato/internal/shared" - "git.smsvc.net/pomodoro/GoTomato/pkg/models" + // "git.smsvc.net/pomodoro/GoTomato/pkg/models" "sync" ) @@ -13,28 +13,28 @@ 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) { +func RunPomodoro() { mu.Lock() shared.Message.Ongoing = true shared.Message.Paused = false mu.Unlock() - shared.Message.PomodoroSettings = config + pomodoroConfig := shared.Message.PomodoroSettings - for session := 1; session <= config.Sessions; session++ { + for session := 1; session <= pomodoroConfig.Sessions; session++ { shared.Message.Session = session shared.Message.Mode = "Work" - if !startTimer(config.Work) { + if !startTimer(pomodoroConfig.Work) { break } - if session == config.Sessions { + if session == pomodoroConfig.Sessions { shared.Message.Mode = "LongBreak" - if !startTimer(config.LongBreak) { + if !startTimer(pomodoroConfig.LongBreak) { break } } else { shared.Message.Mode = "ShortBreak" - if !startTimer(config.ShortBreak) { + if !startTimer(pomodoroConfig.ShortBreak) { break } } diff --git a/internal/websocket/client_commands.go b/internal/websocket/client_commands.go index c53f45b..0687046 100644 --- a/internal/websocket/client_commands.go +++ b/internal/websocket/client_commands.go @@ -9,8 +9,6 @@ import ( "log" ) -var pomodoroConfig = shared.DefaultPomodoroConfig - // handleClientCommands listens for commands from WebSocket clients func handleClientCommands(ws *websocket.Conn) { for { @@ -35,10 +33,7 @@ func handleClientCommands(ws *websocket.Conn) { switch clientCommand.Command { case "start": if !pomodoro.IsPomodoroOngoing() { - if clientCommand.Config != shared.UnsetPomodoroConfig { - pomodoroConfig = clientCommand.Config - } - go pomodoro.RunPomodoro(pomodoroConfig) // Start the timer with the list of clients + go pomodoro.RunPomodoro() // Start the timer with the list of clients } case "stop": if pomodoro.IsPomodoroOngoing() { @@ -52,6 +47,14 @@ func handleClientCommands(ws *websocket.Conn) { if pomodoro.IsPomodoroOngoing() && pomodoro.IsPomodoroPaused() { pomodoro.ResumePomodoro() // Resume the timer } + case "updateSettings": + if !pomodoro.IsPomodoroOngoing() { + if clientCommand.PomodoroSettings != shared.UnsetPomodoroConfig { + shared.Message.PomodoroSettings = clientCommand.PomodoroSettings + shared.Message.TimeLeft = clientCommand.PomodoroSettings.Work + } + + } } } diff --git a/pkg/models/client.go b/pkg/models/client.go index aa134de..95d8d44 100644 --- a/pkg/models/client.go +++ b/pkg/models/client.go @@ -8,9 +8,9 @@ import ( // ClientCommand represents a command from the client (start/stop). type ClientCommand struct { - Command string `json:"command"` // comman send to the server - Password string `json:"password"` // pomodoro control password - Config GoTomatoPomodoroConfig `json:"config"` // pomodoro config + Command string `json:"command"` // comman send to the server + Password string `json:"password"` // pomodoro control password + PomodoroSettings GoTomatoPomodoroConfig `json:"settings"` // pomodoro config } type Client struct {