From 6e70d0dcf74ddefaf6cbeb37071a0328bf65a790 Mon Sep 17 00:00:00 2001 From: Adam Velebil Date: Sat, 14 Oct 2023 12:54:42 +0200 Subject: [PATCH] refactor Time api --- apps/gtkmm/include/app_window.h | 1 - apps/gtkmm/include/timer.h | 5 +++-- apps/gtkmm/include/worker.h | 7 ++++--- apps/gtkmm/src/app_window.cpp | 3 +-- apps/gtkmm/src/timer.cpp | 9 +++++---- apps/gtkmm/src/worker.cpp | 22 +++++++++++----------- 6 files changed, 24 insertions(+), 23 deletions(-) diff --git a/apps/gtkmm/include/app_window.h b/apps/gtkmm/include/app_window.h index 6be5059..cc2383a 100644 --- a/apps/gtkmm/include/app_window.h +++ b/apps/gtkmm/include/app_window.h @@ -44,7 +44,6 @@ class AppWindow : public Gtk::ApplicationWindow { Glib::Dispatcher signal_devices_change; Glib::Dispatcher signal_accounts_change; - Timer timer; Worker worker; std::thread* workerThread; }; diff --git a/apps/gtkmm/include/timer.h b/apps/gtkmm/include/timer.h index b816508..d34637e 100644 --- a/apps/gtkmm/include/timer.h +++ b/apps/gtkmm/include/timer.h @@ -8,11 +8,12 @@ namespace authppgtk { class Timer { public: - Timer(); - void start(long delay, const std::function& callback); + explicit Timer(std::function&& callback); + void start(long delay); void stop(); private: + std::function m_callback; sigc::connection m_timeout; }; diff --git a/apps/gtkmm/include/worker.h b/apps/gtkmm/include/worker.h index 710b38d..3934376 100644 --- a/apps/gtkmm/include/worker.h +++ b/apps/gtkmm/include/worker.h @@ -2,14 +2,15 @@ #include +#include "timer.h" + #include namespace authppgtk { class AppWindow; -class Timer; class Worker { public: - explicit Worker(Timer&); + Worker(); void run(AppWindow*); void requestAccounts(); @@ -23,7 +24,7 @@ class Worker { std::vector m_devices; std::vector m_accounts; - Timer& m_refresh_timer; + Timer m_refresh_timer; bool m_stopped { false }; bool m_stop_request { false }; bool m_accounts_request { false }; diff --git a/apps/gtkmm/src/app_window.cpp b/apps/gtkmm/src/app_window.cpp index 044c766..bfb0b55 100644 --- a/apps/gtkmm/src/app_window.cpp +++ b/apps/gtkmm/src/app_window.cpp @@ -23,8 +23,7 @@ AppWindow::AppWindow(BaseObjectType* baseObjectType, const Glib::RefPtr::create()) , signal_devices_change() , signal_accounts_change() - , timer() - , worker(timer) + , worker() , workerThread() { auto accountListView = refBuilder->get_widget("listview_accounts"); diff --git a/apps/gtkmm/src/timer.cpp b/apps/gtkmm/src/timer.cpp index d39af2b..3129721 100644 --- a/apps/gtkmm/src/timer.cpp +++ b/apps/gtkmm/src/timer.cpp @@ -11,19 +11,20 @@ namespace { authpp::Logger log("Timer"); } -Timer::Timer() - : m_timeout() +Timer::Timer(std::function&& callback) + : m_callback(std::move(callback)) + , m_timeout() { } -void Timer::start(long delay, const std::function& callback) +void Timer::start(long delay) { if (m_timeout.connected()) { stop(); } auto currentMs = authpp::TimeUtil::getCurrentMilliSeconds(); log.d("Next update in {} ({})", delay, TimeUtil::toString(currentMs + delay)); - m_timeout = Glib::signal_timeout().connect(sigc::bind(callback), delay); + m_timeout = Glib::signal_timeout().connect(sigc::bind(m_callback), delay); } void Timer::stop() diff --git a/apps/gtkmm/src/worker.cpp b/apps/gtkmm/src/worker.cpp index 09adfd7..cf90544 100644 --- a/apps/gtkmm/src/worker.cpp +++ b/apps/gtkmm/src/worker.cpp @@ -8,21 +8,28 @@ #include namespace authppgtk { +using namespace authpp; namespace { authpp::Logger log("Worker"); } -Worker::Worker(Timer& timer) +Worker::Worker() : m_mutex() , m_devices() , m_accounts() - , m_refresh_timer(timer) + , m_refresh_timer([this]() { + std::lock_guard lock(m_mutex); + auto currentMs = TimeUtil::getCurrentMilliSeconds(); + log.d("Passed refresh time (current: {})", TimeUtil::toString(currentMs)); + m_accounts_request = true; + return false; + }) { } void Worker::run(authppgtk::AppWindow* appWindow) { - using namespace authpp; + using namespace authpp::oath; using namespace std::chrono_literals; @@ -69,14 +76,7 @@ void Worker::run(authppgtk::AppWindow* appWindow) appWindow->notify_accounts_change(); int nextRefresh = 30000; - m_refresh_timer.start(nextRefresh, [this]() { - std::lock_guard lock(m_mutex); - - auto currentMs = TimeUtil::getCurrentMilliSeconds(); - log.d("Passed refresh time (current: {})", TimeUtil::toString(currentMs)); - m_accounts_request = true; - return false; - }); + m_refresh_timer.start(nextRefresh); } }