|  |  | @ -1,8 +1,6 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <QDebug> |  |  |  | #include <QDebug> | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <QJsonDocument> |  |  |  | #include <QJsonDocument> | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <QJsonObject> |  |  |  | #include <QJsonObject> | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <QLabel> |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <QStackedLayout> |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | #include <QVBoxLayout> |  |  |  | #include <QVBoxLayout> | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | #include "api.hpp" |  |  |  | #include "api.hpp" | 
			
		
	
	
		
		
			
				
					|  |  | @ -11,94 +9,62 @@ | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | const double MILE_TO_KM = 1.60934; |  |  |  | const double MILE_TO_KM = 1.60934; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | void clearLayouts(QLayout* layout) { |  |  |  | static QLayout* build_stat_layout(QLabel** metric, const QString& name) { | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   while (QLayoutItem* item = layout->takeAt(0)) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (QWidget* widget = item->widget()) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       widget->deleteLater(); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     if (QLayout* childLayout = item->layout()) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       clearLayouts(childLayout); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     delete item; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | QLayout* build_stat(QString name, int stat) { |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   QVBoxLayout* layout = new QVBoxLayout; |  |  |  |   QVBoxLayout* layout = new QVBoxLayout; | 
			
		
	
		
		
			
				
					
					|  |  |  |   layout->setMargin(0); |  |  |  |   layout->setMargin(0); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |   *metric = new QLabel("0"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   QLabel* metric = new QLabel(QString("%1").arg(stat)); |  |  |  |   (*metric)->setStyleSheet("font-size: 80px; font-weight: 600;"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   metric->setStyleSheet(R"( |  |  |  |   layout->addWidget(*metric, 0, Qt::AlignLeft); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     font-size: 80px; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     font-weight: 600; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   )"); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   layout->addWidget(metric, 0, Qt::AlignLeft); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   QLabel* label = new QLabel(name); |  |  |  |   QLabel* label = new QLabel(name); | 
			
		
	
		
		
			
				
					
					|  |  |  |   label->setStyleSheet(R"( |  |  |  |   label->setStyleSheet("font-size: 45px; font-weight: 500;"); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |     font-size: 45px; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     font-weight: 500; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   )"); |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   layout->addWidget(label, 0, Qt::AlignLeft); |  |  |  |   layout->addWidget(label, 0, Qt::AlignLeft); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   return layout; |  |  |  |   return layout; | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | void DriveStats::parseError(QString response) { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   clearLayouts(vlayout); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   vlayout->addWidget(new QLabel("No Internet connection"), 0, Qt::AlignCenter); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | void DriveStats::parseResponse(QString response) { |  |  |  | void DriveStats::parseResponse(QString response) { | 
			
		
	
		
		
			
				
					
					|  |  |  |   response.chop(1); |  |  |  |   response = response.trimmed(); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   clearLayouts(vlayout); |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8()); |  |  |  |   QJsonDocument doc = QJsonDocument::fromJson(response.toUtf8()); | 
			
		
	
		
		
			
				
					
					|  |  |  |   if (doc.isNull()) { |  |  |  |   if (doc.isNull()) { | 
			
		
	
		
		
			
				
					
					|  |  |  |     qDebug() << "JSON Parse failed on getting past drives statistics"; |  |  |  |     qDebug() << "JSON Parse failed on getting past drives statistics"; | 
			
		
	
		
		
			
				
					
					|  |  |  |     return; |  |  |  |     return; | 
			
		
	
		
		
			
				
					
					|  |  |  |   } |  |  |  |   } | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   bool metric = Params().read_db_bool("IsMetric"); |  |  |  |   auto update = [](const QJsonObject &obj, StatsLabels& labels, bool metric) { | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     labels.routes->setText(QString::number((int)obj["routes"].toDouble())); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     labels.distance->setText(QString::number(obj["distance"].toDouble() * (metric ? MILE_TO_KM : 1))); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     labels.hours->setText(QString::number((int)(obj["minutes"].toDouble() / 60))); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   }; | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   bool metric = Params().read_db_bool("IsMetric"); | 
			
		
	
		
		
			
				
					
					|  |  |  |   QJsonObject json = doc.object(); |  |  |  |   QJsonObject json = doc.object(); | 
			
		
	
		
		
			
				
					
					|  |  |  |   auto all = json["all"].toObject(); |  |  |  |   update(json["all"].toObject(), all_, metric); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   auto week = json["week"].toObject(); |  |  |  |   update(json["week"].toObject(), week_, metric); | 
			
				
				
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | } | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | DriveStats::DriveStats(QWidget* parent) : QWidget(parent) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   setStyleSheet("QLabel {font-size: 48px; font-weight: 500;}"); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   auto add_stats_layouts = [&](QGridLayout* gl, StatsLabels& labels, int row, const char* distance_unit) { | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     gl->addLayout(build_stat_layout(&labels.routes, "DRIVES"), row, 0, 3, 1); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     gl->addLayout(build_stat_layout(&labels.distance, distance_unit), row, 1, 3, 1); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |     gl->addLayout(build_stat_layout(&labels.hours, "HOURS"), row, 2, 3, 1); | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   }; | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |  |  |  |  |   const char* distance_unit = Params().read_db_bool("IsMetric") ? "KM" : "MILES"; | 
			
		
	
		
		
			
				
					
					|  |  |  |   QGridLayout* gl = new QGridLayout(); |  |  |  |   QGridLayout* gl = new QGridLayout(); | 
			
		
	
		
		
			
				
					
					|  |  |  |   gl->setMargin(0); |  |  |  |   gl->setMargin(0); | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   int all_distance = all["distance"].toDouble() * (metric ? MILE_TO_KM : 1); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addWidget(new QLabel("ALL TIME"), 0, 0, 1, 3); |  |  |  |   gl->addWidget(new QLabel("ALL TIME"), 0, 0, 1, 3); | 
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addLayout(build_stat("DRIVES", all["routes"].toDouble()), 1, 0, 3, 1); |  |  |  |   add_stats_layouts(gl, all_, 1, distance_unit); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addLayout(build_stat(metric ? "KM" : "MILES", all_distance), 1, 1, 3, 1); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addLayout(build_stat("HOURS", all["minutes"].toDouble() / 60), 1, 2, 3, 1); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   int week_distance = week["distance"].toDouble() * (metric ? MILE_TO_KM : 1); |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   gl->addWidget(new QLabel("PAST WEEK"), 6, 0, 1, 3); |  |  |  |   gl->addWidget(new QLabel("PAST WEEK"), 6, 0, 1, 3); | 
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addLayout(build_stat("DRIVES", week["routes"].toDouble()), 7, 0, 3, 1); |  |  |  |   add_stats_layouts(gl, week_, 7, distance_unit); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addLayout(build_stat(metric ? "KM" : "MILES", week_distance), 7, 1, 3, 1); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   gl->addLayout(build_stat("HOURS", week["minutes"].toDouble() / 60), 7, 2, 3, 1); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   QWidget* q = new QWidget; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   q->setLayout(gl); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   vlayout->addWidget(q); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  |  | 
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  | DriveStats::DriveStats(QWidget* parent) : QWidget(parent) { |  |  |  |   QVBoxLayout* vlayout = new QVBoxLayout(this); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   vlayout = new QVBoxLayout(this); |  |  |  |   vlayout->addLayout(gl); | 
			
				
				
			
		
	
		
		
			
				
					
					|  |  |  |   vlayout->setMargin(0); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   setLayout(vlayout); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   setStyleSheet(R"( |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     QLabel { |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       font-size: 48px; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |       font-weight: 500; |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |     } |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  |   )"); |  |  |  |  | 
			
		
	
		
		
	
		
		
	
		
		
			
				
					
					|  |  |  | 
 |  |  |  | 
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   // TODO: do we really need to update this frequently?
 |  |  |  |   // TODO: do we really need to update this frequently?
 | 
			
		
	
		
		
			
				
					
					|  |  |  |   QString dongleId = QString::fromStdString(Params().get("DongleId")); |  |  |  |   QString dongleId = QString::fromStdString(Params().get("DongleId")); | 
			
		
	
		
		
			
				
					
					|  |  |  |   QString url = "https://api.commadotai.com/v1.1/devices/" + dongleId + "/stats"; |  |  |  |   QString url = "https://api.commadotai.com/v1.1/devices/" + dongleId + "/stats"; | 
			
		
	
		
		
			
				
					
					|  |  |  |   RequestRepeater* repeater = new RequestRepeater(this, url, 13); |  |  |  |   RequestRepeater* repeater = new RequestRepeater(this, url, 13, "ApiCache_DriveStats"); | 
			
				
				
			
		
	
		
		
	
		
		
			
				
					
					|  |  |  |   QObject::connect(repeater, SIGNAL(receivedResponse(QString)), this, SLOT(parseResponse(QString))); |  |  |  |   QObject::connect(repeater, SIGNAL(receivedResponse(QString)), this, SLOT(parseResponse(QString))); | 
			
		
	
		
		
			
				
					
					|  |  |  |   QObject::connect(repeater, SIGNAL(failedResponse(QString)), this, SLOT(parseError(QString))); |  |  |  |  | 
			
		
	
		
		
			
				
					
					|  |  |  | } |  |  |  | } | 
			
		
	
	
		
		
			
				
					|  |  | 
 |