Corrently add, edit and list journey details
[harbour-pedalo.git] / qml / pages / AddJourney.qml
index 2330b13..9d7a436 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,39 +46,28 @@ Dialog {
 
             ValueButton {
                 id: startDate
-                property date date: new Date()
                 label: "Date"
-                value: Qt.formatDate(date, 'd MMM yyyy')
+                value: Qt.formatDate(start, 'd MMM yyyy')
                 width: parent.width
                 onClicked: {
-                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: value })
+                    var dialog = pageStack.push("Sailfish.Silica.DatePickerDialog", { date: start })
                     dialog.accepted.connect(function() {
-                        date = dialog.date
+                        start = new Date(dialog.year, dialog.month - 1, dialog.day, start.getHours(), start.getMinutes())
                     })
                 }
-                onDateChanged: {
-                    value = Qt.formatDate(date, 'd MMM yyyy')
-                }
             }
 
             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 {
@@ -72,30 +80,25 @@ Dialog {
                     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)
+                        duration = ((endTime.time.getHours() - start.getHours()) * 60) + (endTime.time.getMinutes() - start.getMinutes()) * 60
                     })
                 }
                 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("Sailfish.Silica.TimePickerDialog", { 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 {
@@ -104,6 +107,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
@@ -115,6 +119,7 @@ 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()
@@ -123,10 +128,13 @@ Dialog {
     }
 
     onAccepted: {
-        var start = new Date(startDate.date.getFullYear(), startDate.date.getMonth(), startDate.date.getDate(), startTime.time.getHours(), startTime.time.getMinutes())
-        var duration = (durationTime.duration.getHours() * 24 * 60) + (durationTime.duration.getMinutes() * 60) + (durationTime.duration.getSeconds())
         var overtook = parseInt(faster.text)
         var overtakenby = parseInt(slower.text)
-        journeymodel.addJourney(start, duration, overtook, overtakenby)
+        if (index < 0) {
+            journeymodel.addJourney(start, duration, overtook, overtakenby)
+        }
+        else {
+            journeymodel.editJourney(index, start, duration, overtook, overtakenby)
+        }
     }
 }