From 685558d2c02b9873bcf5d7b0774b320f165b6245 Mon Sep 17 00:00:00 2001 From: Mathias Tausen Date: Sun, 11 Feb 2024 21:25:13 +0100 Subject: [PATCH] Timer: Remember last timer setting Remember last duration used in Timer app. Reset to zero with long press on start button. --- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/Timer.cpp | 27 +++++++++++++++++++++------ src/displayapp/screens/Timer.h | 2 ++ 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index e5329b2d9d..a355d8d968 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -268,7 +268,7 @@ void DisplayApp::Refresh() { if (currentApp == Apps::Timer) { lv_disp_trig_activity(nullptr); auto* timer = static_cast(currentScreen.get()); - timer->Reset(); + timer->Stop(); } else { LoadNewScreen(Apps::Timer, DisplayApp::FullRefreshDirections::Up); } diff --git a/src/displayapp/screens/Timer.cpp b/src/displayapp/screens/Timer.cpp index a1ede6bed6..b58e498383 100644 --- a/src/displayapp/screens/Timer.cpp +++ b/src/displayapp/screens/Timer.cpp @@ -6,6 +6,8 @@ using namespace Pinetime::Applications::Screens; +static std::chrono::seconds lastTimer; + static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { auto* screen = static_cast(obj->user_data); if (event == LV_EVENT_PRESSED) { @@ -27,6 +29,7 @@ Timer::Timer(Controllers::Timer& timerController) : timer {timerController} { minuteCounter.Create(); secondCounter.Create(); + SetCounters(lastTimer); lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0); lv_obj_align(secondCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0); @@ -105,8 +108,7 @@ void Timer::UpdateMask() { void Timer::Refresh() { if (timer.IsRunning()) { auto secondsRemaining = std::chrono::duration_cast(timer.GetTimeRemaining()); - minuteCounter.SetValue(secondsRemaining.count() / 60); - secondCounter.SetValue(secondsRemaining.count() % 60); + SetCounters(secondsRemaining); } else if (buttonPressing && xTaskGetTickCount() > pressTime + pdMS_TO_TICKS(150)) { lv_label_set_text_static(txtPlayPause, "Reset"); maskPosition += 15; @@ -131,23 +133,36 @@ void Timer::SetTimerStopped() { lv_label_set_text_static(txtPlayPause, "Start"); } +void Timer::SetCounters(std::chrono::seconds& duration) { + minuteCounter.SetValue(duration.count() / 60); + secondCounter.SetValue(duration.count() % 60); +} + void Timer::ToggleRunning() { if (timer.IsRunning()) { auto secondsRemaining = std::chrono::duration_cast(timer.GetTimeRemaining()); - minuteCounter.SetValue(secondsRemaining.count() / 60); - secondCounter.SetValue(secondsRemaining.count() % 60); + if (secondsRemaining == std::chrono::seconds::zero()) { + secondsRemaining = lastTimer; + } + SetCounters(secondsRemaining); timer.StopTimer(); SetTimerStopped(); + lastTimer = secondsRemaining; } else if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) { auto timerDuration = std::chrono::minutes(minuteCounter.GetValue()) + std::chrono::seconds(secondCounter.GetValue()); timer.StartTimer(timerDuration); Refresh(); SetTimerRunning(); + lastTimer = timerDuration; } } void Timer::Reset() { - minuteCounter.SetValue(0); - secondCounter.SetValue(0); + lastTimer = std::chrono::seconds::zero(); + Stop(); +} + +void Timer::Stop() { + SetCounters(lastTimer); SetTimerStopped(); } diff --git a/src/displayapp/screens/Timer.h b/src/displayapp/screens/Timer.h index 409cae1cd4..ef38b76b6c 100644 --- a/src/displayapp/screens/Timer.h +++ b/src/displayapp/screens/Timer.h @@ -17,6 +17,7 @@ namespace Pinetime::Applications { Timer(Controllers::Timer& timerController); ~Timer() override; void Refresh() override; + void Stop(); void Reset(); void ToggleRunning(); void ButtonPressed(); @@ -26,6 +27,7 @@ namespace Pinetime::Applications { void SetTimerRunning(); void SetTimerStopped(); void UpdateMask(); + void SetCounters(std::chrono::seconds& duration); Pinetime::Controllers::Timer& timer; lv_obj_t* btnPlayPause;