Skip to content

Commit cf8b422

Browse files
committed
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.
1 parent 6dd67eb commit cf8b422

4 files changed

Lines changed: 36 additions & 50 deletions

File tree

src/components/settings/Settings.h

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ namespace Pinetime {
5252

5353
Settings(Pinetime::Controllers::FS& fs);
5454

55+
Settings(const Settings&) = delete;
56+
Settings& operator=(const Settings&) = delete;
57+
Settings(Settings&&) = delete;
58+
Settings& operator=(Settings&&) = delete;
59+
5560
void Init();
5661
void SaveSettings();
5762

@@ -135,14 +140,6 @@ namespace Pinetime {
135140
appMenu = menu;
136141
};
137142

138-
void SetWatchfacesMenu(uint8_t menu) {
139-
watchFacesMenu = menu;
140-
};
141-
142-
uint8_t GetWatchfacesMenu() const {
143-
return watchFacesMenu;
144-
};
145-
146143
uint8_t GetAppMenu() const {
147144
return appMenu;
148145
};

src/displayapp/screens/CheckboxList.cpp

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include "displayapp/screens/CheckboxList.h"
21
#include "displayapp/DisplayApp.h"
2+
#include "displayapp/screens/CheckboxList.h"
33
#include "displayapp/screens/Styles.h"
44

55
using namespace Pinetime::Applications::Screens;
@@ -9,27 +9,21 @@ namespace {
99
CheckboxList* screen = static_cast<CheckboxList*>(obj->user_data);
1010
screen->UpdateSelected(obj, event);
1111
}
12-
1312
}
1413

