Add duration entry dialog
[harbour-pedalo.git] / qml / pages / AddJourney.qml
index b0d8170..a9124ae 100644 (file)
@@ -5,6 +5,25 @@ Dialog {
     id: addJourneyDialog
     canAccept: true
     property string title: "Add journey"
+    property var start: new Date()
+    property int duration: 0
+    property int overtook: -1
+    property int overtakenby: -1
+    property int index: -1
+
+    onDurationChanged: {
+        var structured = new Date(0, 0, 0, 0, parseInt(duration / 60))
+        durationTime.value = Qt.formatTime(structured, 'hh:mm')
+        endTime.time = new Date(0, 0, 0, start.getHours() + structured.getHours(), start.getMinutes() + structured.getMinutes())
+    }
+
+    onStartChanged: {
+        startDate.value = Qt.formatDate(start, 'd MMM yyyy')
+        startTime.value = Qt.formatTime(start, 'hh:mm')
+
+        var structured = new Date(0, 0, 0, 0, parseInt(duration / 60))
+        endTime.time = new Date(0, 0, 0, start.getHours() + structured.getHours(), start.getMinutes() + structured.getMinutes())
+    }
 
     // The effective value will be restricted by ApplicationWindow.allowedOrientations
     allowedOrientations: Orientation.All
@@ -27,41 +46,28 @@ Dialog {
 
             ValueButton {
                 id: startDate
-                function openDateDialog() {
-                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", {
-                                    date: value
-                                 })
-
+                label: "Date"
+                value: Qt.formatDate(start, 'd MMM yyyy')
+                width: parent.width
+                onClicked: {
+                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: start })
                     dialog.accepted.connect(function() {
-                        value = dialog.dateText
-                        selectedDate = dialog.date
+                        start = new Date(dialog.year, dialog.month - 1, dialog.day, start.getHours(), start.getMinutes())
                     })
                 }
-
-                label: "Date"
-                value: Qt.formatDate(new Date(), 'd MMM yyyy')
-                width: parent.width
-                onClicked: openDateDialog()
             }
 
             ValueButton {
                 id: startTime
-                property date time: new Date()
                 label: qsTr("Start time")
-                value: Qt.formatTime(time, 'hh:mm')
+                value: Qt.formatTime(start, 'hh:mm')
                 width: parent.width
                 onClicked: {
-                    console.log("Hours: " + time.getHours())
-                    console.log("Mins: " + time.getMinutes())
-                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
+                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: start.getHours(), minute: start.getMinutes()})
                     dialog.accepted.connect(function() {
-                        time = new Date(0, 0, 0, dialog.hour, dialog.minute)
+                        start = new Date(start.getFullYear(), start.getMonth(), start.getDate(), dialog.hour, dialog.minute)
                     })
                 }
-                onTimeChanged: {
-                    value = Qt.formatTime(time, 'hh:mm')
-                    endTime.time = new Date(0, 0, 0, startTime.time.getHours() + durationTime.duration.getHours(), startTime.time.getMinutes() + durationTime.duration.getMinutes())
-                }
             }
 
             ValueButton {
@@ -73,31 +79,27 @@ Dialog {
                 onClicked: {
                     var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: time.getHours(), minute: time.getMinutes()})
                     dialog.accepted.connect(function() {
-                        time = new Date(0, 0, 0, dialog.hour, dialog.minute)
+                        // We have to hack around the fact that % in Javascript is 'remainder' and not 'mod' (hence will happpily return negative numbers)
+                        duration = ((((((dialog.hour - start.getHours()) * 60) + (dialog.minute - start.getMinutes())) * 60) % 86400) + 86400) % 86400
+                        // Updating the duration will automatically trigger the end time to be udpated
                     })
                 }
                 onTimeChanged: {
                     value = Qt.formatTime(time, 'hh:mm')
-                    durationTime.duration = new Date(0, 0, 0, endTime.time.getHours() - startTime.time.getHours(), endTime.time.getMinutes() - startTime.time.getMinutes())
                 }
             }
 
             ValueButton {
                 id: durationTime
-                property date duration: new Date(0, 0, 0, 0, 0)
                 label: qsTr("Duration")
-                value: Qt.formatTime(duration, 'hh:mm')
+                value: Qt.formatTime(new Date(0, 0, 0, 0, parseInt(duration / 60)), 'hh:mm')
                 width: parent.width
                 onClicked: {
-                    var dialog = pageStack.push("Sailfish.Silica.TimePickerDialog", { hour: duration.getHours(), minute: duration.getMinutes()})
+                    var dialog = pageStack.push("DurationEditDialog.qml", { hour: parseInt(duration / (60 * 60)), minute: parseInt(duration / 60) % 60})
                     dialog.accepted.connect(function() {
-                        duration = new Date(0, 0, 0, dialog.hour, dialog.minute)
+                        duration = ((dialog.hour * 60) + dialog.minute) * 60
                     })
                 }
-                onDurationChanged: {
-                    value = Qt.formatTime(duration, 'hh:mm')
-                    endTime.time = new Date(0, 0, 0, startTime.time.getHours() + durationTime.duration.getHours(), startTime.time.getMinutes() + durationTime.duration.getMinutes())
-                }
             }
 
             TextField {
@@ -106,6 +108,7 @@ Dialog {
                 inputMethodHints: Qt.ImhDigitsOnly
                 label: qsTr("Cycles which you overtook")
                 placeholderText: label
+                text: overtook >= 0 ? "" + overtook : ""
                 horizontalAlignment: TextInput.AlignLeft
                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
                 EnterKey.onClicked: slower.focus = true
@@ -117,10 +120,22 @@ Dialog {
                 inputMethodHints: Qt.ImhDigitsOnly
                 label: qsTr("Cycles which overtook you")
                 placeholderText: label
+                text: overtakenby >= 0 ? "" + overtakenby : ""
                 horizontalAlignment: TextInput.AlignLeft
                 EnterKey.iconSource: "image://theme/icon-m-enter-next"
                 EnterKey.onClicked: addJourneyDialog.accept()
             }
         }
     }
+
+    onAccepted: {
+        var overtook = parseInt(faster.text)
+        var overtakenby = parseInt(slower.text)
+        if (index < 0) {
+            journeymodel.addJourney(start, duration, overtook, overtakenby)
+        }
+        else {
+            journeymodel.editJourney(index, start, duration, overtook, overtakenby)
+        }
+    }
 }