Skip to content

Commit

Permalink
#44 #74 Update cache usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexandrePTJ committed Mar 19, 2023
1 parent 241a4cc commit 142bd45
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/client/kimaiCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* TODO: Removes when std::views is available on MacOS/clang
*/
#ifdef Q_OS_MACOS
#include <range/v3/all.hpp>
# include <range/v3/all.hpp>
#else
#include <ranges>
# include <ranges>
namespace ranges = std;
#endif

Expand All @@ -23,6 +23,8 @@ void KimaiCache::synchronize(const std::shared_ptr<KimaiClient>& client, const s
return;
}

mStatus = KimaiCache::Status::SyncPending;

// Fill what to sync
if (categories.empty())
{
Expand Down Expand Up @@ -68,6 +70,11 @@ void KimaiCache::synchronize(const std::shared_ptr<KimaiClient>& client, const s
}
}

KimaiCache::Status KimaiCache::status() const
{
return mStatus;
}

Customers KimaiCache::customers() const
{
return mCustomers;
Expand Down Expand Up @@ -107,6 +114,7 @@ void KimaiCache::updateSyncProgress(Category finishedCategory)

if (mPendingSync.empty())
{
mStatus = KimaiCache::Status::Ready;
mSyncSemaphore.release();
emit synchronizeFinished();
}
Expand Down
9 changes: 9 additions & 0 deletions src/client/kimaiCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ class KimaiCache : public QObject
Activities
};

enum class Status
{
Empty,
SyncPending,
Ready
};

void synchronize(const std::shared_ptr<KimaiClient>& client, const std::set<Category>& categories = {});
Status status() const;

Customers customers() const;
Projects projects(std::optional<int> customerId) const;
Expand All @@ -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;
};
Expand Down
8 changes: 7 additions & 1 deletion src/gui/activitywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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);
Expand Down
8 changes: 2 additions & 6 deletions src/gui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 142bd45

Please sign in to comment.