aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorent Revest <revestflo@gmail.com>2017-06-12 02:28:12 +0200
committerFlorent Revest <revestflo@gmail.com>2017-06-12 02:28:12 +0200
commit941e702b9d244a9f469bbdef531dffa1c6f3eac4 (patch)
treeb9506e93ee44bcebca92e85e3ceb25cf683ce95b
parent737a0f6f12ac10db634f6d5249ae35a1409f0b4b (diff)
Use persistent values to keep stopwatch counting/paused even when the app is closed
-rw-r--r--main.qml51
1 files changed, 30 insertions, 21 deletions
diff --git a/main.qml b/main.qml
index 9c7c215..92fdf3f 100644
--- a/main.qml
+++ b/main.qml
@@ -20,6 +20,7 @@
import QtQuick 2.5
import org.asteroid.controls 1.0
+import org.nemomobile.configuration 1.0
Application {
id: app
@@ -27,9 +28,21 @@ Application {
centerColor: "#b01c7e"
outerColor: "#420a2f"
- property bool running
- property var previousTime
- property int elapsed: 0
+ ConfigurationValue {
+ id: previousTime
+ key: "/stopwatch/previousTime"
+ defaultValue: -1
+ }
+ ConfigurationValue {
+ id: elapsed
+ key: "/stopwatch/elapsed"
+ defaultValue: -1
+ }
+ ConfigurationValue {
+ id: running
+ key: "/stopwatch/running"
+ defaultValue: false
+ }
function zeroPad(n) {
return (n < 10 ? "0" : "") + n
@@ -56,7 +69,7 @@ Application {
id: mainPage
anchors.fill: parent
- state: "zero"
+ state: running.value ? "running" : elapsed.value == -1 ? "zero" : "paused"
states: [
State { name: "zero" },
State { name: "running" },
@@ -67,7 +80,7 @@ Application {
id: elapsedLabel
textFormat: Text.RichText
anchors.centerIn: parent
- text: toTimeString(elapsed)
+ text: toTimeString(elapsed.value)
font.pixelSize: parent.height*0.25
color: "#FFFFFF"
horizontalAlignment: Text.AlignHCenter
@@ -84,18 +97,19 @@ Application {
MouseArea {
anchors.fill: parent
onClicked: {
- switch (mainPage.state) {
+ console.log("from:" + mainPage.state + " " + elapsed.value + " " + running.value + " " + previousTime.value)
+ switch(mainPage.state) {
case "zero":
case "paused":
- previousTime = new Date;
- stopwatch.start();
- mainPage.state = "running";
+ var curTime = new Date
+ previousTime.value = curTime.getTime()
+ running.value = true
break;
case "running":
- mainPage.state = "paused";
- stopwatch.stop();
+ running.value = false
break;
}
+ console.log("from:" + mainPage.state + " " + elapsed.value + " " + running.value + " " + previousTime.value)
}
}
@@ -114,26 +128,21 @@ Application {
top: parent.top
}
- onClicked: {
- elapsed = 0;
- mainPage.state = "zero"
- }
+ onClicked: elapsed.value = -1
}
}
Timer {
- id: stopwatch
-
interval: 100
repeat: true
- running: false
+ running: mainPage.state == "running"
triggeredOnStart: true
onTriggered: {
var currentTime = new Date
- var delta = (currentTime.getTime() - previousTime.getTime())
- previousTime = currentTime
- elapsed += delta
+ var delta = (currentTime.getTime() - previousTime.value)
+ previousTime.value = currentTime.getTime()
+ elapsed.value += delta
}
}
}