66 lines
2.0 KiB
C++
66 lines
2.0 KiB
C++
#include "Timer.h"
|
|
|
|
|
|
float calcElapsedSeconds(std::chrono::system_clock::time_point startTime)
|
|
{
|
|
auto elapsedTime = std::chrono::system_clock::now() - startTime;
|
|
return std::chrono::duration_cast<std::chrono::seconds>(elapsedTime).count();
|
|
}
|
|
|
|
void updateTimer(ApplicationData& appData)
|
|
{
|
|
while (true)
|
|
{
|
|
appData.timerMutex.lock();
|
|
TimerData& timerData = appData.timerData;
|
|
|
|
if (timerData.isTimerActive)
|
|
{
|
|
// Main timer
|
|
float elapsedSeconds = calcElapsedSeconds(timerData.timerStartTimestamp);
|
|
float timerPercentComplete = elapsedSeconds / appData.settings.timerDuration;
|
|
|
|
if (timerPercentComplete >= 1.f)
|
|
{
|
|
if (!timerData.timerHasNotified)
|
|
{
|
|
// Alert
|
|
timerData.timerHasNotified = true;
|
|
|
|
std::wstring titleText = std::format(L"{} minutes over!", appData.settings.timerDuration / 60.f);
|
|
|
|
std::wstring messageText;
|
|
if (appData.settings.timerRepeating)
|
|
{
|
|
messageText = std::format(L"Repeats every {} minutes.", appData.settings.timerRepeatDuration / 60.f);
|
|
timerData.lastNotifySeconds = elapsedSeconds;
|
|
}
|
|
else
|
|
{
|
|
timerData.isTimerActive = false;
|
|
messageText = L"Timer does not repeat.";
|
|
}
|
|
showToastNotification(&timerData.toastHandler, titleText.c_str(), messageText.c_str(), appData.settings.timerRepeating ? appData.settings.timerRepeatDuration * 1000 : 0);
|
|
}
|
|
else
|
|
{
|
|
// Loop timer
|
|
if (elapsedSeconds - timerData.lastNotifySeconds >= appData.settings.timerRepeatDuration)
|
|
{
|
|
timerData.lastNotifySeconds = elapsedSeconds;
|
|
|
|
float elapsedSecondsSinceEnd = elapsedSeconds - appData.settings.timerDuration;
|
|
std::wstring titleText = std::format(L"Timer ended {} minutes ago", elapsedSecondsSinceEnd / 60.f);
|
|
std::wstring messageText = std::format(L"Repeats every {} minutes.", appData.settings.timerRepeatDuration / 60.f);
|
|
|
|
showToastNotification(&timerData.toastHandler, titleText.c_str(), messageText.c_str());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
appData.timerMutex.unlock();
|
|
Sleep(1000);
|
|
}
|
|
}
|