Skip to content

Commit 8cfb51b

Browse files
Settings app: use SettingsActivity framework
1 parent 6064805 commit 8cfb51b

File tree

3 files changed

+28
-84
lines changed

3 files changed

+28
-84
lines changed

internal_filesystem/builtin/apps/com.micropythonos.settings/assets/settings.py

Lines changed: 6 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import lvgl as lv
2-
from mpos.apps import Activity, Intent
3-
from mpos.activity_navigator import ActivityNavigator
4-
5-
from mpos.ui.keyboard import MposKeyboard
2+
from mpos.apps import Intent
3+
from mpos.ui import SettingsActivity as SettingsActivityFramework
64
from mpos import PackageManager, SettingActivity
75
import mpos.config
86
import mpos.ui
@@ -12,7 +10,7 @@
1210
from check_imu_calibration import CheckIMUCalibrationActivity
1311

1412
# Used to list and edit all settings:
15-
class SettingsActivity(Activity):
13+
class SettingsActivity(SettingsActivityFramework):
1614
def __init__(self):
1715
super().__init__()
1816
self.prefs = None
@@ -60,79 +58,9 @@ def __init__(self):
6058
]
6159

6260
def onCreate(self):
63-
screen = lv.obj()
64-
print("creating SettingsActivity ui...")
65-
screen.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
66-
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
67-
screen.set_style_border_width(0, 0)
68-
self.setContentView(screen)
69-
70-
def onResume(self, screen):
71-
# reload settings because the SettingsActivity might have changed them - could be optimized to only load if it did:
72-
self.prefs = mpos.config.SharedPreferences("com.micropythonos.settings")
73-
74-
# Create settings entries
75-
screen.clean()
76-
# Get the group for focusable objects
77-
focusgroup = lv.group_get_default()
78-
if not focusgroup:
79-
print("WARNING: could not get default focusgroup")
80-
81-
for setting in self.settings:
82-
#print(f"setting {setting.get('title')} has changed_callback {setting.get('changed_callback')}")
83-
# Container for each setting
84-
setting_cont = lv.obj(screen)
85-
setting_cont.set_width(lv.pct(100))
86-
setting_cont.set_height(lv.SIZE_CONTENT)
87-
setting_cont.set_style_border_width(1, 0)
88-
#setting_cont.set_style_border_side(lv.BORDER_SIDE.BOTTOM, 0)
89-
setting_cont.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
90-
setting_cont.add_flag(lv.obj.FLAG.CLICKABLE)
91-
setting["cont"] = setting_cont # Store container reference for visibility control
92-
93-
# Title label (bold, larger)
94-
title = lv.label(setting_cont)
95-
title.set_text(setting["title"])
96-
title.set_style_text_font(lv.font_montserrat_16, 0)
97-
title.set_pos(0, 0)
98-
99-
# Value label (smaller, below title)
100-
value = lv.label(setting_cont)
101-
value.set_text(self.prefs.get_string(setting["key"], "(not set)" if not setting.get("dont_persist") else "(not persisted)"))
102-
value.set_style_text_font(lv.font_montserrat_12, 0)
103-
value.set_style_text_color(lv.color_hex(0x666666), 0)
104-
value.set_pos(0, 20)
105-
setting["value_label"] = value # Store reference for updating
106-
setting_cont.add_event_cb(lambda e, s=setting: self.startSettingActivity(s), lv.EVENT.CLICKED, None)
107-
setting_cont.add_event_cb(lambda e, container=setting_cont: self.focus_container(container),lv.EVENT.FOCUSED,None)
108-
setting_cont.add_event_cb(lambda e, container=setting_cont: self.defocus_container(container),lv.EVENT.DEFOCUSED,None)
109-
if focusgroup:
110-
focusgroup.add_obj(setting_cont)
111-
112-
def startSettingActivity(self, setting):
113-
ui_type = setting.get("ui")
114-
115-
activity_class = SettingActivity
116-
if ui_type == "activity":
117-
activity_class = setting.get("activity_class")
118-
if not activity_class:
119-
print("ERROR: Setting is defined as 'activity' ui without 'activity_class', aborting...")
120-
121-
intent = Intent(activity_class=activity_class)
122-
intent.putExtra("setting", setting)
123-
intent.putExtra("prefs", self.prefs)
124-
self.startActivity(intent)
125-
126-
def focus_container(self, container):
127-
print(f"container {container} focused, setting border...")
128-
container.set_style_border_color(lv.theme_get_color_primary(None),lv.PART.MAIN)
129-
container.set_style_border_width(1, lv.PART.MAIN)
130-
container.scroll_to_view(True) # scroll to bring it into view
131-
132-
def defocus_container(self, container):
133-
print(f"container {container} defocused, unsetting border...")
134-
container.set_style_border_width(0, lv.PART.MAIN)
135-
61+
if not self.prefs:
62+
self.prefs = mpos.config.SharedPreferences("com.micropythonos.settings")
63+
super().onCreate()
13664

13765
# Change handlers:
13866
def reset_into_bootloader(self, new_value):

internal_filesystem/lib/mpos/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from .app.activities.share import ShareActivity
1616

1717
from .ui.setting_activity import SettingActivity
18+
from .ui.settings_activity import SettingsActivity
1819

1920
__all__ = [
2021
"App",
@@ -23,5 +24,5 @@
2324
"ConnectivityManager", "DownloadManager", "Intent",
2425
"ActivityNavigator", "PackageManager", "TaskManager",
2526
"ChooserActivity", "ViewActivity", "ShareActivity",
26-
"SettingActivity"
27+
"SettingActivity", "SettingsActivity"
2728
]

internal_filesystem/lib/mpos/ui/settings_activity.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ class SettingsActivity(Activity):
1212
settings = None
1313

1414
def onCreate(self):
15-
self.prefs = self.getIntent().extras.get("prefs")
16-
self.settings = self.getIntent().extras.get("settings")
15+
# Try to get from Intent first (for apps launched with Intent)
16+
intent = self.getIntent()
17+
if intent and intent.extras:
18+
self.prefs = intent.extras.get("prefs")
19+
self.settings = intent.extras.get("settings")
20+
21+
# If not set from Intent, subclasses should have set them in __init__()
22+
# (for apps that define their own settings)
1723

1824
print("creating SettingsActivity ui...")
1925
screen = lv.obj()
@@ -23,7 +29,10 @@ def onCreate(self):
2329
self.setContentView(screen)
2430

2531
def onResume(self, screen):
26-
wallet_type = self.prefs.get_string("wallet_type") # might have changed in the settings
32+
# If prefs/settings not set yet, they should be set by subclass
33+
if not self.prefs or not self.settings:
34+
print("WARNING: SettingsActivity.onResume() called but prefs or settings not set")
35+
return
2736

2837
# Create settings entries
2938
screen.clean()
@@ -79,7 +88,13 @@ def defocus_container(self, container):
7988
container.set_style_border_width(0, lv.PART.MAIN)
8089

8190
def startSettingActivity(self, setting):
82-
intent = Intent(activity_class=SettingActivity)
83-
intent.putExtra("prefs", self.prefs)
91+
activity_class = SettingActivity
92+
if setting.get("ui") == "activity":
93+
activity_class = setting.get("activity_class")
94+
if not activity_class:
95+
print("ERROR: Setting is defined as 'activity' ui without 'activity_class', aborting...")
96+
97+
intent = Intent(activity_class=activity_class)
8498
intent.putExtra("setting", setting)
99+
intent.putExtra("prefs", self.prefs)
85100
self.startActivity(intent)

0 commit comments

Comments
 (0)