Skip to content

Commit 6064805

Browse files
Add SettingsActivity framework
...so apps can easily add settings screens with just a few lines of code!
1 parent 1e0f31f commit 6064805

File tree

3 files changed

+89
-2
lines changed

3 files changed

+89
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
- Improve robustness with custom exception that does not deinit() the TaskHandler
99
- Improve robustness by removing TaskHandler callback that throws an uncaught exception
1010
- Make "Power Off" button on desktop exit completely
11-
- Promote SettingActivity from app to framework: now all apps can use it to easily build a setting screen
11+
- Create new SettingsActivity and SettingActivity framework so apps can easily add settings screens with just a few lines of code
1212

1313
0.5.2
1414
=====

internal_filesystem/lib/mpos/ui/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .event import get_event_name, print_event
1616
from .util import shutdown, set_foreground_app, get_foreground_app
1717
from .setting_activity import SettingActivity
18+
from .settings_activity import SettingsActivity
1819

1920
__all__ = [
2021
"setContentView", "back_screen", "remove_and_stop_current_activity", "remove_and_stop_all_activities"
@@ -28,5 +29,6 @@
2829
"get_pointer_xy",
2930
"get_event_name", "print_event",
3031
"shutdown", "set_foreground_app", "get_foreground_app",
31-
"SettingActivity"
32+
"SettingActivity",
33+
"SettingsActivity"
3234
]
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import lvgl as lv
2+
3+
import mpos
4+
from mpos.apps import Activity, Intent
5+
from .setting_activity import SettingActivity
6+
7+
# Used to list and edit all settings:
8+
class SettingsActivity(Activity):
9+
10+
# Taken the Intent:
11+
prefs = None
12+
settings = None
13+
14+
def onCreate(self):
15+
self.prefs = self.getIntent().extras.get("prefs")
16+
self.settings = self.getIntent().extras.get("settings")
17+
18+
print("creating SettingsActivity ui...")
19+
screen = lv.obj()
20+
screen.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
21+
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
22+
screen.set_style_border_width(0, 0)
23+
self.setContentView(screen)
24+
25+
def onResume(self, screen):
26+
wallet_type = self.prefs.get_string("wallet_type") # might have changed in the settings
27+
28+
# Create settings entries
29+
screen.clean()
30+
# Get the group for focusable objects
31+
focusgroup = lv.group_get_default()
32+
if not focusgroup:
33+
print("WARNING: could not get default focusgroup")
34+
35+
for setting in self.settings:
36+
# Check if it should be shown:
37+
should_show_function = setting.get("should_show")
38+
if should_show_function:
39+
should_show = should_show_function(setting)
40+
if should_show is False:
41+
continue
42+
# Container for each setting
43+
setting_cont = lv.obj(screen)
44+
setting_cont.set_width(lv.pct(100))
45+
setting_cont.set_height(lv.SIZE_CONTENT)
46+
setting_cont.set_style_border_width(1, 0)
47+
#setting_cont.set_style_border_side(lv.BORDER_SIDE.BOTTOM, 0)
48+
setting_cont.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
49+
setting_cont.add_flag(lv.obj.FLAG.CLICKABLE)
50+
setting["cont"] = setting_cont # Store container reference for visibility control
51+
52+
# Title label (bold, larger)
53+
title = lv.label(setting_cont)
54+
title.set_text(setting["title"])
55+
title.set_style_text_font(lv.font_montserrat_16, 0)
56+
title.set_pos(0, 0)
57+
58+
# Value label (smaller, below title)
59+
value = lv.label(setting_cont)
60+
value.set_text(self.prefs.get_string(setting["key"], "(not set)"))
61+
value.set_style_text_font(lv.font_montserrat_12, 0)
62+
value.set_style_text_color(lv.color_hex(0x666666), 0)
63+
value.set_pos(0, 20)
64+
setting["value_label"] = value # Store reference for updating
65+
setting_cont.add_event_cb(lambda e, s=setting: self.startSettingActivity(s), lv.EVENT.CLICKED, None)
66+
setting_cont.add_event_cb(lambda e, container=setting_cont: self.focus_container(container),lv.EVENT.FOCUSED,None)
67+
setting_cont.add_event_cb(lambda e, container=setting_cont: self.defocus_container(container),lv.EVENT.DEFOCUSED,None)
68+
if focusgroup:
69+
focusgroup.add_obj(setting_cont)
70+
71+
def focus_container(self, container):
72+
print(f"container {container} focused, setting border...")
73+
container.set_style_border_color(lv.theme_get_color_primary(None),lv.PART.MAIN)
74+
container.set_style_border_width(1, lv.PART.MAIN)
75+
container.scroll_to_view(True) # scroll to bring it into view
76+
77+
def defocus_container(self, container):
78+
print(f"container {container} defocused, unsetting border...")
79+
container.set_style_border_width(0, lv.PART.MAIN)
80+
81+
def startSettingActivity(self, setting):
82+
intent = Intent(activity_class=SettingActivity)
83+
intent.putExtra("prefs", self.prefs)
84+
intent.putExtra("setting", setting)
85+
self.startActivity(intent)

0 commit comments

Comments
 (0)