Skip to content

Commit 64bd7cf

Browse files
settings.py: prepare for generic SettingsActivity
1 parent 736fd49 commit 64bd7cf

File tree

1 file changed

+64
-53
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.settings/assets

1 file changed

+64
-53
lines changed

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

Lines changed: 64 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,17 @@ def __init__(self):
4444
]
4545
self.settings = [
4646
# Basic settings, alphabetically:
47-
{"title": "Light/Dark Theme", "key": "theme_light_dark", "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("Light", "light"), ("Dark", "dark")]},
48-
{"title": "Theme Color", "key": "theme_primary_color", "value_label": None, "cont": None, "placeholder": "HTML hex color, like: EC048C", "ui": "dropdown", "ui_options": theme_colors},
49-
{"title": "Timezone", "key": "timezone", "value_label": None, "cont": None, "ui": "dropdown", "ui_options": self.get_timezone_tuples(), "changed_callback": lambda : mpos.time.refresh_timezone_preference()},
47+
{"title": "Light/Dark Theme", "key": "theme_light_dark", "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("Light", "light"), ("Dark", "dark")], "changed_callback": self.theme_changed},
48+
{"title": "Theme Color", "key": "theme_primary_color", "value_label": None, "cont": None, "placeholder": "HTML hex color, like: EC048C", "ui": "dropdown", "ui_options": theme_colors, "changed_callback": self.theme_changed},
49+
{"title": "Timezone", "key": "timezone", "value_label": None, "cont": None, "ui": "dropdown", "ui_options": self.get_timezone_tuples(), "changed_callback": lambda *args: mpos.time.refresh_timezone_preference()},
5050
# Advanced settings, alphabetically:
5151
#{"title": "Audio Output Device", "key": "audio_device", "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("Auto-detect", "auto"), ("I2S (Digital Audio)", "i2s"), ("Buzzer (PWM Tones)", "buzzer"), ("Both I2S and Buzzer", "both"), ("Disabled", "null")], "changed_callback": self.audio_device_changed},
5252
{"title": "Auto Start App", "key": "auto_start_app", "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [(app.name, app.fullname) for app in PackageManager.get_app_list()]},
5353
{"title": "Check IMU Calibration", "key": "check_imu_calibration", "value_label": None, "cont": None, "ui": "activity", "activity_class": "CheckIMUCalibrationActivity"},
5454
{"title": "Calibrate IMU", "key": "calibrate_imu", "value_label": None, "cont": None, "ui": "activity", "activity_class": "CalibrateIMUActivity"},
5555
# Expert settings, alphabetically
56-
{"title": "Restart to Bootloader", "key": "boot_mode", "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("Normal", "normal"), ("Bootloader", "bootloader")]}, # special that doesn't get saved
57-
{"title": "Format internal data partition", "key": "format_internal_data_partition", "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("No, do not format", "no"), ("Yes, erase all settings, files and non-builtin apps", "yes")]}, # special that doesn't get saved
56+
{"title": "Restart to Bootloader", "key": "boot_mode", "dont_persist": True, "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("Normal", "normal"), ("Bootloader", "bootloader")], "changed_callback": self.reset_into_bootloader},
57+
{"title": "Format internal data partition", "key": "format_internal_data_partition", "dont_persist": True, "value_label": None, "cont": None, "ui": "radiobuttons", "ui_options": [("No, do not format", "no"), ("Yes, erase all settings, files and non-builtin apps", "yes")], "changed_callback": self.format_internal_data_partition},
5858
# This is currently only in the drawer but would make sense to have it here for completeness:
5959
#{"title": "Display Brightness", "key": "display_brightness", "value_label": None, "cont": None, "placeholder": "A value from 0 to 100."},
6060
# Maybe also add font size (but ideally then all fonts should scale up/down)
@@ -128,6 +128,7 @@ def startSettingActivity(self, setting):
128128
# Handle traditional settings (existing code)
129129
intent = Intent(activity_class=SettingActivity)
130130
intent.putExtra("setting", setting)
131+
intent.putExtra("prefs", self.prefs)
131132
self.startActivity(intent)
132133

133134
@staticmethod
@@ -172,11 +173,54 @@ def defocus_container(self, container):
172173
print(f"container {container} defocused, unsetting border...")
173174
container.set_style_border_width(0, lv.PART.MAIN)
174175

176+
def reset_into_bootloader(self, new_value):
177+
if new_value is not "bootloader":
178+
return
179+
from mpos.bootloader import ResetIntoBootloader
180+
intent = Intent(activity_class=ResetIntoBootloader)
181+
self.startActivity(intent)
175182

176-
# Used to edit one setting:
183+
def format_internal_data_partition(self, new_value):
184+
if new_value is not "yes":
185+
return
186+
# Inspired by lvgl_micropython/lib/micropython/ports/esp32/modules/inisetup.py
187+
# Note: it would be nice to create a "FormatInternalDataPartition" activity with some progress or confirmation
188+
try:
189+
import vfs
190+
from flashbdev import bdev
191+
except Exception as e:
192+
print(f"Could not format internal data partition because: {e}")
193+
return
194+
if bdev.info()[4] == "vfs":
195+
print(f"Formatting {bdev} as LittleFS2")
196+
vfs.VfsLfs2.mkfs(bdev)
197+
fs = vfs.VfsLfs2(bdev)
198+
elif bdev.info()[4] == "ffat":
199+
print(f"Formatting {bdev} as FAT")
200+
vfs.VfsFat.mkfs(bdev)
201+
fs = vfs.VfsFat(bdev)
202+
print(f"Mounting {fs} at /")
203+
vfs.mount(fs, "/")
204+
print("Done formatting, (re)mounting /builtin")
205+
try:
206+
import freezefs_mount_builtin
207+
except Exception as e:
208+
# This will throw an exception if there is already a "/builtin" folder present
209+
print("settings.py: WARNING: could not import/run freezefs_mount_builtin: ", e)
210+
print("Done mounting, refreshing apps")
211+
PackageManager.refresh_apps()
212+
213+
def theme_changed(self, new_value):
214+
mpos.ui.set_theme(self.prefs)
215+
216+
"""
217+
SettingActivity is used to edit one setting.
218+
For now, it only supports strings.
219+
"""
177220
class SettingActivity(Activity):
178221

179222
active_radio_index = -1 # Track active radio button index
223+
prefs = None # taken from the intent
180224

181225
# Widgets:
182226
keyboard = None
@@ -186,12 +230,12 @@ class SettingActivity(Activity):
186230

187231
def __init__(self):
188232
super().__init__()
189-
self.prefs = mpos.config.SharedPreferences("com.micropythonos.settings")
190233
self.setting = None
191234

192235
def onCreate(self):
236+
self.prefs = self.getIntent().extras.get("prefs")
193237
setting = self.getIntent().extras.get("setting")
194-
#print(f"onCreate changed_callback: {setting.get('changed_callback')}")
238+
195239
settings_screen_detail = lv.obj()
196240
settings_screen_detail.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
197241
settings_screen_detail.set_flex_flow(lv.FLEX_FLOW.COLUMN)
@@ -351,44 +395,6 @@ def cambutton_cb_unused(self, event):
351395
self.startActivityForResult(Intent(activity_class=CameraApp).putExtra("scanqr_mode", True), self.gotqr_result_callback)
352396

353397
def save_setting(self, setting):
354-
# Check special cases that aren't saved
355-
if self.radio_container and self.active_radio_index == 1:
356-
if setting["key"] == "boot_mode":
357-
from mpos.bootloader import ResetIntoBootloader
358-
intent = Intent(activity_class=ResetIntoBootloader)
359-
self.startActivity(intent)
360-
return
361-
elif setting["key"] == "format_internal_data_partition":
362-
# Inspired by lvgl_micropython/lib/micropython/ports/esp32/modules/inisetup.py
363-
# Note: it would be nice to create a "FormatInternalDataPartition" activity with some progress or confirmation
364-
try:
365-
import vfs
366-
from flashbdev import bdev
367-
except Exception as e:
368-
print(f"Could not format internal data partition because: {e}")
369-
self.finish() # would be nice to show the error instead of silently returning
370-
return
371-
if bdev.info()[4] == "vfs":
372-
print(f"Formatting {bdev} as LittleFS2")
373-
vfs.VfsLfs2.mkfs(bdev)
374-
fs = vfs.VfsLfs2(bdev)
375-
elif bdev.info()[4] == "ffat":
376-
print(f"Formatting {bdev} as FAT")
377-
vfs.VfsFat.mkfs(bdev)
378-
fs = vfs.VfsFat(bdev)
379-
print(f"Mounting {fs} at /")
380-
vfs.mount(fs, "/")
381-
print("Done formatting, (re)mounting /builtin")
382-
try:
383-
import freezefs_mount_builtin
384-
except Exception as e:
385-
# This will throw an exception if there is already a "/builtin" folder present
386-
print("settings.py: WARNING: could not import/run freezefs_mount_builtin: ", e)
387-
print("Done mounting, refreshing apps")
388-
PackageManager.refresh_apps()
389-
self.finish()
390-
return
391-
392398
ui = setting.get("ui")
393399
ui_options = setting.get("ui_options")
394400
if ui and ui == "radiobuttons" and ui_options:
@@ -405,15 +411,20 @@ def save_setting(self, setting):
405411
else:
406412
new_value = ""
407413
old_value = self.prefs.get_string(setting["key"])
408-
editor = self.prefs.edit()
409-
editor.put_string(setting["key"], new_value)
410-
editor.commit()
414+
415+
# Save it
416+
if setting.get("dont_persist") is not True:
417+
editor = self.prefs.edit()
418+
editor.put_string(setting["key"], new_value)
419+
editor.commit()
420+
421+
# Update model for UI
411422
setting["value_label"].set_text(new_value if new_value else "(not set)")
423+
self.finish() # the self.finish (= back action) should happen before callback, in case it happens to start a new activity
424+
425+
# Call changed_callback if set
412426
changed_callback = setting.get("changed_callback")
413427
#print(f"changed_callback: {changed_callback}")
414428
if changed_callback and old_value != new_value:
415429
print(f"Setting {setting['key']} changed from {old_value} to {new_value}, calling changed_callback...")
416-
changed_callback()
417-
if setting["key"] == "theme_light_dark" or setting["key"] == "theme_primary_color":
418-
mpos.ui.set_theme(self.prefs)
419-
self.finish()
430+
changed_callback(new_value)

0 commit comments

Comments
 (0)