Optimize SettingWakeUp
This commit is contained in:
parent
9c5b1437ec
commit
99b5b49993
|
@ -4,19 +4,23 @@
|
||||||
#include "displayapp/screens/Screen.h"
|
#include "displayapp/screens/Screen.h"
|
||||||
#include "displayapp/screens/Symbols.h"
|
#include "displayapp/screens/Symbols.h"
|
||||||
#include "components/settings/Settings.h"
|
#include "components/settings/Settings.h"
|
||||||
|
#include "displayapp/screens/Styles.h"
|
||||||
|
|
||||||
using namespace Pinetime::Applications::Screens;
|
using namespace Pinetime::Applications::Screens;
|
||||||
|
|
||||||
|
constexpr std::array<SettingWakeUp::Option, 4> SettingWakeUp::options;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
void event_handler(lv_obj_t* obj, lv_event_t event) {
|
void event_handler(lv_obj_t* obj, lv_event_t event) {
|
||||||
auto* screen = static_cast<SettingWakeUp*>(obj->user_data);
|
auto* screen = static_cast<SettingWakeUp*>(obj->user_data);
|
||||||
screen->UpdateSelected(obj, event);
|
if (event == LV_EVENT_VALUE_CHANGED) {
|
||||||
|
screen->UpdateSelected(obj);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingWakeUp::SettingWakeUp(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController)
|
SettingWakeUp::SettingWakeUp(Pinetime::Applications::DisplayApp* app, Pinetime::Controllers::Settings& settingsController)
|
||||||
: Screen(app), settingsController {settingsController} {
|
: Screen(app), settingsController {settingsController} {
|
||||||
ignoringEvents = false;
|
|
||||||
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);
|
lv_obj_t* container1 = lv_cont_create(lv_scr_act(), nullptr);
|
||||||
|
|
||||||
lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
|
lv_obj_set_style_local_bg_opa(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, LV_OPA_TRANSP);
|
||||||
|
@ -40,39 +44,15 @@ SettingWakeUp::SettingWakeUp(Pinetime::Applications::DisplayApp* app, Pinetime::
|
||||||
lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER);
|
lv_label_set_align(icon, LV_LABEL_ALIGN_CENTER);
|
||||||
lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0);
|
lv_obj_align(icon, title, LV_ALIGN_OUT_LEFT_MID, -10, 0);
|
||||||
|
|
||||||
optionsTotal = 0;
|
for (unsigned int i = 0; i < options.size(); i++) {
|
||||||
cbOption[optionsTotal] = lv_checkbox_create(container1, nullptr);
|
cbOption[i] = lv_checkbox_create(container1, nullptr);
|
||||||
lv_checkbox_set_text_static(cbOption[optionsTotal], "Single Tap");
|
lv_checkbox_set_text(cbOption[i], options[i].name);
|
||||||
cbOption[optionsTotal]->user_data = this;
|
if (settingsController.isWakeUpModeOn(static_cast<Controllers::Settings::WakeUpMode>(i))) {
|
||||||
lv_obj_set_event_cb(cbOption[optionsTotal], event_handler);
|
lv_checkbox_set_checked(cbOption[i], true);
|
||||||
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::SingleTap)) {
|
|
||||||
lv_checkbox_set_checked(cbOption[optionsTotal], true);
|
|
||||||
}
|
}
|
||||||
optionsTotal++;
|
cbOption[i]->user_data = this;
|
||||||
cbOption[optionsTotal] = lv_checkbox_create(container1, nullptr);
|
lv_obj_set_event_cb(cbOption[i], event_handler);
|
||||||
lv_checkbox_set_text_static(cbOption[optionsTotal], "Double Tap");
|
|
||||||
cbOption[optionsTotal]->user_data = this;
|
|
||||||
lv_obj_set_event_cb(cbOption[optionsTotal], event_handler);
|
|
||||||
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::DoubleTap)) {
|
|
||||||
lv_checkbox_set_checked(cbOption[optionsTotal], true);
|
|
||||||
}
|
}
|
||||||
optionsTotal++;
|
|
||||||
cbOption[optionsTotal] = lv_checkbox_create(container1, nullptr);
|
|
||||||
lv_checkbox_set_text_static(cbOption[optionsTotal], "Raise Wrist");
|
|
||||||
cbOption[optionsTotal]->user_data = this;
|
|
||||||
lv_obj_set_event_cb(cbOption[optionsTotal], event_handler);
|
|
||||||
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::RaiseWrist)) {
|
|
||||||
lv_checkbox_set_checked(cbOption[optionsTotal], true);
|
|
||||||
}
|
|
||||||
optionsTotal++;
|
|
||||||
cbOption[optionsTotal] = lv_checkbox_create(container1, nullptr);
|
|
||||||
lv_checkbox_set_text_static(cbOption[optionsTotal], "Shake Wake");
|
|
||||||
cbOption[optionsTotal]->user_data = this;
|
|
||||||
lv_obj_set_event_cb(cbOption[optionsTotal], event_handler);
|
|
||||||
if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::Shake)) {
|
|
||||||
lv_checkbox_set_checked(cbOption[optionsTotal], true);
|
|
||||||
}
|
|
||||||
optionsTotal++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingWakeUp::~SettingWakeUp() {
|
SettingWakeUp::~SettingWakeUp() {
|
||||||
|
@ -80,32 +60,21 @@ SettingWakeUp::~SettingWakeUp() {
|
||||||
settingsController.SaveSettings();
|
settingsController.SaveSettings();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SettingWakeUp::UpdateSelected(lv_obj_t* object, lv_event_t event) {
|
void SettingWakeUp::UpdateSelected(lv_obj_t* object) {
|
||||||
using WakeUpMode = Pinetime::Controllers::Settings::WakeUpMode;
|
|
||||||
if (event == LV_EVENT_VALUE_CHANGED && !ignoringEvents) {
|
|
||||||
ignoringEvents = true;
|
|
||||||
|
|
||||||
// Find the index of the checkbox that triggered the event
|
// Find the index of the checkbox that triggered the event
|
||||||
int index = 0;
|
for (size_t i = 0; i < options.size(); i++) {
|
||||||
for (; index < optionsTotal; ++index) {
|
if (cbOption[i] == object) {
|
||||||
if (cbOption[index] == object) {
|
bool currentState = settingsController.isWakeUpModeOn(options[i].wakeUpMode);
|
||||||
|
settingsController.setWakeUpMode(options[i].wakeUpMode, !currentState);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Toggle needed wakeup mode
|
|
||||||
auto mode = static_cast<WakeUpMode>(index);
|
|
||||||
auto currentState = settingsController.isWakeUpModeOn(mode);
|
|
||||||
settingsController.setWakeUpMode(mode, !currentState);
|
|
||||||
|
|
||||||
// Update checkbox according to current wakeup modes.
|
// Update checkbox according to current wakeup modes.
|
||||||
// This is needed because we can have extra logic when setting or unsetting wakeup modes,
|
// This is needed because we can have extra logic when setting or unsetting wakeup modes,
|
||||||
// for example, when setting SingleTap, DoubleTap is unset and vice versa.
|
// for example, when setting SingleTap, DoubleTap is unset and vice versa.
|
||||||
auto modes = settingsController.getWakeUpModes();
|
auto modes = settingsController.getWakeUpModes();
|
||||||
for (int i = 0; i < optionsTotal; ++i) {
|
for (size_t i = 0; i < options.size(); ++i) {
|
||||||
lv_checkbox_set_checked(cbOption[i], modes[i]);
|
lv_checkbox_set_checked(cbOption[i], modes[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
ignoringEvents = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <lvgl/lvgl.h>
|
#include <lvgl/lvgl.h>
|
||||||
#include "components/settings/Settings.h"
|
#include "components/settings/Settings.h"
|
||||||
|
@ -15,17 +16,22 @@ namespace Pinetime {
|
||||||
SettingWakeUp(DisplayApp* app, Pinetime::Controllers::Settings& settingsController);
|
SettingWakeUp(DisplayApp* app, Pinetime::Controllers::Settings& settingsController);
|
||||||
~SettingWakeUp() override;
|
~SettingWakeUp() override;
|
||||||
|
|
||||||
void UpdateSelected(lv_obj_t* object, lv_event_t event);
|
void UpdateSelected(lv_obj_t* object);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
struct Option {
|
||||||
|
Controllers::Settings::WakeUpMode wakeUpMode;
|
||||||
|
const char* name;
|
||||||
|
};
|
||||||
Controllers::Settings& settingsController;
|
Controllers::Settings& settingsController;
|
||||||
uint8_t optionsTotal;
|
static constexpr std::array<Option, 4> options = {{
|
||||||
lv_obj_t* cbOption[5];
|
{Controllers::Settings::WakeUpMode::SingleTap, "Single Tap"},
|
||||||
// When UpdateSelected is called, it uses lv_checkbox_set_checked,
|
{Controllers::Settings::WakeUpMode::DoubleTap, "Double Tap"},
|
||||||
// which can cause extra events to be fired,
|
{Controllers::Settings::WakeUpMode::RaiseWrist, "Raise Wrist"},
|
||||||
// which might trigger UpdateSelected again, causing a loop.
|
{Controllers::Settings::WakeUpMode::Shake, "Shake Wake"},
|
||||||
// This variable is used as a mutex to prevent that.
|
}};
|
||||||
bool ignoringEvents;
|
|
||||||
|
lv_obj_t* cbOption[options.size()];
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user