From 0b0610efc2b93fa44915b9bf5ad1fb1c313bda15 Mon Sep 17 00:00:00 2001 From: Arseniy-Movshev Date: Fri, 21 Jul 2023 13:56:28 +0100 Subject: Add file watcher to stepsDataLoader this is essentially a copy of the previous commit applied to hrDataLoader --- qmlplugin/stepsDataLoader.cpp | 18 ++++++++++++++++-- qmlplugin/stepsDataLoader.h | 3 +++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/qmlplugin/stepsDataLoader.cpp b/qmlplugin/stepsDataLoader.cpp index d78ab94..8983d80 100644 --- a/qmlplugin/stepsDataLoader.cpp +++ b/qmlplugin/stepsDataLoader.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include @@ -32,6 +33,10 @@ StepsDataLoader::StepsDataLoader() : QObject() } else { qDebug()<<"interface is valid"; } + m_fileWatcher = new QFileSystemWatcher(); + m_fileWatcher->addPath(sensorDirPath("stepCounter")); + QObject::connect(m_fileWatcher,SIGNAL(directoryChanged(const QString)),this,SIGNAL(dataChanged())); + QObject::connect(m_fileWatcher,SIGNAL(fileChanged(const QString)),this,SIGNAL(dataChanged())); } int StepsDataLoader::getTodayTotal() { @@ -45,11 +50,16 @@ int StepsDataLoader::getTodayTotal() { int StepsDataLoader::getTotalForDate(QDate date) { // This is obvious garbage. This should really be abstracted and cached, so that every page doesn't have to reload the file from scratch. // The intention is to also add graph functionality at some point. The graph will be simplifying the data before loading it in - it would be worth caching the simplified data when it comes to that as well. - QFile file(fileNameForDate(date, "stepCounter")); + + QString path = fileNameForDate(date, "stepCounter"); + QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "failed to open file"; return 0; } + if (!m_fileWatcher->files().contains(path)) { + m_fileWatcher->addPath(path); + } QTextStream inStream(&file); QString line = "0"; int i; @@ -71,11 +81,15 @@ QVariant StepsDataLoader::getDataForDate(QDate date) { QList StepsDataLoader::getRawDataForDate(QDate date) { QList m_filedata; - QFile file(fileNameForDate(date, "stepCounter")); + QString path = fileNameForDate(date, "stepCounter"); + QFile file(path); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "failed to open file"; return m_filedata; } + if (!m_fileWatcher->files().contains(path)) { + m_fileWatcher->addPath(path); + } QTextStream inStream(&file); QString line; while (!inStream.atEnd()) { diff --git a/qmlplugin/stepsDataLoader.h b/qmlplugin/stepsDataLoader.h index 312a9f5..a248f32 100644 --- a/qmlplugin/stepsDataLoader.h +++ b/qmlplugin/stepsDataLoader.h @@ -14,6 +14,7 @@ #include #include #include +#include #include @@ -33,8 +34,10 @@ public: QList getRawDataForDate(QDate date); signals: void todayTotalChanged(); + void dataChanged(); private: QDBusInterface *m_iface; + QFileSystemWatcher *m_fileWatcher; QStepCounterSensor *m_stepcounterSensor; }; -- cgit v1.2.3-54-g00ecf