Skip to content

Commit

Permalink
refactor Time api
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamVe committed Oct 14, 2023
1 parent a8e3a27 commit 6e70d0d
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 23 deletions.
1 change: 0 additions & 1 deletion apps/gtkmm/include/app_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
5 changes: 3 additions & 2 deletions apps/gtkmm/include/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ namespace authppgtk {

class Timer {
public:
Timer();
void start(long delay, const std::function<bool()>& callback);
explicit Timer(std::function<bool()>&& callback);
void start(long delay);
void stop();

private:
std::function<bool()> m_callback;
sigc::connection m_timeout;
};

Expand Down
7 changes: 4 additions & 3 deletions apps/gtkmm/include/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

#include <libauthpp/oath_session.h>

#include "timer.h"

#include <vector>

namespace authppgtk {
class AppWindow;
class Timer;
class Worker {
public:
explicit Worker(Timer&);
Worker();

void run(AppWindow*);
void requestAccounts();
Expand All @@ -23,7 +24,7 @@ class Worker {

std::vector<authpp::UsbDevice> m_devices;
std::vector<authpp::oath::Credential> m_accounts;
Timer& m_refresh_timer;
Timer m_refresh_timer;
bool m_stopped { false };
bool m_stop_request { false };
bool m_accounts_request { false };
Expand Down
3 changes: 1 addition & 2 deletions apps/gtkmm/src/app_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ AppWindow::AppWindow(BaseObjectType* baseObjectType, const Glib::RefPtr<Gtk::Bui
, accountModel(Gio::ListStore<AccountHolder>::create())
, signal_devices_change()
, signal_accounts_change()
, timer()
, worker(timer)
, worker()
, workerThread()
{
auto accountListView = refBuilder->get_widget<Gtk::ListView>("listview_accounts");
Expand Down
9 changes: 5 additions & 4 deletions apps/gtkmm/src/timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@ namespace {
authpp::Logger log("Timer");
}

Timer::Timer()
: m_timeout()
Timer::Timer(std::function<bool()>&& callback)
: m_callback(std::move(callback))
, m_timeout()
{
}

void Timer::start(long delay, const std::function<bool()>& 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()
Expand Down
22 changes: 11 additions & 11 deletions apps/gtkmm/src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,28 @@
#include <libauthpp/usb_manager.h>

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<std::mutex> 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;

Expand Down Expand Up @@ -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<std::mutex> 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);
}
}

Expand Down

0 comments on commit 6e70d0d

Please sign in to comment.