Generate sensible stats when no journeys are logged
[harbour-pedalo.git] / qml / cover / CoverPage.qml
1 import QtQuick 2.0
2 import Sailfish.Silica 1.0
3 import harbour.pedalo.settings 1.0
4
5 CoverBackground {
6     id: root
7     property bool update: status === Cover.Active && currentStatus.cycling
8
9     onUpdateChanged: {
10         stopwatch.updateseconds()
11         stopwatchtrigger.restart()
12     }
13
14     allowResize: true
15
16     property int _maximumItems: height >= Theme.coverSizeLarge.height
17                                 ? 2
18                                 : height >= Theme.coverSizeSmall.height ? 1 : 0
19
20
21     Item {
22         id: stopwatch
23         property int seconds: 0
24         property int fast: seconds < 60 * 10
25         property bool update: root.update
26
27         Timer {
28             id: stopwatchtrigger
29             interval: stopwatch.fast ? 200 : 5000
30             running: root.update
31             repeat: true
32             triggeredOnStart: true
33             onTriggered: {
34                 stopwatch.updateseconds()
35             }
36         }
37
38         function updateseconds() {
39             stopwatch.seconds = (new Date().getTime() - currentStatus.startTime) / 1000
40         }
41     }
42
43     Item {
44         id: contents
45
46         width: Theme.coverSizeLarge.width
47         height: Theme.coverSizeLarge.height
48         property real xScale: root.width / contents.width
49
50         transform: Scale {
51             xScale: contents.xScale
52             yScale: root.height / contents.height
53         }
54
55         ClockView {
56             id: clockView
57
58             anchors {
59                 top: parent.top
60                 left: parent.left
61                 right: parent.right
62                 margins: Theme.paddingLarge
63             }
64             height: width
65         }
66
67         LargeItem {
68             id: stopwatchView
69
70             anchors {
71                 top: clockView.bottom
72                 topMargin: Theme.paddingMedium
73                 leftMargin: Theme.paddingLarge
74                 rightMargin: Theme.paddingLarge
75             }
76
77             titleVisible: ((!root.update) && _maximumItems > 0) || (_maximumItems > 1)
78             textVisible: (root.update && (_maximumItems > 0))
79
80             title: qsTr("Pedalo")
81
82             text: {
83                 if (stopwatch.seconds < 60) {
84                     return qsTr("%n seconds", "", stopwatch.seconds)
85                 } else if (stopwatch.seconds < 60*60) {
86                     return qsTr("%1 minutes").arg(Math.floor(stopwatch.seconds/60))
87                 } else {
88                     var minutes = Math.floor(stopwatch.seconds/60)
89                     var hours = Math.floor(minutes/60)
90                     minutes = minutes % 60
91                     return qsTrId("%1h %2min").arg(hours).arg(minutes)
92                 }
93             }
94         }
95     }
96
97     CoverActionList {
98         id: coverAction
99
100         CoverAction {
101             iconSource: currentStatus.cycling ? Settings.getImageUrl("icon-cover-journey-finish") : Settings.getImageUrl("icon-cover-journey-start")
102             onTriggered: {
103                 if (currentStatus.cycling) {
104                     app.activate()
105                     app.showMainPage(PageStackAction.Immediate)
106
107                     var dialog = pageStack.push(Qt.resolvedUrl("../pages/JourneyEdit.qml"), {title: "Finish journey", start: journeymodel.epochToDateTime(currentStatus.startTime), duration: currentStatus.getDuration()})
108
109                     dialog.accepted.connect(function() {
110                         currentStatus.cycling = false
111                     })
112                 }
113                 else {
114                     currentStatus.startJourney()
115                 }
116             }
117         }
118     }
119 }