@@ -16,7 +16,6 @@ class AppStore(Activity):
1616apps = []
1717app_index_url = "https://apps.micropythonos.com/app_index.json"
1818can_check_network = True
19- keep_running = False
2019
2120# Widgets:
2221main_screen = None
@@ -34,7 +33,7 @@ def onCreate(self):
3433self .setContentView (self .main_screen )
3534
3635def onResume (self ,screen ):
37- self . keep_running = True
36+ super (). onResume ( screen )
3837if len (self .apps ):
3938return # already downloaded them
4039try :
@@ -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-
5349def download_app_index (self ,json_url ):
5450try :
5551response = requests .get (json_url ,timeout = 10 )
5652except Exception as e :
5753print ("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 } \n got error:{ error } " ),None )
54+ self .update_ui_threadsafe_if_foreground (self .please_wait_label .set_text ,f"App index download\n { json_url } \n got error:{ e } " )
6055return
6156if 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
7469self .apps .sort (key = lambda x :x .name .lower ())# Use .lower() for case-insensitive sorting
7570time .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 )
7973except Exception as e :
8074print (f"ERROR: could not parse reponse.text JSON:{ e } " )
8175finally :
@@ -91,6 +85,7 @@ def create_apps_list(self):
9185apps_list .set_size (lv .pct (100 ),lv .pct (100 ))
9286print ("create_apps_list iterating" )
9387for app in self .apps :
88+ print (app )
9489item = apps_list .add_button (None ,"Test" )
9590item .set_style_pad_all (0 ,0 )
9691#item.set_style_border_width(0, 0)
@@ -126,7 +121,7 @@ def create_apps_list(self):
126121print ("create_apps_list app done" )
127122try :
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()?
130125except Exception as e :
131126print ("Could not start thread to download icons: " ,e )
132127
@@ -135,17 +130,13 @@ def download_icons(self):
135130if app .image_dsc :
136131print (f"Skipping icon download for{ app .name } because already downloaded." )
137132continue
138- if not self .keep_running :
133+ if not self .has_foreground () :
139134print (f"App is stopping, aborting icon downloads." )
140135break
141136print (f"Downloading icon for{ app .name } " )
142137image_dsc = self .download_icon (app .icon_url )
143138app .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 )
149140time .sleep_ms (200 )# not waiting here will result in some async_calls() not being executed
150141print ("Finished downloading icons." )
151142