Skip to content

Commit 5a623e2

Browse files
committed
fixup! Timer: Remember last timer setting
Move last timer setting handling from Controllers::Timer to Screens::Timer. This way timer setting is saved as soon as it is changed instead of only when the timer stops.
1 parent ec17031 commit 5a623e2

File tree

4 files changed

+18
-19
lines changed

4 files changed

+18
-19
lines changed

src/components/timer/Timer.cpp

-10
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ using namespace Pinetime::Controllers;
44

55
Timer::Timer(void* const timerData, TimerCallbackFunction_t timerCallbackFunction) {
66
pausedAtTimer = std::chrono::seconds(0);
7-
lastTimer = std::chrono::seconds(0);
87
state = Stopped;
98
timer = xTimerCreate("Timer", 1, pdFALSE, timerData, timerCallbackFunction);
109
}
@@ -21,21 +20,12 @@ std::chrono::milliseconds Timer::GetTimeRemaining() {
2120
return std::chrono::milliseconds(0);
2221
}
2322

24-
std::chrono::milliseconds Timer::GetLast() {
25-
return lastTimer;
26-
}
27-
28-
void Timer::ResetLast() {
29-
lastTimer = std::chrono::seconds(0);
30-
}
31-
3223
Timer::TimerState Timer::GetState() {
3324
return state;
3425
}
3526

3627
void Timer::Start(const std::chrono::milliseconds duration) {
3728
TimerStart(duration);
38-
lastTimer = duration;
3929
state = Running;
4030
}
4131

src/components/timer/Timer.h

-4
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,13 @@ namespace Pinetime {
2121

2222
std::chrono::milliseconds GetTimeRemaining();
2323

24-
std::chrono::milliseconds GetLast();
25-
void ResetLast();
26-
2724
typedef enum TimerState { Running, Stopped, Paused } TimerState;
2825

2926
TimerState GetState();
3027

3128
private:
3229
TimerHandle_t timer;
3330

34-
std::chrono::milliseconds lastTimer;
3531
std::chrono::milliseconds pausedAtTimer;
3632

3733
TimerState state;

src/displayapp/screens/Timer.cpp

+17-5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
using namespace Pinetime::Applications::Screens;
88

9+
static std::chrono::milliseconds lastTimerSetting;
10+
911
static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
1012
auto* screen = static_cast<Timer*>(obj->user_data);
1113
if (event == LV_EVENT_PRESSED) {
@@ -17,6 +19,11 @@ static void btnEventHandler(lv_obj_t* obj, lv_event_t event) {
1719
}
1820
}
1921

22+
static void counterChangeHandler(void *timerScreen) {
23+
Timer* timer = (Timer*)timerScreen;
24+
lastTimerSetting = timer->GetCounters();
25+
}
26+
2027
Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {
2128

2229
lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr);
@@ -29,6 +36,8 @@ Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {
2936
secondCounter.Create();
3037
lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0);
3138
lv_obj_align(secondCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0);
39+
minuteCounter.SetValueChangedEventCallback(this, counterChangeHandler);
40+
secondCounter.SetValueChangedEventCallback(this, counterChangeHandler);
3241

3342
highlightObjectMask = lv_objmask_create(lv_scr_act(), nullptr);
3443
lv_obj_set_size(highlightObjectMask, 240, 50);
@@ -63,7 +72,7 @@ Timer::Timer(Controllers::Timer& timerController) : timer {timerController} {
6372

6473
switch (timer.GetState()) {
6574
case Controllers::Timer::TimerState::Stopped:
66-
SetCounters(timer.GetLast());
75+
SetCounters(lastTimerSetting);
6776
SetInterfaceStopped();
6877
break;
6978
case Controllers::Timer::TimerState::Running:
@@ -188,11 +197,14 @@ void Timer::SetCounters(const std::chrono::seconds& duration) {
188197
secondCounter.SetValue(duration.count() % 60);
189198
}
190199

200+
std::chrono::seconds Timer::GetCounters() {
201+
return std::chrono::minutes(minuteCounter.GetValue()) + std::chrono::seconds(secondCounter.GetValue());
202+
}
203+
191204
void Timer::ToggleRunning() {
192205
if (timer.GetState() == Controllers::Timer::TimerState::Stopped) {
193206
if (secondCounter.GetValue() + minuteCounter.GetValue() > 0) {
194-
auto timerDuration = std::chrono::minutes(minuteCounter.GetValue()) + std::chrono::seconds(secondCounter.GetValue());
195-
timer.Start(timerDuration);
207+
timer.Start(GetCounters());
196208
Refresh();
197209
SetInterfaceRunning();
198210
}
@@ -206,12 +218,12 @@ void Timer::ToggleRunning() {
206218
}
207219

208220
void Timer::Reset() {
209-
timer.ResetLast();
221+
lastTimerSetting = std::chrono::seconds(0);
210222
Stop();
211223
}
212224

213225
void Timer::Stop() {
214226
timer.Stop();
215-
SetCounters(timer.GetLast());
227+
SetCounters(lastTimerSetting);
216228
SetInterfaceStopped();
217229
}

src/displayapp/screens/Timer.h

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ namespace Pinetime::Applications {
2222
void ToggleRunning();
2323
void ButtonPressed();
2424
void MaskReset();
25+
std::chrono::seconds GetCounters();
2526

2627
private:
2728
void SetInterfaceRunning();

0 commit comments

Comments
 (0)