1514
CheckboxList::CheckboxList(const uint8_t screenID,
1615
const uint8_t numScreens,
1716
DisplayApp* app,
18-
Controllers::Settings& settingsController,
1917
const char* optionsTitle,
2018
const char* optionsSymbol,
21-
void (Controllers::Settings::*SetOptionIndex)(uint8_t),
22-
uint8_t (Controllers::Settings::*GetOptionIndex)() const,
19+
uint32_t originalValue,
20+
std::function<void(uint32_t)>OnValueChanged,
2321
std::array<const char*, MaxItems> options)
2422
: Screen(app),
2523
screenID {screenID},
26-
settingsController {settingsController},
27-
SetOptionIndex {SetOptionIndex},
28-
GetOptionIndex {GetOptionIndex},
29-
options {options} {
30-
31-
settingsController.SetWatchfacesMenu(screenID);
32-
24+
OnValueChanged{std::move(OnValueChanged)},
25+
options {options},
26+
newValue{originalValue} {
3327
// Set the background to Black
3428
lv_obj_set_style_local_bg_color(lv_scr_act(), LV_OBJ_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_BLACK);
3529

@@ -39,7 +33,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
3933
pageIndicatorBasePoints[1].x = LV_HOR_RES - 1;
4034
pageIndicatorBasePoints[1].y = LV_VER_RES;
4135

42-
pageIndicatorBase = lv_line_create(lv_scr_act(), NULL);
36+
pageIndicatorBase = lv_line_create(lv_scr_act(), nullptr);
4337
lv_obj_set_style_local_line_width(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
4438
lv_obj_set_style_local_line_color(pageIndicatorBase, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, lv_color_hex(0x111111));
4539
lv_line_set_points(pageIndicatorBase, pageIndicatorBasePoints.data(), 2);
@@ -52,7 +46,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
5246
pageIndicatorPoints[1].x = LV_HOR_RES - 1;
5347
pageIndicatorPoints[1].y = indicatorPos + indicatorSize;
5448

55-
pageIndicator = lv_line_create(lv_scr_act(), NULL);
49+
pageIndicator = lv_line_create(lv_scr_act(), nullptr);
5650
lv_obj_set_style_local_line_width(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, 3);
5751
lv_obj_set_style_local_line_color(pageIndicator, LV_LINE_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_GRAY);
5852
lv_line_set_points(pageIndicator, pageIndicatorPoints.data(), 2);
@@ -89,7 +83,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
8983
lv_obj_set_event_cb(cbOption[i], event_handler);
9084
SetRadioButtonStyle(cbOption[i]);
9185

92-
if (static_cast<unsigned int>((settingsController.*GetOptionIndex)() - MaxItems * screenID) == i) {
86+
if (static_cast<unsigned int>(originalValue - MaxItems * screenID) == i) {
9387
lv_checkbox_set_checked(cbOption[i], true);
9488
}
9589
}
@@ -98,6 +92,7 @@ CheckboxList::CheckboxList(const uint8_t screenID,
9892

9993
CheckboxList::~CheckboxList() {
10094
lv_obj_clean(lv_scr_act());
95+
OnValueChanged(newValue);
10196
}
10297

10398
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) {
106101
if (strcmp(options[i], "")) {
107102
if (object == cbOption[i]) {
108103
lv_checkbox_set_checked(cbOption[i], true);
109-
(settingsController.*SetOptionIndex)(MaxItems * screenID + i);
104+
newValue = MaxItems * screenID + i;
110105
} else {
111106
lv_checkbox_set_checked(cbOption[i], false);
112107
}

src/displayapp/screens/CheckboxList.h

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
11
#pragma once
22

3-
#include <lvgl/lvgl.h>
3+
#include "displayapp/Apps.h"
4+
#include "displayapp/screens/Screen.h"
5+
#include <array>
46
#include <cstdint>
7+
#include <functional>
8+
#include <lvgl/lvgl.h>
59
#include <memory>
6-
#include <array>
7-
#include "displayapp/screens/Screen.h"
8-
#include "displayapp/Apps.h"
9-
#include "components/settings/Settings.h"
1010

1111
namespace Pinetime {
1212
namespace Applications {
1313
namespace Screens {
1414
class CheckboxList : public Screen {
1515
public:
1616
static constexpr size_t MaxItems = 4;
17-
1817
CheckboxList(const uint8_t screenID,
1918
const uint8_t numScreens,
2019
DisplayApp* app,
21-
Controllers::Settings& settingsController,
2220
const char* optionsTitle,
2321
const char* optionsSymbol,
24-
void (Controllers::Settings::*SetOptionIndex)(uint8_t),
25-
uint8_t (Controllers::Settings::*GetOptionIndex)() const,
22+
uint32_t originalValue,
23+
std::function<void(uint32_t)>OnValueChanged,
2624
std::array<const char*, MaxItems> options);
27-
2825
~CheckboxList() override;
29-
3026
void UpdateSelected(lv_obj_t* object, lv_event_t event);
3127

3228
private:
3329
const uint8_t screenID;
34-
Controllers::Settings& settingsController;
35-
const char* optionsTitle;
36-
const char* optionsSymbol;
37-
void (Controllers::Settings::*SetOptionIndex)(uint8_t);
38-
uint8_t (Controllers::Settings::*GetOptionIndex)() const;
30+
std::function<void(uint32_t)>OnValueChanged;
3931
std::array<const char*, MaxItems> options;
4032
std::array<lv_obj_t*, MaxItems> cbOption;
4133
std::array<lv_point_t, 2> pageIndicatorBasePoints;
4234
std::array<lv_point_t, 2> pageIndicatorPoints;
4335
lv_obj_t* pageIndicatorBase;
4436
lv_obj_t* pageIndicator;
37+
uint32_t newValue;
4538
};
4639
}
4740
}

src/displayapp/screens/settings/SettingWatchFace.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "displayapp/DisplayApp.h"
44
#include "displayapp/screens/CheckboxList.h"
55
#include "displayapp/screens/Screen.h"
6-
#include "displayapp/screens/Styles.h"
7-
#include "displayapp/screens/Symbols.h"
86
#include "components/settings/Settings.h"
97

108
using namespace Pinetime::Applications::Screens;
@@ -16,7 +14,7 @@ SettingWatchFace::SettingWatchFace(Pinetime::Applications::DisplayApp* app, Pine
1614
: Screen(app),
1715
settingsController {settingsController},
1816
screens {app,
19-
settingsController.GetWatchfacesMenu(),
17+
0,
2018
{[this]() -> std::unique_ptr<Screen> {
2119
return CreateScreen1();
2220
},
@@ -28,7 +26,6 @@ SettingWatchFace::SettingWatchFace(Pinetime::Applications::DisplayApp* app, Pine
2826

2927
SettingWatchFace::~SettingWatchFace() {
3028
lv_obj_clean(lv_scr_act());
31-
settingsController.SaveSettings();
3229
}
3330

3431
bool SettingWatchFace::OnTouchEvent(Pinetime::Applications::TouchEvents event) {
@@ -40,11 +37,13 @@ std::unique_ptr<Screen> SettingWatchFace::CreateScreen1() {
4037
return std::make_unique<Screens::CheckboxList>(0,
4138
2,
4239
app,
43-
settingsController,
4440
title,
4541
symbol,
46-
&Controllers::Settings::SetClockFace,
47-
&Controllers::Settings::GetClockFace,
42+
settingsController.GetClockFace(),
43+
[&settings = settingsController](uint32_t clockFace) {
44+
settings.SetClockFace(clockFace);
45+
settings.SaveSettings();
46+
},
4847
watchfaces);
4948
}
5049

@@ -53,10 +52,12 @@ std::unique_ptr<Screen> SettingWatchFace::CreateScreen2() {
5352
return std::make_unique<Screens::CheckboxList>(1,
5453
2,
5554
app,
56-
settingsController,
5755
title,
5856
symbol,
59-
&Controllers::Settings::SetClockFace,
60-
&Controllers::Settings::GetClockFace,
57+
settingsController.GetClockFace(),
58+
[&settings = settingsController](uint32_t clockFace) {
59+
settings.SetClockFace(clockFace);
60+
settings.SaveSettings();
61+
},
6162
watchfaces);
6263
}

0 commit comments

Comments
 (0)