diff --git a/src/update/UpdateManager.cpp b/src/update/UpdateManager.cpp index a9485425b..e1e4c417c 100644 --- a/src/update/UpdateManager.cpp +++ b/src/update/UpdateManager.cpp @@ -22,26 +22,26 @@ #include #include +#include BoolSetting UpdatesCheckingSettings::DefaultUpdatesCheckingFlag{ L"/Update/DefaultUpdatesChecking", true }; static const char* prefsUpdateScheduledTime = "/Update/UpdateScheduledTime"; + +using Clock = std::chrono::system_clock; +using TimePoint = Clock::time_point; +using Duration = TimePoint::duration; + +constexpr Duration updatesCheckInterval = std::chrono::hours(12); + enum { ID_TIMER = wxID_HIGHEST + 1 }; BEGIN_EVENT_TABLE(UpdateManager, wxEvtHandler) EVT_TIMER(ID_TIMER, UpdateManager::OnTimer) END_EVENT_TABLE() -UpdateManager::UpdateManager() - : mUpdateCheckingInterval( - std::chrono::milliseconds(std::chrono::hours(12)).count()) -{} - -UpdateManager::~UpdateManager() -{} - UpdateManager& UpdateManager::GetInstance() { static UpdateManager updateManager; @@ -123,29 +123,41 @@ void UpdateManager::OnTimer(wxTimerEvent& WXUNUSED(event)) if (updatesCheckingEnabled && IsTimeForUpdatesChecking()) GetUpdates(); - mTimer.StartOnce(mUpdateCheckingInterval); + mTimer.StartOnce(std::chrono::duration_cast( + updatesCheckInterval) + .count()); } bool UpdateManager::IsTimeForUpdatesChecking() { - long long nextUpdatesCheckingTime = std::stoll( - gPrefs->Read(prefsUpdateScheduledTime, "0").ToStdString()); + const TimePoint nextUpdatesCheckingTime(std::chrono::milliseconds( + stoll(gPrefs->Read(prefsUpdateScheduledTime, "0").ToStdString()))); - // Get current time in milliseconds - auto now_ms = std::chrono::time_point_cast( - std::chrono::system_clock::now()); - - auto currentTimeInMillisec = std::chrono::duration_cast( - now_ms.time_since_epoch()).count(); + // Get current time + const TimePoint currentTime = Clock::now(); // If next update time 0 or less then current time -> show update dialog, // else this condition allow us to avoid from duplicating update notifications. - if (nextUpdatesCheckingTime < currentTimeInMillisec) + if (nextUpdatesCheckingTime < currentTime) { - nextUpdatesCheckingTime = currentTimeInMillisec + mUpdateCheckingInterval; - gPrefs->Write(prefsUpdateScheduledTime, - wxString(std::to_string(nextUpdatesCheckingTime))); + // Round down the nextUpdatesChecking time to a day. + // This is required to ensure, that update is + // checked daily + using DayDuration = + std::chrono::duration>; + + const auto postponeUpdateUntil = + std::chrono::time_point_cast( + currentTime) + DayDuration(1); + + const std::chrono::milliseconds postponeUpdateUntilMS( + postponeUpdateUntil.time_since_epoch()); + + gPrefs->Write( + prefsUpdateScheduledTime, + wxString(std::to_string(postponeUpdateUntilMS.count()))); + gPrefs->Flush(); return true; diff --git a/src/update/UpdateManager.h b/src/update/UpdateManager.h index b88767b20..f938908d4 100644 --- a/src/update/UpdateManager.h +++ b/src/update/UpdateManager.h @@ -31,8 +31,7 @@ namespace UpdatesCheckingSettings { class UpdateManager final : public wxEvtHandler { public: - UpdateManager(); - ~UpdateManager(); + UpdateManager() = default; static UpdateManager& GetInstance(); static void Start(); @@ -46,7 +45,6 @@ private: VersionPatch mVersionPatch; wxTimer mTimer; - const int mUpdateCheckingInterval; void OnTimer(wxTimerEvent& event);