From 97048121b05abb2c51c09a4340fe0aa223f46182 Mon Sep 17 00:00:00 2001 From: Riku Isokoski Date: Fri, 22 Jul 2022 08:33:15 +0300 Subject: [PATCH] Use Counter widget in Alarm --- src/displayapp/DisplayApp.cpp | 2 +- src/displayapp/screens/Alarm.cpp | 162 ++++++++++--------------------- src/displayapp/screens/Alarm.h | 14 +-- 3 files changed, 61 insertions(+), 117 deletions(-) diff --git a/src/displayapp/DisplayApp.cpp b/src/displayapp/DisplayApp.cpp index d5cc9810..29684466 100644 --- a/src/displayapp/DisplayApp.cpp +++ b/src/displayapp/DisplayApp.cpp @@ -368,7 +368,7 @@ void DisplayApp::LoadApp(Apps app, DisplayApp::FullRefreshDirections direction) currentScreen = std::make_unique(this, timerController); break; case Apps::Alarm: - currentScreen = std::make_unique(this, alarmController, settingsController, *systemTask); + currentScreen = std::make_unique(this, alarmController, settingsController.GetClockType(), *systemTask); break; // Settings diff --git a/src/displayapp/screens/Alarm.cpp b/src/displayapp/screens/Alarm.cpp index c1ee6469..d508dd39 100644 --- a/src/displayapp/screens/Alarm.cpp +++ b/src/displayapp/screens/Alarm.cpp @@ -22,6 +22,13 @@ using namespace Pinetime::Applications::Screens; using Pinetime::Controllers::AlarmController; +namespace { + void ValueChangedHandler(void* userData) { + auto* screen = static_cast(userData); + screen->OnValueChanged(); + } +} + static void btnEventHandler(lv_obj_t* obj, lv_event_t event) { auto* screen = static_cast(obj->user_data); screen->OnButtonEvent(obj, event); @@ -34,59 +41,34 @@ static void StopAlarmTaskCallback(lv_task_t* task) { Alarm::Alarm(DisplayApp* app, Controllers::AlarmController& alarmController, - Pinetime::Controllers::Settings& settingsController, + Controllers::Settings::ClockType clockType, System::SystemTask& systemTask) - : Screen(app), alarmController {alarmController}, settingsController {settingsController}, systemTask {systemTask} { + : Screen(app), alarmController {alarmController}, clockType {clockType}, systemTask {systemTask} { - time = lv_label_create(lv_scr_act(), nullptr); - lv_obj_set_style_local_text_font(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); - lv_obj_set_style_local_text_color(time, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); + hourCounter.Create(); + lv_obj_align(hourCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_LEFT, 0, 0); + if (clockType == Controllers::Settings::ClockType::H12) { + hourCounter.EnableTwelveHourMode(); + } + hourCounter.SetValue(alarmController.Hours()); + hourCounter.SetValueChangedEventCallback(this, ValueChangedHandler); - alarmHours = alarmController.Hours(); - alarmMinutes = alarmController.Minutes(); - lv_label_set_text_fmt(time, "%02hhu:%02hhu", alarmHours, alarmMinutes); + minuteCounter.Create(); + lv_obj_align(minuteCounter.GetObject(), nullptr, LV_ALIGN_IN_TOP_RIGHT, 0, 0); + minuteCounter.SetValue(alarmController.Minutes()); + minuteCounter.SetValueChangedEventCallback(this, ValueChangedHandler); - lv_obj_align(time, lv_scr_act(), LV_ALIGN_CENTER, 0, -25); + lv_obj_t* colonLabel = lv_label_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_text_font(colonLabel, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_76); + lv_label_set_text_static(colonLabel, ":"); + lv_obj_align(colonLabel, lv_scr_act(), LV_ALIGN_CENTER, 0, -29); lblampm = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_font(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_bold_20); - lv_obj_set_style_local_text_color(lblampm, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0xb0, 0xb0, 0xb0)); lv_label_set_text_static(lblampm, " "); lv_label_set_align(lblampm, LV_LABEL_ALIGN_CENTER); lv_obj_align(lblampm, lv_scr_act(), LV_ALIGN_CENTER, 0, 30); - btnHoursUp = lv_btn_create(lv_scr_act(), nullptr); - btnHoursUp->user_data = this; - lv_obj_set_event_cb(btnHoursUp, btnEventHandler); - lv_obj_set_size(btnHoursUp, 60, 40); - lv_obj_align(btnHoursUp, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, -85); - txtHrUp = lv_label_create(btnHoursUp, nullptr); - lv_label_set_text_static(txtHrUp, "+"); - - btnHoursDown = lv_btn_create(lv_scr_act(), nullptr); - btnHoursDown->user_data = this; - lv_obj_set_event_cb(btnHoursDown, btnEventHandler); - lv_obj_set_size(btnHoursDown, 60, 40); - lv_obj_align(btnHoursDown, lv_scr_act(), LV_ALIGN_IN_LEFT_MID, 20, 35); - txtHrDown = lv_label_create(btnHoursDown, nullptr); - lv_label_set_text_static(txtHrDown, "-"); - - btnMinutesUp = lv_btn_create(lv_scr_act(), nullptr); - btnMinutesUp->user_data = this; - lv_obj_set_event_cb(btnMinutesUp, btnEventHandler); - lv_obj_set_size(btnMinutesUp, 60, 40); - lv_obj_align(btnMinutesUp, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, -85); - txtMinUp = lv_label_create(btnMinutesUp, nullptr); - lv_label_set_text_static(txtMinUp, "+"); - - btnMinutesDown = lv_btn_create(lv_scr_act(), nullptr); - btnMinutesDown->user_data = this; - lv_obj_set_event_cb(btnMinutesDown, btnEventHandler); - lv_obj_set_size(btnMinutesDown, 60, 40); - lv_obj_align(btnMinutesDown, lv_scr_act(), LV_ALIGN_IN_RIGHT_MID, -20, 35); - txtMinDown = lv_label_create(btnMinutesDown, nullptr); - lv_label_set_text_static(txtMinDown, "-"); - btnStop = lv_btn_create(lv_scr_act(), nullptr); btnStop->user_data = this; lv_obj_set_event_cb(btnStop, btnEventHandler); @@ -104,14 +86,15 @@ Alarm::Alarm(DisplayApp* app, lv_obj_align(btnRecur, lv_scr_act(), LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0); txtRecur = lv_label_create(btnRecur, nullptr); SetRecurButtonState(); + lv_obj_set_style_local_bg_color(btnRecur, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38)); + // Invisible button above the time btnInfo = lv_btn_create(lv_scr_act(), nullptr); btnInfo->user_data = this; lv_obj_set_event_cb(btnInfo, btnEventHandler); - lv_obj_set_size(btnInfo, 50, 40); - lv_obj_align(btnInfo, lv_scr_act(), LV_ALIGN_CENTER, 0, -85); - txtInfo = lv_label_create(btnInfo, nullptr); - lv_label_set_text_static(txtInfo, "i"); + lv_obj_set_size(btnInfo, LV_HOR_RES, 80); + lv_obj_align(btnInfo, lv_scr_act(), LV_ALIGN_CENTER, 0, -29); + lv_obj_set_style_local_bg_opa(btnInfo, LV_BTN_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP); enableSwitch = lv_switch_create(lv_scr_act(), nullptr); enableSwitch->user_data = this; @@ -119,6 +102,7 @@ Alarm::Alarm(DisplayApp* app, lv_obj_set_size(enableSwitch, 100, 50); // Align to the center of 115px from edge lv_obj_align(enableSwitch, lv_scr_act(), LV_ALIGN_IN_BOTTOM_LEFT, 7, 0); + lv_obj_set_style_local_bg_color(enableSwitch, LV_SWITCH_PART_BG, LV_STATE_DEFAULT, LV_COLOR_MAKE(0x38, 0x38, 0x38)); UpdateAlarmTime(); @@ -136,8 +120,14 @@ Alarm::~Alarm() { lv_obj_clean(lv_scr_act()); } +void Alarm::DisableAlarm() { + if (alarmController.State() == AlarmController::AlarmState::Set) { + alarmController.DisableAlarm(); + lv_switch_off(enableSwitch, LV_ANIM_ON); + } +} + void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { - using Pinetime::Controllers::AlarmController; if (event == LV_EVENT_CLICKED) { if (obj == btnStop) { StopAlerting(); @@ -159,49 +149,8 @@ void Alarm::OnButtonEvent(lv_obj_t* obj, lv_event_t event) { } return; } - // If any other button was pressed, disable the alarm - // this is to make it clear that the alarm won't be set until it is turned back on - if (alarmController.State() == AlarmController::AlarmState::Set) { - alarmController.DisableAlarm(); - lv_switch_off(enableSwitch, LV_ANIM_ON); - } - if (obj == btnMinutesUp) { - if (alarmMinutes >= 59) { - alarmMinutes = 0; - } else { - alarmMinutes++; - } - UpdateAlarmTime(); - return; - } - if (obj == btnMinutesDown) { - if (alarmMinutes == 0) { - alarmMinutes = 59; - } else { - alarmMinutes--; - } - UpdateAlarmTime(); - return; - } - if (obj == btnHoursUp) { - if (alarmHours >= 23) { - alarmHours = 0; - } else { - alarmHours++; - } - UpdateAlarmTime(); - return; - } - if (obj == btnHoursDown) { - if (alarmHours == 0) { - alarmHours = 23; - } else { - alarmHours--; - } - UpdateAlarmTime(); - return; - } if (obj == btnRecur) { + DisableAlarm(); ToggleRecurrence(); } } @@ -224,30 +173,20 @@ bool Alarm::OnTouchEvent(Pinetime::Applications::TouchEvents event) { return alarmController.State() == AlarmController::AlarmState::Alerting && event == TouchEvents::SwipeDown; } +void Alarm::OnValueChanged() { + DisableAlarm(); + UpdateAlarmTime(); +} + void Alarm::UpdateAlarmTime() { - if (settingsController.GetClockType() == Controllers::Settings::ClockType::H12) { - switch (alarmHours) { - case 0: - lv_label_set_text_static(lblampm, "AM"); - lv_label_set_text_fmt(time, "%02d:%02d", 12, alarmMinutes); - break; - case 1 ... 11: - lv_label_set_text_static(lblampm, "AM"); - lv_label_set_text_fmt(time, "%02d:%02d", alarmHours, alarmMinutes); - break; - case 12: - lv_label_set_text_static(lblampm, "PM"); - lv_label_set_text_fmt(time, "%02d:%02d", 12, alarmMinutes); - break; - case 13 ... 23: - lv_label_set_text_static(lblampm, "PM"); - lv_label_set_text_fmt(time, "%02d:%02d", alarmHours - 12, alarmMinutes); - break; + if (clockType == Controllers::Settings::ClockType::H12) { + if (hourCounter.GetValue() >= 12) { + lv_label_set_text_static(lblampm, "PM"); + } else { + lv_label_set_text_static(lblampm, "AM"); } - } else { - lv_label_set_text_fmt(time, "%02d:%02d", alarmHours, alarmMinutes); } - alarmController.SetAlarmTime(alarmHours, alarmMinutes); + alarmController.SetAlarmTime(hourCounter.GetValue(), minuteCounter.GetValue()); } void Alarm::SetAlerting() { @@ -283,6 +222,9 @@ void Alarm::SetSwitchState(lv_anim_enable_t anim) { } void Alarm::ShowInfo() { + if (btnMessage != nullptr) { + return; + } btnMessage = lv_btn_create(lv_scr_act(), nullptr); btnMessage->user_data = this; lv_obj_set_event_cb(btnMessage, btnEventHandler); diff --git a/src/displayapp/screens/Alarm.h b/src/displayapp/screens/Alarm.h index 80e446f1..a64bbe81 100644 --- a/src/displayapp/screens/Alarm.h +++ b/src/displayapp/screens/Alarm.h @@ -21,6 +21,7 @@ #include "systemtask/SystemTask.h" #include "displayapp/LittleVgl.h" #include "components/alarm/AlarmController.h" +#include "displayapp/widgets/Counter.h" namespace Pinetime { namespace Applications { @@ -29,29 +30,28 @@ namespace Pinetime { public: Alarm(DisplayApp* app, Controllers::AlarmController& alarmController, - Pinetime::Controllers::Settings& settingsController, + Controllers::Settings::ClockType clockType, System::SystemTask& systemTask); ~Alarm() override; void SetAlerting(); void OnButtonEvent(lv_obj_t* obj, lv_event_t event); bool OnButtonPushed() override; bool OnTouchEvent(TouchEvents event) override; + void OnValueChanged(); void StopAlerting(); private: - uint8_t alarmHours; - uint8_t alarmMinutes; Controllers::AlarmController& alarmController; - Controllers::Settings& settingsController; + const Controllers::Settings::ClockType clockType; System::SystemTask& systemTask; - lv_obj_t *time, *lblampm, *btnStop, *txtStop, *btnMinutesUp, *btnMinutesDown, *btnHoursUp, *btnHoursDown, *txtMinUp, *txtMinDown, - *txtHrUp, *txtHrDown, *btnRecur, *txtRecur, *btnInfo, *txtInfo, *enableSwitch; + lv_obj_t *lblampm, *btnStop, *txtStop, *btnRecur, *txtRecur, *btnInfo, *enableSwitch; lv_obj_t* txtMessage = nullptr; lv_obj_t* btnMessage = nullptr; lv_task_t* taskStopAlarm = nullptr; enum class EnableButtonState { On, Off, Alerting }; + void DisableAlarm(); void SetRecurButtonState(); void SetSwitchState(lv_anim_enable_t anim); void SetAlarm(); @@ -59,6 +59,8 @@ namespace Pinetime { void HideInfo(); void ToggleRecurrence(); void UpdateAlarmTime(); + Widgets::Counter hourCounter = Widgets::Counter(0, 23, jetbrains_mono_76); + Widgets::Counter minuteCounter = Widgets::Counter(0, 59, jetbrains_mono_76); }; }; };