Skip to content

Commit 301132f

Browse files
Start framework docs
1 parent 0dee468 commit 301132f

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

docs/frameworks/preferences.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
MicroPythonOS provides a simple way to load and save preferences, similar to Android's "SharedPreferences" framework.
2+
3+
Here's a simple example of how to add it to your app, taken from [QuasiNametag](https://github.com/QuasiKili/MPOS-QuasiNametag):
4+
5+
<pre>
6+
```
7+
--- quasinametag.py.orig 2025-10-29 12:24:27.494193748 +0100
8+
+++ quasinametag.py 2025-10-29 12:07:59.357264302 +0100
9+
@@ -1,4 +1,5 @@
10+
from mpos.apps import Activity
11+
+import mpos.config
12+
import mpos.ui
13+
import mpos.ui.anim
14+
import mpos.ui.focus_direction
15+
@@ -42,6 +43,12 @@
16+
# Add key event handler to container to catch all key events
17+
container.add_event_cb(self.global_key_handler, lv.EVENT.KEY, None)
18+
19+
+ print("Loading preferences...")
20+
+ prefs = mpos.config.SharedPreferences("com.quasikili.quasinametag")
21+
+ self.name_text = prefs.get_string("name_text", self.name_text)
22+
+ self.fg_color = prefs.get_int("fg_color", self.fg_color)
23+
+ self.bg_color = prefs.get_int("bg_color", self.bg_color)
24+
+
25+
# Create both screens as children of the container
26+
self.create_edit_screen(container)
27+
self.create_display_screen(container)
28+
@@ -263,6 +270,13 @@
29+
if focusgroup:
30+
mpos.ui.focus_direction.emulate_focus_obj(focusgroup, self.display_screen)
31+
32+
+ print("Saving preferences...")
33+
+ editor = mpos.config.SharedPreferences("com.quasikili.quasinametag").edit()
34+
+ editor.put_string("name_text", self.name_text)
35+
+ editor.put_int("fg_color", self.fg_color)
36+
+ editor.put_int("bg_color", self.bg_color)
37+
+ editor.commit()
38+
+
39+
def update_display_screen(self):
40+
# Set background color
41+
self.display_screen.set_style_bg_color(lv.color_hex(self.bg_color), 0)
42+
```
43+
</pre>
44+
45+
Here's a more complete example:
46+
47+
<pre>
48+
```
49+
# Example usage with access_points as a dictionary
50+
def main():
51+
# Initialize SharedPreferences
52+
prefs = SharedPreferences("com.example.test_shared_prefs")
53+
54+
# Save some simple settings and a dictionary-based access_points
55+
editor = prefs.edit()
56+
editor.put_string("someconfig", "somevalue")
57+
editor.put_int("othervalue", 54321)
58+
editor.put_dict("access_points", {
59+
"example_ssid1": {"password": "examplepass1", "detail": "yes please", "numericalconf": 1234},
60+
"example_ssid2": {"password": "examplepass2", "detail": "no please", "numericalconf": 9875}
61+
})
62+
editor.apply()
63+
64+
# Read back the settings
65+
print("Simple settings:")
66+
print("someconfig:", prefs.get_string("someconfig", "default_value"))
67+
print("othervalue:", prefs.get_int("othervalue", 0))
68+
69+
print("\nAccess points (dictionary-based):")
70+
ssids = prefs.get_dict_keys("access_points")
71+
for ssid in ssids:
72+
print(f"Access Point SSID: {ssid}")
73+
print(f" Password: {prefs.get_dict_item_field('access_points', ssid, 'password', 'N/A')}")
74+
print(f" Detail: {prefs.get_dict_item_field('access_points', ssid, 'detail', 'N/A')}")
75+
print(f" Numerical Conf: {prefs.get_dict_item_field('access_points', ssid, 'numericalconf', 0)}")
76+
print(f" Full config: {prefs.get_dict_item('access_points', ssid)}")
77+
78+
# Add a new access point
79+
editor = prefs.edit()
80+
editor.put_dict_item("access_points", "example_ssid3", {
81+
"password": "examplepass3",
82+
"detail": "maybe",
83+
"numericalconf": 5555
84+
})
85+
editor.commit()
86+
87+
# Update an existing access point
88+
editor = prefs.edit()
89+
editor.put_dict_item("access_points", "example_ssid1", {
90+
"password": "newpass1",
91+
"detail": "updated please",
92+
"numericalconf": 4321
93+
})
94+
editor.commit()
95+
96+
# Remove an access point
97+
editor = prefs.edit()
98+
editor.remove_dict_item("access_points", "example_ssid2")
99+
editor.commit()
100+
101+
# Read updated access points
102+
print("\nUpdated access points (dictionary-based):")
103+
ssids = prefs.get_dict_keys("access_points")
104+
for ssid in ssids:
105+
print(f"Access Point SSID: {ssid}: {prefs.get_dict_item('access_points', ssid)}")
106+
107+
# Demonstrate compatibility with list-based configs
108+
editor = prefs.edit()
109+
editor.put_list("somelist", [
110+
{"a": "ok", "numericalconf": 1111},
111+
{"a": "not ok", "numericalconf": 2222}
112+
])
113+
editor.apply()
114+
115+
print("\List-based config:")
116+
somelist = prefs.get_list("somelist")
117+
for i, ap in enumerate(somelist):
118+
print(f"List item {i}:")
119+
print(f" a: {prefs.get_list_item('somelist', i, 'a', 'N/A')}")
120+
print(f" Full dict: {prefs.get_list_item_dict('somelist', i)}")
121+
122+
if __name__ == '__main__':
123+
main()
124+
```
125+
</pre>

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ nav:
4646
- App Store: apps/appstore.md
4747
- Creating Apps: apps/creating-apps.md
4848
- Bundling Apps: apps/bundling-apps.md
49+
- Frameworks:
50+
- Preferences: frameworks/preferences.md
4951
- Architecture:
5052
- Overview: architecture/overview.md
5153
- System Components: architecture/system-components.md

0 commit comments

Comments
 (0)