Skip to content

Commit 984d644

Browse files
AppStore app: use update_ui_threadsafe_if_foreground
1 parent 72947af commit 984d644

File tree

2 files changed

+9
-21
lines changed

2 files changed

+9
-21
lines changed

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ class AppStore(Activity):
1616
apps = []
1717
app_index_url = "https://apps.micropythonos.com/app_index.json"
1818
can_check_network = True
19-
keep_running = False
2019

2120
# Widgets:
2221
main_screen = None
@@ -34,7 +33,7 @@ def onCreate(self):
3433
self.setContentView(self.main_screen)
3534

3635
def onResume(self, screen):
37-
self.keep_running = True
36+
super().onResume(screen)
3837
if len(self.apps):
3938
return # already downloaded them
4039
try:
@@ -47,16 +46,12 @@ def onResume(self, screen):
4746
_thread.stack_size(mpos.apps.good_stack_size())
4847
_thread.start_new_thread(self.download_app_index, (self.app_index_url,))
4948

50-
def onStop(self, screen):
51-
self.keep_running = False
52-
5349
def download_app_index(self, json_url):
5450
try:
5551
response = requests.get(json_url, timeout=10)
5652
except Exception as e:
5753
print("Download failed:", e)
58-
if self.keep_running:
59-
lv.async_call(lambda l, error=e: self.please_wait_label.set_text(f"App index download \n{json_url}\ngot error: {error}"), None)
54+
self.update_ui_threadsafe_if_foreground(self.please_wait_label.set_text, f"App index download \n{json_url}\ngot error: {e}")
6055
return
6156
if response and response.status_code == 200:
6257
#print(f"Got response text: {response.text}")
@@ -73,9 +68,8 @@ def download_app_index(self, json_url):
7368
# Sort apps by app.name
7469
self.apps.sort(key=lambda x: x.name.lower()) # Use .lower() for case-insensitive sorting
7570
time.sleep_ms(200)
76-
if self.keep_running:
77-
lv.async_call(lambda l: self.please_wait_label.add_flag(lv.obj.FLAG.HIDDEN), None)
78-
lv.async_call(lambda l: self.create_apps_list(), None)
71+
self.update_ui_threadsafe_if_foreground(self.please_wait_label.add_flag, lv.obj.FLAG.HIDDEN)
72+
self.update_ui_threadsafe_if_foreground(self.create_apps_list)
7973
except Exception as e:
8074
print(f"ERROR: could not parse reponse.text JSON: {e}")
8175
finally:
@@ -91,6 +85,7 @@ def create_apps_list(self):
9185
apps_list.set_size(lv.pct(100), lv.pct(100))
9286
print("create_apps_list iterating")
9387
for app in self.apps:
88+
print(app)
9489
item = apps_list.add_button(None, "Test")
9590
item.set_style_pad_all(0, 0)
9691
#item.set_style_border_width(0, 0)
@@ -126,7 +121,7 @@ def create_apps_list(self):
126121
print("create_apps_list app done")
127122
try:
128123
_thread.stack_size(mpos.apps.good_stack_size())
129-
_thread.start_new_thread(self.download_icons,())
124+
_thread.start_new_thread(self.download_icons,()) # maybe there's no need for a new thread here, just do it in download_app_index()?
130125
except Exception as e:
131126
print("Could not start thread to download icons: ", e)
132127

@@ -135,17 +130,13 @@ def download_icons(self):
135130
if app.image_dsc:
136131
print(f"Skipping icon download for {app.name} because already downloaded.")
137132
continue
138-
if not self.keep_running:
133+
if not self.has_foreground():
139134
print(f"App is stopping, aborting icon downloads.")
140135
break
141136
print(f"Downloading icon for {app.name}")
142137
image_dsc = self.download_icon(app.icon_url)
143138
app.image_dsc = image_dsc # save it for the app detail page
144-
if not self.keep_running:
145-
print(f"App is stopping, aborting all icon downloads.")
146-
break
147-
else:
148-
lv.async_call(lambda l: app.image.set_src(image_dsc), None)
139+
self.update_ui_threadsafe_if_foreground(app.image.set_src, image_dsc)
149140
time.sleep_ms(200) # not waiting here will result in some async_calls() not being executed
150141
print("Finished downloading icons.")
151142

internal_filesystem/lib/mpos/app/activity.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,4 @@ def if_foreground(self, func, *args, **kwargs):
7373
# Update the UI in a threadsafe way if the Activity is in the foreground
7474
def update_ui_threadsafe_if_foreground(self, func, *args, **kwargs):
7575
# lv.async_call() is needed to update the UI from another thread than the main one (as LVGL is not thread safe)
76-
lv.async_call(
77-
lambda _: self.if_foreground(func, *args, **kwargs),
78-
None
79-
)
76+
lv.async_call(lambda _: self.if_foreground(func, *args, **kwargs),None)

0 commit comments

Comments
 (0)