From 142bd457886b86b1bb90cdc4ef591ab5fbdec2f3 Mon Sep 17 00:00:00 2001 From: Alexandre Petitjean Date: Sun, 19 Mar 2023 21:41:36 +0100 Subject: [PATCH] #44 #74 Update cache usage --- src/client/kimaiCache.cpp | 12 ++++++++++-- src/client/kimaiCache.h | 9 +++++++++ src/gui/activitywidget.cpp | 8 +++++++- src/gui/mainwindow.cpp | 8 ++------ 4 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/client/kimaiCache.cpp b/src/client/kimaiCache.cpp index d1bbec4..96e2dd7 100644 --- a/src/client/kimaiCache.cpp +++ b/src/client/kimaiCache.cpp @@ -4,9 +4,9 @@ * TODO: Removes when std::views is available on MacOS/clang */ #ifdef Q_OS_MACOS -#include +# include #else -#include +# include namespace ranges = std; #endif @@ -23,6 +23,8 @@ void KimaiCache::synchronize(const std::shared_ptr& client, const s return; } + mStatus = KimaiCache::Status::SyncPending; + // Fill what to sync if (categories.empty()) { @@ -68,6 +70,11 @@ void KimaiCache::synchronize(const std::shared_ptr& client, const s } } +KimaiCache::Status KimaiCache::status() const +{ + return mStatus; +} + Customers KimaiCache::customers() const { return mCustomers; @@ -107,6 +114,7 @@ void KimaiCache::updateSyncProgress(Category finishedCategory) if (mPendingSync.empty()) { + mStatus = KimaiCache::Status::Ready; mSyncSemaphore.release(); emit synchronizeFinished(); } diff --git a/src/client/kimaiCache.h b/src/client/kimaiCache.h index 2d7c9a5..c575102 100644 --- a/src/client/kimaiCache.h +++ b/src/client/kimaiCache.h @@ -23,7 +23,15 @@ class KimaiCache : public QObject Activities }; + enum class Status + { + Empty, + SyncPending, + Ready + }; + void synchronize(const std::shared_ptr& client, const std::set& categories = {}); + Status status() const; Customers customers() const; Projects projects(std::optional customerId) const; @@ -45,6 +53,7 @@ class KimaiCache : public QObject Projects mProjects; Activities mActivities; + kemai::KimaiCache::Status mStatus = kemai::KimaiCache::Status::Empty; std::binary_semaphore mSyncSemaphore{1}; std::mutex mProgressMutex; }; diff --git a/src/gui/activitywidget.cpp b/src/gui/activitywidget.cpp index 73cb794..e118620 100644 --- a/src/gui/activitywidget.cpp +++ b/src/gui/activitywidget.cpp @@ -85,7 +85,7 @@ void ActivityWidget::onCbCustomerTextChanged(const QString& /*text*/) updateControls(); } -void ActivityWidget::onCbProjectTextChanged(const QString& text) +void ActivityWidget::onCbProjectTextChanged(const QString& /*text*/) { updateActivitiesCombo(); updateControls(); @@ -200,6 +200,12 @@ void ActivityWidget::onSecondTimeout() void ActivityWidget::onSessionCurrentTimeSheetChanged() { + // Waiting for cache to be filled before trying to update combos + while (mSession->cache().status() != KimaiCache::Status::Ready) + { + qApp->processEvents(); + } + if (mSession->hasCurrentTimeSheet()) { mUi->dteStartedAt->setDateTime(mSession->currentTimeSheet()->beginAt); diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index d1561bb..4243300 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -214,15 +214,11 @@ void MainWindow::createKemaiSession(const Settings::Profile& profile) connect(mSession.get(), &KemaiSession::currentTimeSheetChanged, this, &MainWindow::onCurrentTimeSheetChanged); connect(mSession.get(), &KemaiSession::pluginsChanged, this, &MainWindow::onPluginsChanged); - // TODO: Change to avoid refresh each time en entry is added - connect(&mSession->cache(), &KimaiCache::synchronizeFinished, this, [this]() { - mSession->refreshSessionInfos(); - mSession->refreshCurrentTimeSheet(); - }); - mActivityWidget->setKemaiSession(mSession); mSession->refreshCache(); + mSession->refreshSessionInfos(); + mSession->refreshCurrentTimeSheet(); // Save profile connection settings.kemai.lastConnectedProfile = profile.id;