pomodoro: move timers to service, specific button logic to widget

This commit is contained in:
end-4
2025-08-09 19:45:26 +07:00
parent 1f4568d22f
commit 5bf80dae4e
4 changed files with 67 additions and 61 deletions
@@ -49,6 +49,7 @@ Singleton {
property JsonObject pomodoro: JsonObject { property JsonObject pomodoro: JsonObject {
property bool running: false property bool running: false
property int start: 0 property int start: 0
property bool isBreak: false
} }
property JsonObject stopwatch: JsonObject { property JsonObject stopwatch: JsonObject {
property bool running: false property bool running: false
@@ -45,23 +45,6 @@ Item {
} }
} }
Timer {
id: pomodoroTimer
interval: 200
running: Pomodoro.isPomodoroRunning
repeat: true
onTriggered: Pomodoro.refreshPomodoro()
}
Timer {
id: stopwatchTimer
interval: 10
running: Pomodoro.isStopwatchRunning
repeat: true
onTriggered: Pomodoro.refreshStopwatch()
}
ColumnLayout { ColumnLayout {
anchors.fill: parent anchors.fill: parent
spacing: 0 spacing: 0
@@ -61,7 +61,9 @@ Item {
Layout.preferredWidth: 90 Layout.preferredWidth: 90
font.pixelSize: Appearance.font.pixelSize.larger font.pixelSize: Appearance.font.pixelSize.larger
onClicked: Pomodoro.toggleStopwatch() onClicked: {
Pomodoro.toggleStopwatch()
}
colBackground: Pomodoro.isStopwatchRunning ? Appearance.colors.colSecondaryContainer : Appearance.colors.colPrimary colBackground: Pomodoro.isStopwatchRunning ? Appearance.colors.colSecondaryContainer : Appearance.colors.colPrimary
colBackgroundHover: Pomodoro.isStopwatchRunning ? Appearance.colors.colSecondaryContainerHover : Appearance.colors.colPrimaryHover colBackgroundHover: Pomodoro.isStopwatchRunning ? Appearance.colors.colSecondaryContainerHover : Appearance.colors.colPrimaryHover
@@ -79,7 +81,12 @@ Item {
implicitWidth: 90 implicitWidth: 90
font.pixelSize: Appearance.font.pixelSize.larger font.pixelSize: Appearance.font.pixelSize.larger
onClicked: Pomodoro.stopwatchResetOrLaps() onClicked: {
if (Pomodoro.isStopwatchRunning)
Pomodoro.stopwatchRecordLap()
else
Pomodoro.stopwatchReset()
}
enabled: Pomodoro.stopwatchTime !== 0 enabled: Pomodoro.stopwatchTime !== 0
colBackground: Pomodoro.isStopwatchRunning ? Appearance.colors.colLayer2 : Appearance.colors.colErrorContainer colBackground: Pomodoro.isStopwatchRunning ? Appearance.colors.colLayer2 : Appearance.colors.colErrorContainer
+57 -42
View File
@@ -21,7 +21,7 @@ Singleton {
property string alertSound: Config.options.time.pomodoro.alertSound property string alertSound: Config.options.time.pomodoro.alertSound
property bool isPomodoroRunning: Persistent.states.timer.pomodoro.running property bool isPomodoroRunning: Persistent.states.timer.pomodoro.running
property bool isBreak: false property bool isBreak: Persistent.states.timer.pomodoro.isBreak
property bool isPomodoroReset: !isPomodoroRunning property bool isPomodoroReset: !isPomodoroRunning
property int timeLeft: focusTime property int timeLeft: focusTime
property int pomodoroSecondsLeft: focusTime property int pomodoroSecondsLeft: focusTime
@@ -33,35 +33,24 @@ Singleton {
property int stopwatchStart: Persistent.states.timer.stopwatch.start property int stopwatchStart: Persistent.states.timer.stopwatch.start
property var stopwatchLaps: Persistent.states.timer.stopwatch.laps property var stopwatchLaps: Persistent.states.timer.stopwatch.laps
// General
Component.onCompleted: { Component.onCompleted: {
if (!isStopwatchRunning) stopwatchReset() if (!isStopwatchRunning) stopwatchReset()
} }
// Start and Stop button function getCurrentTimeInSeconds() { // Pomodoro uses Seconds
function togglePomodoro() { return Math.floor(Date.now() / 1000)
isPomodoroReset = false
Persistent.states.timer.pomodoro.running = !isPomodoroRunning
if (isPomodoroRunning) { // Pressed Start button
Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds()
} else { // Pressed Stop button
timeLeft -= (getCurrentTimeInSeconds() - pomodoroStart)
}
} }
// Reset button function getCurrentTimeIn10ms() { // Stopwatch uses 10ms
function resetPomodoro() { return Math.floor(Date.now() / 10)
Persistent.states.timer.pomodoro.running = false
isBreak = false
isPomodoroReset = true
timeLeft = focusTime
Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds()
pomodoroCycle = 1
refreshPomodoro()
} }
// Pomodoro
function refreshPomodoro() { function refreshPomodoro() {
// Work <-> break ?
if (getCurrentTimeInSeconds() >= pomodoroStart + timeLeft) { if (getCurrentTimeInSeconds() >= pomodoroStart + timeLeft) {
isBreak = !isBreak Persistent.states.timer.pomodoro.isBreak = !isBreak
Persistent.states.timer.pomodoro.start += timeLeft Persistent.states.timer.pomodoro.start += timeLeft
timeLeft = isBreak ? breakTime : focusTime timeLeft = isBreak ? breakTime : focusTime
@@ -86,45 +75,71 @@ Singleton {
pomodoroSecondsLeft = (pomodoroStart + timeLeft) - getCurrentTimeInSeconds() pomodoroSecondsLeft = (pomodoroStart + timeLeft) - getCurrentTimeInSeconds()
} }
function getCurrentTimeInSeconds() { // Pomodoro uses Seconds Timer {
return Math.floor(Date.now() / 1000) id: pomodoroTimer
interval: 200
running: root.isPomodoroRunning
repeat: true
onTriggered: Pomodoro.refreshPomodoro()
} }
function getCurrentTimeIn10ms() { // Stopwatch uses 10ms function togglePomodoro() {
return Math.floor(Date.now() / 10) isPomodoroReset = false
Persistent.states.timer.pomodoro.running = !isPomodoroRunning
if (isPomodoroRunning) { // Pressed Start button
Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds()
} else { // Pressed Stop button
timeLeft -= (getCurrentTimeInSeconds() - pomodoroStart)
}
} }
function refreshStopwatch() { // stopwatch stores time in 10ms function resetPomodoro() {
Persistent.states.timer.pomodoro.running = false
Persistent.states.timer.pomodoro.isBreak = false
isPomodoroReset = true
timeLeft = focusTime
Persistent.states.timer.pomodoro.start = getCurrentTimeInSeconds()
pomodoroCycle = 1
refreshPomodoro()
}
// Stopwatch
function refreshStopwatch() { // Stopwatch stores time in 10ms
stopwatchTime = getCurrentTimeIn10ms() - stopwatchStart stopwatchTime = getCurrentTimeIn10ms() - stopwatchStart
} }
// Stopwatch functions Timer {
function toggleStopwatch() { id: stopwatchTimer
Persistent.states.timer.stopwatch.running = !isStopwatchRunning interval: 10
if (isStopwatchRunning) { running: root.isStopwatchRunning
// Resume from paused time by adjusting start time repeat: true
Persistent.states.timer.stopwatch.start = getCurrentTimeIn10ms() - stopwatchTime onTriggered: root.refreshStopwatch()
}
} }
function stopwatchResetOrLaps() { function toggleStopwatch() {
if (isStopwatchRunning) { if (root.isStopwatchRunning)
recordLaps() root.stopwatchPause()
} else { else
stopwatchReset() root.stopwatchResume()
} }
function stopwatchPause() {
Persistent.states.timer.stopwatch.running = false
}
function stopwatchResume() {
Persistent.states.timer.stopwatch.running = true
Persistent.states.timer.stopwatch.start = getCurrentTimeIn10ms() - stopwatchTime
} }
function stopwatchReset() { function stopwatchReset() {
Persistent.states.timer.stopwatch.running = false Persistent.states.timer.stopwatch.running = false
stopwatchTime = 0 stopwatchTime = 0
stopwatchStart = getCurrentTimeIn10ms() Persistent.states.timer.stopwatch.start = getCurrentTimeIn10ms()
Persistent.states.timer.stopwatch.laps = [] Persistent.states.timer.stopwatch.laps = []
} }
function recordLaps() { function stopwatchRecordLap() {
Persistent.states.timer.stopwatch.laps.push(stopwatchTime) Persistent.states.timer.stopwatch.laps.push(stopwatchTime)
// Reassign to trigger change
// Persistent.states.timer.stopwatch.laps = Persistent.states.timer.stopwatch.laps.slice(0)
} }
} }