From 369becbf8ec7e28e88700218a4a0a6306c1f0680 Mon Sep 17 00:00:00 2001 From: Kevin Whitaker <eyecreate@eyecreate.org> Date: Wed, 29 Jul 2020 10:10:38 -0400 Subject: [PATCH] Complete oil change logic and connect up to vehicle page. --- src/contents/ui/main.qml | 9 +++++++-- src/db/recordanalytics.cpp | 22 +++++++++++++++++++--- src/main.cpp | 3 +++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/src/contents/ui/main.qml b/src/contents/ui/main.qml index b4624a8..a6511c4 100644 --- a/src/contents/ui/main.qml +++ b/src/contents/ui/main.qml @@ -262,6 +262,7 @@ Kirigami.ApplicationWindow { property var serviceId: Kirigami.PageRouter.data.id Component.onCompleted: { recordModel.changeVehicleFilter(serviceId); + dbAnalytics.checkOilChangeNeededForVehicle(serviceId); } id: serviceRecordPage title: qsTr("Service Records for ") + Kirigami.PageRouter.data.name @@ -286,8 +287,12 @@ Kirigami.ApplicationWindow { type: Kirigami.MessageType.Information id: oilMessage showCloseButton: false - Component.onCompleted: { - //TODO: check if oil change is needed. + text: qsTr("You are due for an oil change!") + Connections { + target: dbAnalytics + onIsOilChangeNeeded: { + oilMessage.visible = changeNeeded; + } } } } diff --git a/src/db/recordanalytics.cpp b/src/db/recordanalytics.cpp index a3302b3..e01544f 100644 --- a/src/db/recordanalytics.cpp +++ b/src/db/recordanalytics.cpp @@ -28,13 +28,29 @@ RecordAnalytics::RecordAnalytics(QObject* parent, QSqlDatabase db) : _db(db) void RecordAnalytics::checkOilChangeNeededForVehicle(int vehicleId, int milesForChange, int monthsForChange) { QtConcurrent::run([&](){ - QSqlQuery query = _db.exec("SELECT * FROM records WHERE records.servicetype = 0;"); + QSqlQuery query = _db.exec("SELECT * FROM records WHERE records.vehicle = "+QString::number(vehicleId)); int milesRecord = query.record().indexOf("miles"); int dateRecord = query.record().indexOf("servicedate"); + int typeRecord = query.record().indexOf("servicetype"); + QDateTime lastOilDate; + int lastOilMiles = 0; + int lastKnownMiles = 0; while(query.next()) { - //TODO: query.value(milesRecord); + if(query.value(typeRecord).toInt() == 0) { + if(!lastOilDate.isValid() || lastOilDate < QDateTime::fromString(query.value(dateRecord).toString(), Qt::DateFormat::ISODate)) { + lastOilDate = QDateTime::fromString(query.value(dateRecord).toString(), Qt::DateFormat::ISODate); + lastOilMiles = query.value(milesRecord).toInt(); + } + } + if(lastKnownMiles < query.value(milesRecord).toInt()) { + lastKnownMiles = query.value(milesRecord).toInt(); + } + } + if(lastKnownMiles-lastOilMiles > milesForChange || lastOilMiles == 0 || !lastOilDate.isValid() || lastOilDate.daysTo(QDateTime::currentDateTime()) > 30*monthsForChange) { + emit this->isOilChangeNeeded(true); + } else { + emit this->isOilChangeNeeded(false); } - emit this->isOilChangeNeeded(false); }); } diff --git a/src/main.cpp b/src/main.cpp index 74e7d82..709e63f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ #include "db/sqlvehicle.h" #include "db/sqlservicerecord.h" #include "jsonio.h" +#include "db/recordanalytics.h" Q_DECL_EXPORT int main(int argc, char *argv[]) { @@ -95,12 +96,14 @@ Q_DECL_EXPORT int main(int argc, char *argv[]) vehicles.select(); records.select(); JsonIO converter(&vehicles, &records); + RecordAnalytics analytics(nullptr, db); QQmlApplicationEngine engine; engine.rootContext()->setContextProperty(QStringLiteral("appAboutData"), QVariant::fromValue(aboutData)); engine.rootContext()->setContextProperty(QStringLiteral("vehicleModel"), &vehicles); engine.rootContext()->setContextProperty(QStringLiteral("recordModel"), &records); engine.rootContext()->setContextProperty(QStringLiteral("jsonConverter"), &converter); + engine.rootContext()->setContextProperty(QStringLiteral("dbAnalytics"), &analytics); engine.load(QUrl(QStringLiteral("qrc:///main.qml"))); if (engine.rootObjects().isEmpty()) { -- GitLab