1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-01 08:29:27 +02:00

Postpone updates for a GMT calendar day

This commit is contained in:
Dmitry Vedenko 2021-06-25 20:18:42 +03:00 committed by Dmitry Vedenko
parent db527c55cf
commit 8fa37c729e
2 changed files with 34 additions and 24 deletions

View File

@ -22,26 +22,26 @@
#include <wx/frame.h> #include <wx/frame.h>
#include <mutex> #include <mutex>
#include <cstdint>
BoolSetting UpdatesCheckingSettings::DefaultUpdatesCheckingFlag{ BoolSetting UpdatesCheckingSettings::DefaultUpdatesCheckingFlag{
L"/Update/DefaultUpdatesChecking", true }; L"/Update/DefaultUpdatesChecking", true };
static const char* prefsUpdateScheduledTime = "/Update/UpdateScheduledTime"; 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 }; enum { ID_TIMER = wxID_HIGHEST + 1 };
BEGIN_EVENT_TABLE(UpdateManager, wxEvtHandler) BEGIN_EVENT_TABLE(UpdateManager, wxEvtHandler)
EVT_TIMER(ID_TIMER, UpdateManager::OnTimer) EVT_TIMER(ID_TIMER, UpdateManager::OnTimer)
END_EVENT_TABLE() END_EVENT_TABLE()
UpdateManager::UpdateManager()
: mUpdateCheckingInterval(
std::chrono::milliseconds(std::chrono::hours(12)).count())
{}
UpdateManager::~UpdateManager()
{}
UpdateManager& UpdateManager::GetInstance() UpdateManager& UpdateManager::GetInstance()
{ {
static UpdateManager updateManager; static UpdateManager updateManager;
@ -123,29 +123,41 @@ void UpdateManager::OnTimer(wxTimerEvent& WXUNUSED(event))
if (updatesCheckingEnabled && IsTimeForUpdatesChecking()) if (updatesCheckingEnabled && IsTimeForUpdatesChecking())
GetUpdates(); GetUpdates();
mTimer.StartOnce(mUpdateCheckingInterval); mTimer.StartOnce(std::chrono::duration_cast<std::chrono::milliseconds>(
updatesCheckInterval)
.count());
} }
bool UpdateManager::IsTimeForUpdatesChecking() bool UpdateManager::IsTimeForUpdatesChecking()
{ {
long long nextUpdatesCheckingTime = std::stoll( const TimePoint nextUpdatesCheckingTime(std::chrono::milliseconds(
gPrefs->Read(prefsUpdateScheduledTime, "0").ToStdString()); stoll(gPrefs->Read(prefsUpdateScheduledTime, "0").ToStdString())));
// Get current time in milliseconds // Get current time
auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>( const TimePoint currentTime = Clock::now();
std::chrono::system_clock::now());
auto currentTimeInMillisec = std::chrono::duration_cast<std::chrono::milliseconds>(
now_ms.time_since_epoch()).count();
// If next update time 0 or less then current time -> show update dialog, // If next update time 0 or less then current time -> show update dialog,
// else this condition allow us to avoid from duplicating update notifications. // else this condition allow us to avoid from duplicating update notifications.
if (nextUpdatesCheckingTime < currentTimeInMillisec) if (nextUpdatesCheckingTime < currentTime)
{ {
nextUpdatesCheckingTime = currentTimeInMillisec + mUpdateCheckingInterval;
gPrefs->Write(prefsUpdateScheduledTime, // Round down the nextUpdatesChecking time to a day.
wxString(std::to_string(nextUpdatesCheckingTime))); // This is required to ensure, that update is
// checked daily
using DayDuration =
std::chrono::duration<int32_t, std::ratio<60 * 60 * 24>>;
const auto postponeUpdateUntil =
std::chrono::time_point_cast<DayDuration>(
currentTime) + DayDuration(1);
const std::chrono::milliseconds postponeUpdateUntilMS(
postponeUpdateUntil.time_since_epoch());
gPrefs->Write(
prefsUpdateScheduledTime,
wxString(std::to_string(postponeUpdateUntilMS.count())));
gPrefs->Flush(); gPrefs->Flush();
return true; return true;

View File

@ -31,8 +31,7 @@ namespace UpdatesCheckingSettings {
class UpdateManager final : public wxEvtHandler class UpdateManager final : public wxEvtHandler
{ {
public: public:
UpdateManager(); UpdateManager() = default;
~UpdateManager();
static UpdateManager& GetInstance(); static UpdateManager& GetInstance();
static void Start(); static void Start();
@ -46,7 +45,6 @@ private:
VersionPatch mVersionPatch; VersionPatch mVersionPatch;
wxTimer mTimer; wxTimer mTimer;
const int mUpdateCheckingInterval;
void OnTimer(wxTimerEvent& event); void OnTimer(wxTimerEvent& event);