You've already forked MicroPythonOS
mirror of
https://github.com/m5stack/MicroPythonOS.git
synced 2026-05-20 11:51:27 -07:00
Settings app: use SettingsActivity framework
This commit is contained in:
@@ -1,8 +1,6 @@
|
||||
import lvgl as lv
|
||||
from mpos.apps import Activity, Intent
|
||||
from mpos.activity_navigator import ActivityNavigator
|
||||
|
||||
from mpos.ui.keyboard import MposKeyboard
|
||||
from mpos.apps import Intent
|
||||
from mpos.ui import SettingsActivity as SettingsActivityFramework
|
||||
from mpos import PackageManager, SettingActivity
|
||||
import mpos.config
|
||||
import mpos.ui
|
||||
@@ -12,7 +10,7 @@ from calibrate_imu import CalibrateIMUActivity
|
||||
from check_imu_calibration import CheckIMUCalibrationActivity
|
||||
|
||||
# Used to list and edit all settings:
|
||||
class SettingsActivity(Activity):
|
||||
class SettingsActivity(SettingsActivityFramework):
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.prefs = None
|
||||
@@ -60,79 +58,9 @@ class SettingsActivity(Activity):
|
||||
]
|
||||
|
||||
def onCreate(self):
|
||||
screen = lv.obj()
|
||||
print("creating SettingsActivity ui...")
|
||||
screen.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
|
||||
screen.set_flex_flow(lv.FLEX_FLOW.COLUMN)
|
||||
screen.set_style_border_width(0, 0)
|
||||
self.setContentView(screen)
|
||||
|
||||
def onResume(self, screen):
|
||||
# reload settings because the SettingsActivity might have changed them - could be optimized to only load if it did:
|
||||
self.prefs = mpos.config.SharedPreferences("com.micropythonos.settings")
|
||||
|
||||
# Create settings entries
|
||||
screen.clean()
|
||||
# Get the group for focusable objects
|
||||
focusgroup = lv.group_get_default()
|
||||
if not focusgroup:
|
||||
print("WARNING: could not get default focusgroup")
|
||||
|
||||
for setting in self.settings:
|
||||
#print(f"setting {setting.get('title')} has changed_callback {setting.get('changed_callback')}")
|
||||
# Container for each setting
|
||||
setting_cont = lv.obj(screen)
|
||||
setting_cont.set_width(lv.pct(100))
|
||||
setting_cont.set_height(lv.SIZE_CONTENT)
|
||||
setting_cont.set_style_border_width(1, 0)
|
||||
#setting_cont.set_style_border_side(lv.BORDER_SIDE.BOTTOM, 0)
|
||||
setting_cont.set_style_pad_all(mpos.ui.pct_of_display_width(2), 0)
|
||||
setting_cont.add_flag(lv.obj.FLAG.CLICKABLE)
|
||||
setting["cont"] = setting_cont # Store container reference for visibility control
|
||||
|
||||
# Title label (bold, larger)
|
||||
title = lv.label(setting_cont)
|
||||
title.set_text(setting["title"])
|
||||
title.set_style_text_font(lv.font_montserrat_16, 0)
|
||||
title.set_pos(0, 0)
|
||||
|
||||
# Value label (smaller, below title)
|
||||
value = lv.label(setting_cont)
|
||||
value.set_text(self.prefs.get_string(setting["key"], "(not set)" if not setting.get("dont_persist") else "(not persisted)"))
|
||||
value.set_style_text_font(lv.font_montserrat_12, 0)
|
||||
value.set_style_text_color(lv.color_hex(0x666666), 0)
|
||||
value.set_pos(0, 20)
|
||||
setting["value_label"] = value # Store reference for updating
|
||||
setting_cont.add_event_cb(lambda e, s=setting: self.startSettingActivity(s), lv.EVENT.CLICKED, None)
|
||||
setting_cont.add_event_cb(lambda e, container=setting_cont: self.focus_container(container),lv.EVENT.FOCUSED,None)
|
||||
setting_cont.add_event_cb(lambda e, container=setting_cont: self.defocus_container(container),lv.EVENT.DEFOCUSED,None)
|
||||
if focusgroup:
|
||||
focusgroup.add_obj(setting_cont)
|
||||
|
||||
def startSettingActivity(self, setting):
|
||||
ui_type = setting.get("ui")
|
||||
|
||||
activity_class = SettingActivity
|
||||
if ui_type == "activity":
|
||||
activity_class = setting.get("activity_class")
|
||||
if not activity_class:
|
||||
print("ERROR: Setting is defined as 'activity' ui without 'activity_class', aborting...")
|
||||
|
||||
intent = Intent(activity_class=activity_class)
|
||||
intent.putExtra("setting", setting)
|
||||
intent.putExtra("prefs", self.prefs)
|
||||
self.startActivity(intent)
|
||||
|
||||
def focus_container(self, container):
|
||||
print(f"container {container} focused, setting border...")
|
||||
container.set_style_border_color(lv.theme_get_color_primary(None),lv.PART.MAIN)
|
||||
container.set_style_border_width(1, lv.PART.MAIN)
|
||||
container.scroll_to_view(True) # scroll to bring it into view
|
||||
|
||||
def defocus_container(self, container):
|
||||
print(f"container {container} defocused, unsetting border...")
|
||||
container.set_style_border_width(0, lv.PART.MAIN)
|
||||
|
||||
if not self.prefs:
|
||||
self.prefs = mpos.config.SharedPreferences("com.micropythonos.settings")
|
||||
super().onCreate()
|
||||
|
||||
# Change handlers:
|
||||
def reset_into_bootloader(self, new_value):
|
||||
|
||||
@@ -15,6 +15,7 @@ from .app.activities.view import ViewActivity
|
||||
from .app.activities.share import ShareActivity
|
||||
|
||||
from .ui.setting_activity import SettingActivity
|
||||
from .ui.settings_activity import SettingsActivity
|
||||
|
||||
__all__ = [
|
||||
"App",
|
||||
@@ -23,5 +24,5 @@ __all__ = [
|
||||
"ConnectivityManager", "DownloadManager", "Intent",
|
||||
"ActivityNavigator", "PackageManager", "TaskManager",
|
||||
"ChooserActivity", "ViewActivity", "ShareActivity",
|
||||
"SettingActivity"
|
||||
"SettingActivity", "SettingsActivity"
|
||||
]
|
||||
|
||||
@@ -12,8 +12,14 @@ class SettingsActivity(Activity):
|
||||
settings = None
|
||||
|
||||
def onCreate(self):
|
||||
self.prefs = self.getIntent().extras.get("prefs")
|
||||
self.settings = self.getIntent().extras.get("settings")
|
||||
# Try to get from Intent first (for apps launched with Intent)
|
||||
intent = self.getIntent()
|
||||
if intent and intent.extras:
|
||||
self.prefs = intent.extras.get("prefs")
|
||||
self.settings = intent.extras.get("settings")
|
||||
|
||||
# If not set from Intent, subclasses should have set them in __init__()
|
||||
# (for apps that define their own settings)
|
||||
|
||||
print("creating SettingsActivity ui...")
|
||||
screen = lv.obj()
|
||||
@@ -23,7 +29,10 @@ class SettingsActivity(Activity):
|
||||
self.setContentView(screen)
|
||||
|
||||
def onResume(self, screen):
|
||||
wallet_type = self.prefs.get_string("wallet_type") # might have changed in the settings
|
||||
# If prefs/settings not set yet, they should be set by subclass
|
||||
if not self.prefs or not self.settings:
|
||||
print("WARNING: SettingsActivity.onResume() called but prefs or settings not set")
|
||||
return
|
||||
|
||||
# Create settings entries
|
||||
screen.clean()
|
||||
@@ -79,7 +88,13 @@ class SettingsActivity(Activity):
|
||||
container.set_style_border_width(0, lv.PART.MAIN)
|
||||
|
||||
def startSettingActivity(self, setting):
|
||||
intent = Intent(activity_class=SettingActivity)
|
||||
intent.putExtra("prefs", self.prefs)
|
||||
activity_class = SettingActivity
|
||||
if setting.get("ui") == "activity":
|
||||
activity_class = setting.get("activity_class")
|
||||
if not activity_class:
|
||||
print("ERROR: Setting is defined as 'activity' ui without 'activity_class', aborting...")
|
||||
|
||||
intent = Intent(activity_class=activity_class)
|
||||
intent.putExtra("setting", setting)
|
||||
intent.putExtra("prefs", self.prefs)
|
||||
self.startActivity(intent)
|
||||
|
||||
Reference in New Issue
Block a user