Complete oil change logic and connect up to vehicle page.

This commit is contained in:
2020-07-29 10:10:38 -04:00
parent b7e20e3302
commit 369becbf8e
3 changed files with 29 additions and 5 deletions

View File

@@ -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;
}
}
}
}

View File

@@ -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);
}
});
}

View File

@@ -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()) {