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);
|
cbOption[i]->user_data = this;
|
||||||
|
lv_obj_set_event_cb(cbOption[i], event_handler);
|
||||||
}
|
}
|
||||||
optionsTotal++;
|
|
||||||
cbOption[optionsTotal] = lv_checkbox_create(container1, nullptr);
|
|
||||||
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;
|
// Find the index of the checkbox that triggered the event
|
||||||
if (event == LV_EVENT_VALUE_CHANGED && !ignoringEvents) {
|
for (size_t i = 0; i < options.size(); i++) {
|
||||||
ignoringEvents = true;
|
if (cbOption[i] == object) {
|
||||||
|
bool currentState = settingsController.isWakeUpModeOn(options[i].wakeUpMode);
|
||||||
// Find the index of the checkbox that triggered the event
|
settingsController.setWakeUpMode(options[i].wakeUpMode, !currentState);
|
||||||
int index = 0;
|
break;
|
||||||
for (; index < optionsTotal; ++index) {
|
|
||||||
if (cbOption[index] == object) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Toggle needed wakeup mode
|
// Update checkbox according to current wakeup modes.
|
||||||
auto mode = static_cast<WakeUpMode>(index);
|
// This is needed because we can have extra logic when setting or unsetting wakeup modes,
|
||||||
auto currentState = settingsController.isWakeUpModeOn(mode);
|
// for example, when setting SingleTap, DoubleTap is unset and vice versa.
|
||||||
settingsController.setWakeUpMode(mode, !currentState);
|
auto modes = settingsController.getWakeUpModes();
|
||||||
|
for (size_t i = 0; i < options.size(); ++i) {
|
||||||
// Update checkbox according to current wakeup modes.
|
lv_checkbox_set_checked(cbOption[i], modes[i]);
|
||||||
// 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.
|
|
||||||
auto modes = settingsController.getWakeUpModes();
|
|
||||||
for (int i = 0; i < optionsTotal; ++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