Complete oil change logic and connect up to vehicle page.
This commit is contained in:
@@ -262,6 +262,7 @@ Kirigami.ApplicationWindow {
|
|||||||
property var serviceId: Kirigami.PageRouter.data.id
|
property var serviceId: Kirigami.PageRouter.data.id
|
||||||
Component.onCompleted: {
|
Component.onCompleted: {
|
||||||
recordModel.changeVehicleFilter(serviceId);
|
recordModel.changeVehicleFilter(serviceId);
|
||||||
|
dbAnalytics.checkOilChangeNeededForVehicle(serviceId);
|
||||||
}
|
}
|
||||||
id: serviceRecordPage
|
id: serviceRecordPage
|
||||||
title: qsTr("Service Records for ") + Kirigami.PageRouter.data.name
|
title: qsTr("Service Records for ") + Kirigami.PageRouter.data.name
|
||||||
@@ -286,8 +287,12 @@ Kirigami.ApplicationWindow {
|
|||||||
type: Kirigami.MessageType.Information
|
type: Kirigami.MessageType.Information
|
||||||
id: oilMessage
|
id: oilMessage
|
||||||
showCloseButton: false
|
showCloseButton: false
|
||||||
Component.onCompleted: {
|
text: qsTr("You are due for an oil change!")
|
||||||
//TODO: check if oil change is needed.
|
Connections {
|
||||||
|
target: dbAnalytics
|
||||||
|
onIsOilChangeNeeded: {
|
||||||
|
oilMessage.visible = changeNeeded;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -28,13 +28,29 @@ RecordAnalytics::RecordAnalytics(QObject* parent, QSqlDatabase db) : _db(db)
|
|||||||
void RecordAnalytics::checkOilChangeNeededForVehicle(int vehicleId, int milesForChange, int monthsForChange)
|
void RecordAnalytics::checkOilChangeNeededForVehicle(int vehicleId, int milesForChange, int monthsForChange)
|
||||||
{
|
{
|
||||||
QtConcurrent::run([&](){
|
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 milesRecord = query.record().indexOf("miles");
|
||||||
int dateRecord = query.record().indexOf("servicedate");
|
int dateRecord = query.record().indexOf("servicedate");
|
||||||
|
int typeRecord = query.record().indexOf("servicetype");
|
||||||
|
QDateTime lastOilDate;
|
||||||
|
int lastOilMiles = 0;
|
||||||
|
int lastKnownMiles = 0;
|
||||||
while(query.next()) {
|
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);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -32,6 +32,7 @@
|
|||||||
#include "db/sqlvehicle.h"
|
#include "db/sqlvehicle.h"
|
||||||
#include "db/sqlservicerecord.h"
|
#include "db/sqlservicerecord.h"
|
||||||
#include "jsonio.h"
|
#include "jsonio.h"
|
||||||
|
#include "db/recordanalytics.h"
|
||||||
|
|
||||||
Q_DECL_EXPORT int main(int argc, char *argv[])
|
Q_DECL_EXPORT int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@@ -95,12 +96,14 @@ Q_DECL_EXPORT int main(int argc, char *argv[])
|
|||||||
vehicles.select();
|
vehicles.select();
|
||||||
records.select();
|
records.select();
|
||||||
JsonIO converter(&vehicles, &records);
|
JsonIO converter(&vehicles, &records);
|
||||||
|
RecordAnalytics analytics(nullptr, db);
|
||||||
|
|
||||||
QQmlApplicationEngine engine;
|
QQmlApplicationEngine engine;
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("appAboutData"), QVariant::fromValue(aboutData));
|
engine.rootContext()->setContextProperty(QStringLiteral("appAboutData"), QVariant::fromValue(aboutData));
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("vehicleModel"), &vehicles);
|
engine.rootContext()->setContextProperty(QStringLiteral("vehicleModel"), &vehicles);
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("recordModel"), &records);
|
engine.rootContext()->setContextProperty(QStringLiteral("recordModel"), &records);
|
||||||
engine.rootContext()->setContextProperty(QStringLiteral("jsonConverter"), &converter);
|
engine.rootContext()->setContextProperty(QStringLiteral("jsonConverter"), &converter);
|
||||||
|
engine.rootContext()->setContextProperty(QStringLiteral("dbAnalytics"), &analytics);
|
||||||
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
|
engine.load(QUrl(QStringLiteral("qrc:///main.qml")));
|
||||||
|
|
||||||
if (engine.rootObjects().isEmpty()) {
|
if (engine.rootObjects().isEmpty()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user