Checkbox list now receives a function pointer to call when the setting has changed. This allow to remove the dependency between CheckBoxList (UI component) with SettingController.

This commit is contained in:
Jean-François Milants 2022-10-02 21:05:15 +02:00 committed by JF
parent 6dd67eb5a2
commit cf8b422899
4 changed files with 37 additions and 51 deletions

View File

@ -52,6 +52,11 @@ namespace Pinetime {
Settings(Pinetime::Controllers::FS& fs); Settings(Pinetime::Controllers::FS& fs);
Settings(const Settings&) = delete;
Settings& operator=(const Settings&) = delete;
Settings(Settings&&) = delete;
Settings& operator=(Settings&&) = delete;
void Init(); void Init();
void SaveSettings(); void SaveSettings();
@ -135,14 +140,6 @@ namespace Pinetime {
appMenu = menu; appMenu = menu;
}; };
void SetWatchfacesMenu(uint8_t menu) {
watchFacesMenu = menu;
};
uint8_t GetWatchfacesMenu() const {
return watchFacesMenu;
};
uint8_t GetAppMenu() const { uint8_t GetAppMenu() const {
return appMenu; return appMenu;
}; };

View File

@ -1,5 +1,5 @@
#include "displayapp/screens/CheckboxList.h"
#include "displayapp/DisplayApp.h" #include "displayapp/DisplayApp.h"
#include "displayapp/screens/CheckboxList.h"
#include "displayapp/screens/Styles.h" #include "displayapp/screens/Styles.h"
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
@ -9,27 +9,21 @@ namespace {
CheckboxList* screen = static_cast<CheckboxList*>(obj->user_data); CheckboxList* screen = static_cast<CheckboxList*>(obj->user_data);
screen->UpdateSelected(obj, event); screen->UpdateSelected(obj, event);
} }
} }
CheckboxList::CheckboxList(const uint8_t screenID, CheckboxList::CheckboxList(const uint8_t screenID,
const uint8_t numScreens, const uint8_t numScreens,
DisplayApp* app, DisplayApp* app,
Controllers::Settings& settingsController,
const char* optionsTitle, const char* optionsTitle,
const char* optionsSymbol, const char* optionsSymbol,
void (Controllers::Settings::*SetOptionIndex)(uint8_t), uint32_t originalValue,
uint8_t (Controllers::Settings::*GetOptionIndex)() const, std::function<void(uint32_t)>OnValueChanged,
std::array<const char*, MaxItems> options) std::array<const char*, MaxItems> options)
: Screen(app), : Screen(app),
screenID {screenID}, screenID {screenID},
settingsController {settingsController}, OnValueChanged{std::move(OnValueChanged)},
SetOptionIndex {SetOptionIndex}, options {options},
GetOptionIndex {GetOptionIndex}, newValue{originalValue} {
options {options} {
settingsController.SetWatchfacesMenu(screenID);
// Set the background to Black // Set the background to Black
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK); lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
@ -39,7 +33,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
pageIndicatorBasePoints[1].x = LV_HOR_RES - 1; pageIndicatorBasePoints[1].x = LV_HOR_RES - 1;
pageIndicatorBasePoints[1].y = LV_VER_RES; pageIndicatorBasePoints[1].y = LV_VER_RES;
pageIndicatorBase = lv_line_create(lv_scr_act(), NULL); pageIndicatorBase = lv_line_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111)); lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111));
lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints.data(), 2); lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints.data(), 2);
@ -52,7 +46,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
pageIndicatorPoints[1].x = LV_HOR_RES - 1; pageIndicatorPoints[1].x = LV_HOR_RES - 1;
pageIndicatorPoints[1].y = indicatorPos + indicatorSize; pageIndicatorPoints[1].y = indicatorPos + indicatorSize;
pageIndicator = lv_line_create(lv_scr_act(), NULL); pageIndicator = lv_line_create(lv_scr_act(), nullptr);
lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3); lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY); lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
lv_line_set_points(pageIndicator, pageIndicatorPoints.data(), 2); lv_line_set_points(pageIndicator, pageIndicatorPoints.data(), 2);
@ -89,7 +83,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
lv_obj_set_event_cb(cbOption[i], event_handler); lv_obj_set_event_cb(cbOption[i], event_handler);
SetRadioButtonStyle(cbOption[i]); SetRadioButtonStyle(cbOption[i]);
if (static_cast<unsigned int>((settingsController.*GetOptionIndex)() - MaxItems * screenID) == i) { if (static_cast<unsigned int>(originalValue - MaxItems * screenID) == i) {
lv_checkbox_set_checked(cbOption[i], true); lv_checkbox_set_checked(cbOption[i], true);
} }
} }
@ -98,6 +92,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
CheckboxList::~CheckboxList() { CheckboxList::~CheckboxList() {
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
OnValueChanged(newValue);
} }
void CheckboxList::UpdateSelected(lv_obj_t* object, lv_event_t event) { void CheckboxList::UpdateSelected(lv_obj_t* object, lv_event_t event) {
@ -106,7 +101,7 @@ void CheckboxList::UpdateSelected(lv_obj_t* object, lv_event_t event) {
if (strcmp(options[i], "")) { if (strcmp(options[i], "")) {
if (object == cbOption[i]) { if (object == cbOption[i]) {
lv_checkbox_set_checked(cbOption[i], true); lv_checkbox_set_checked(cbOption[i], true);
(settingsController.*SetOptionIndex)(MaxItems * screenID + i); newValue = MaxItems * screenID + i;
} else { } else {
lv_checkbox_set_checked(cbOption[i], false); lv_checkbox_set_checked(cbOption[i], false);
} }

View File

@ -1,12 +1,12 @@
#pragma once #pragma once
#include <lvgl/lvgl.h>
#include <cstdint>
#include <memory>
#include <array>
#include "displayapp/screens/Screen.h"
#include "displayapp/Apps.h" #include "displayapp/Apps.h"
#include "components/settings/Settings.h" #include "displayapp/screens/Screen.h"
#include <array>
#include <cstdint>
#include <functional>
#include <lvgl/lvgl.h>
#include <memory>
namespace Pinetime { namespace Pinetime {
namespace Applications { namespace Applications {
@ -14,34 +14,27 @@ namespace Pinetime {
class CheckboxList : public Screen { class CheckboxList : public Screen {
public: public:
static constexpr size_t MaxItems = 4; static constexpr size_t MaxItems = 4;
CheckboxList(const uint8_t screenID, CheckboxList(const uint8_t screenID,
const uint8_t numScreens, const uint8_t numScreens,
DisplayApp* app, DisplayApp* app,
Controllers::Settings& settingsController,
const char* optionsTitle, const char* optionsTitle,
const char* optionsSymbol, const char* optionsSymbol,
void (Controllers::Settings::*SetOptionIndex)(uint8_t), uint32_t originalValue,
uint8_t (Controllers::Settings::*GetOptionIndex)() const, std::function<void(uint32_t)>OnValueChanged,
std::array<const char*, MaxItems> options); std::array<const char*, MaxItems> options);
~CheckboxList() override; ~CheckboxList() override;
void UpdateSelected(lv_obj_t* object, lv_event_t event); void UpdateSelected(lv_obj_t* object, lv_event_t event);
private: private:
const uint8_t screenID; const uint8_t screenID;
Controllers::Settings& settingsController; std::function<void(uint32_t)>OnValueChanged;
const char* optionsTitle;
const char* optionsSymbol;
void (Controllers::Settings::*SetOptionIndex)(uint8_t);
uint8_t (Controllers::Settings::*GetOptionIndex)() const;
std::array<const char*, MaxItems> options; std::array<const char*, MaxItems> options;
std::array<lv_obj_t*, MaxItems> cbOption; std::array<lv_obj_t*, MaxItems> cbOption;
std::array<lv_point_t, 2> pageIndicatorBasePoints; std::array<lv_point_t, 2> pageIndicatorBasePoints;
std::array<lv_point_t, 2> pageIndicatorPoints; std::array<lv_point_t, 2> pageIndicatorPoints;
lv_obj_t* pageIndicatorBase; lv_obj_t* pageIndicatorBase;
lv_obj_t* pageIndicator; lv_obj_t* pageIndicator;
uint32_t newValue;
}; };
} }
} }

View File

@ -3,8 +3,6 @@
#include "displayapp/DisplayApp.h" #include "displayapp/DisplayApp.h"
#include "displayapp/screens/CheckboxList.h" #include "displayapp/screens/CheckboxList.h"
#include "displayapp/screens/Screen.h" #include "displayapp/screens/Screen.h"
#include "displayapp/screens/Styles.h"
#include "displayapp/screens/Symbols.h"
#include "components/settings/Settings.h" #include "components/settings/Settings.h"
using namespace Pinetime::Applications::Screens; using namespace Pinetime::Applications::Screens;
@ -16,7 +14,7 @@ SettingWatchFace::SettingWatchFace(Pinetime::Applications::DisplayApp* app, Pine
: Screen(app), : Screen(app),
settingsController {settingsController}, settingsController {settingsController},
screens {app, screens {app,
settingsController.GetWatchfacesMenu(), 0,
{[this]() -> std::unique_ptr<Screen> { {[this]() -> std::unique_ptr<Screen> {
return CreateScreen1(); return CreateScreen1();
}, },
@ -28,7 +26,6 @@ SettingWatchFace::SettingWatchFace(Pinetime::Applications::DisplayApp* app, Pine
SettingWatchFace::~SettingWatchFace() { SettingWatchFace::~SettingWatchFace() {
lv_obj_clean(lv_scr_act()); lv_obj_clean(lv_scr_act());
settingsController.SaveSettings();
} }
bool SettingWatchFace::OnTouchEvent(Pinetime::Applications::TouchEvents event) { bool SettingWatchFace::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
@ -40,11 +37,13 @@ std::unique_ptr<Screen> SettingWatchFace::CreateScreen1() {
return std::make_unique<Screens::CheckboxList>(0, return std::make_unique<Screens::CheckboxList>(0,
2, 2,
app, app,
settingsController,
title, title,
symbol, symbol,
&Controllers::Settings::SetClockFace, settingsController.GetClockFace(),
&Controllers::Settings::GetClockFace, [&settings = settingsController](uint32_t clockFace) {
settings.SetClockFace(clockFace);
settings.SaveSettings();
},
watchfaces); watchfaces);
} }
@ -53,10 +52,12 @@ std::unique_ptr<Screen> SettingWatchFace::CreateScreen2() {
return std::make_unique<Screens::CheckboxList>(1, return std::make_unique<Screens::CheckboxList>(1,
2, 2,
app, app,
settingsController,
title, title,
symbol, symbol,
&Controllers::Settings::SetClockFace, settingsController.GetClockFace(),
&Controllers::Settings::GetClockFace, [&settings = settingsController](uint32_t clockFace) {
settings.SetClockFace(clockFace);
settings.SaveSettings();
},
watchfaces); watchfaces);
} }