@@ -24,6 +24,7 @@ class AppStore(Activity):
2424 install_label = None
2525 please_wait_label = None
2626 progress_bar = None
27+ _icon_widgets = {}
2728
2829 def onCreate (self ):
2930 self .main_screen = lv .obj ()
@@ -56,7 +57,6 @@ def download_app_index(self, json_url):
5657 if response and response .status_code == 200 :
5758 #print(f"Got response text: {response.text}")
5859 try :
59- applist = json .loads (response .text )
6060 for app in json .loads (response .text ):
6161 try :
6262 self .apps .append (App (app ["name" ], app ["publisher" ], app ["short_description" ], app ["long_description" ], app ["icon_url" ], app ["download_url" ], app ["fullname" ], app ["version" ], app ["category" ], app ["activities" ]))
@@ -70,6 +70,7 @@ def download_app_index(self, json_url):
7070 time .sleep_ms (200 )
7171 self .update_ui_threadsafe_if_foreground (self .please_wait_label .add_flag , lv .obj .FLAG .HIDDEN )
7272 self .update_ui_threadsafe_if_foreground (self .create_apps_list )
73+ time .sleep (0.1 ) # give the UI time to display the app list before starting to download
7374 self .download_icons ()
7475 except Exception as e :
7576 print (f"ERROR: could not parse reponse.text JSON: { e } " )
@@ -78,12 +79,13 @@ def download_app_index(self, json_url):
7879
7980 def create_apps_list (self ):
8081 print ("create_apps_list" )
81- default_icon_dsc = self .load_icon ("builtin/res/mipmap-mdpi/default_icon_64x64.png" )
8282 apps_list = lv .list (self .main_screen )
8383 apps_list .set_style_border_width (0 , 0 )
8484 apps_list .set_style_radius (0 , 0 )
8585 apps_list .set_style_pad_all (0 , 0 )
8686 apps_list .set_size (lv .pct (100 ), lv .pct (100 ))
87+ # Clear old icons
88+ self ._icon_widgets = {}
8789 print ("create_apps_list iterating" )
8890 for app in self .apps :
8991 print (app )
@@ -103,7 +105,8 @@ def create_apps_list(self):
103105 cont .add_event_cb (lambda e , a = app : self .show_app_detail (a ), lv .EVENT .CLICKED , None )
104106 icon_spacer = lv .image (cont )
105107 icon_spacer .set_size (64 , 64 )
106- app .image = icon_spacer
108+ icon_spacer .set_src (lv .SYMBOL .REFRESH )
109+ self ._icon_widgets [app .fullname ] = icon_spacer
107110 icon_spacer .add_event_cb (lambda e , a = app : self .show_app_detail (a ), lv .EVENT .CLICKED , None )
108111 label_cont = lv .obj (cont )
109112 label_cont .set_style_border_width (0 , 0 )
@@ -123,18 +126,21 @@ def create_apps_list(self):
123126
124127 def download_icons (self ):
125128 for app in self .apps :
126- if app .image_dsc :
127- print (f"Skipping icon download for { app .name } because already downloaded." )
128- continue
129129 if not self .has_foreground ():
130130 print (f"App is stopping, aborting icon downloads." )
131131 break
132- #if app.icon_path:
133- print (f"Downloading icon for { app .name } " )
134- image_dsc = self .download_icon (app .icon_url )
135- app .image_dsc = image_dsc # save it for the app detail page
136- self .update_ui_threadsafe_if_foreground (app .image .set_src , image_dsc )
137- time .sleep_ms (200 ) # not waiting here will result in some async_calls() not being executed
132+ if not app .icon_data :
133+ print (f"No icon_data found for { app } , downloading..." )
134+ app .icon_data = self .download_icon_data (app .icon_url )
135+ if app .icon_data :
136+ print ("download_icons has icon_data, showing it..." )
137+ icon_widget = self ._icon_widgets .get (app .fullname )
138+ if icon_widget :
139+ image_dsc = lv .image_dsc_t ({
140+ 'data_size' : len (app .icon_data ),
141+ 'data' : app .icon_data
142+ })
143+ self .update_ui_threadsafe_if_foreground (icon_widget .set_src , image_dsc ) # error: 'App' object has no attribute 'image'
138144 print ("Finished downloading icons." )
139145
140146 def show_app_detail (self , app ):
@@ -143,34 +149,20 @@ def show_app_detail(self, app):
143149 self .startActivity (intent )
144150
145151 @staticmethod
146- def download_icon (url ):
152+ def download_icon_data (url ):
147153 print (f"Downloading icon from { url } " )
148154 try :
149155 response = requests .get (url , timeout = 5 )
150156 if response .status_code == 200 :
151157 image_data = response .content
152158 print ("Downloaded image, size:" , len (image_data ), "bytes" )
153- image_dsc = lv .image_dsc_t ({
154- 'data_size' : len (image_data ),
155- 'data' : image_data
156- })
157- return image_dsc
159+ return image_data
158160 else :
159161 print ("Failed to download image: Status code" , response .status_code )
160162 except Exception as e :
161163 print (f"Exception during download of icon: { e } " )
162164 return None
163165
164- @staticmethod
165- def load_icon (icon_path ):
166- with open (icon_path , 'rb' ) as f :
167- image_data = f .read ()
168- image_dsc = lv .image_dsc_t ({
169- 'data_size' : len (image_data ),
170- 'data' : image_data
171- })
172- return image_dsc
173-
174166class AppDetail (Activity ):
175167
176168 action_label_install = "Install"
@@ -192,18 +184,23 @@ def onCreate(self):
192184 app_detail_screen .set_size (lv .pct (100 ), lv .pct (100 ))
193185 app_detail_screen .set_pos (0 , 40 )
194186 app_detail_screen .set_flex_flow (lv .FLEX_FLOW .COLUMN )
195- #
187+
196188 headercont = lv .obj (app_detail_screen )
197189 headercont .set_style_border_width (0 , 0 )
198190 headercont .set_style_pad_all (0 , 0 )
199191 headercont .set_flex_flow (lv .FLEX_FLOW .ROW )
200192 headercont .set_size (lv .pct (100 ), lv .SIZE_CONTENT )
201193 headercont .set_scrollbar_mode (lv .SCROLLBAR_MODE .OFF )
202194 icon_spacer = lv .image (headercont )
203- if app .image_dsc :
204- icon_spacer .set_src (app .image_dsc )
205195 icon_spacer .set_size (64 , 64 )
206- #
196+ if app .icon_data :
197+ image_dsc = lv .image_dsc_t ({
198+ 'data_size' : len (app .icon_data ),
199+ 'data' : app .icon_data
200+ })
201+ icon_spacer .set_src (image_dsc )
202+ else :
203+ icon_spacer .set_src (lv .SYMBOL .REFRESH )
207204 detail_cont = lv .obj (headercont )
208205 detail_cont .set_style_border_width (0 , 0 )
209206 detail_cont .set_style_radius (0 , 0 )
@@ -216,7 +213,7 @@ def onCreate(self):
216213 publisher_label = lv .label (detail_cont )
217214 publisher_label .set_text (app .publisher )
218215 publisher_label .set_style_text_font (lv .font_montserrat_16 , 0 )
219- #
216+
220217 self .progress_bar = lv .bar (app_detail_screen )
221218 self .progress_bar .set_width (lv .pct (100 ))
222219 self .progress_bar .set_range (0 , 100 )
0 commit comments