Skip to content

Commit c70fe71

Browse files
Nice progress bar for app installs
1 parent 21b98bb commit c70fe71

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

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

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,64 @@ def load_icon(url):
6464
except ImportError:
6565
zipfile = None
6666

67-
def download_and_unzip(zip_url, dest_folder):
67+
def download_and_unzip(zip_url, dest_folder, label):
68+
global install_button, progress_bar
69+
install_button.remove_flag(lv.obj.FLAG.CLICKABLE) # TODO: change color so it's clear the button is not clickable
70+
label.set_text("Please wait...") # TODO: Put "Cancel" if cancellation is possible
71+
progress_bar.remove_flag(lv.obj.FLAG.HIDDEN)
72+
progress_bar.set_value(20, lv.ANIM.ON)
73+
time.sleep_ms(500)
6874
try:
6975
# Step 1: Download the .zip file
7076
print(f"Downloading .zip file from: {zip_url}")
7177
response = urequests.get(zip_url, timeout=10)
7278
if response.status_code != 200:
7379
print("Download failed: Status code", response.status_code)
7480
response.close()
75-
return False
81+
label.set_text(action_label_install)
82+
progress_bar.set_value(40, lv.ANIM.ON)
83+
time.sleep_ms(500)
7684
# Save the .zip file to a temporary location
77-
os.mkdir("/tmp")
85+
try:
86+
os.remove(temp_zip_path)
87+
except Exception:
88+
pass
89+
try:
90+
os.mkdir("/tmp")
91+
except Exception:
92+
pass
7893
temp_zip_path = "/tmp/temp.zip"
7994
print(f"Writing to temporary zip path: {temp_zip_path}")
8095
# TODO: check free available space first!
8196
with open(temp_zip_path, "wb") as f:
8297
f.write(response.content)
98+
progress_bar.set_value(60, lv.ANIM.ON)
99+
time.sleep_ms(500)
83100
response.close()
84101
print("Downloaded .zip file, size:", os.stat(temp_zip_path)[6], "bytes")
85102
# Step 2: Unzip the file
86103
if zipfile is None:
87-
print("Error: zipfile module not available in this MicroPython build")
88-
return False
104+
print("WARNING: zipfile module not available in this MicroPython build, unzip will fail!")
89105
print("Unzipping it to:", dest_folder)
90106
with zipfile.ZipFile(temp_zip_path, "r") as zip_ref:
91107
zip_ref.extractall(dest_folder)
108+
progress_bar.set_value(80, lv.ANIM.ON)
109+
time.sleep_ms(500)
92110
print("Unzipped successfully")
93111
# Step 3: Clean up
94112
os.remove(temp_zip_path)
95113
print("Removed temporary .zip file")
96-
return True
97114
except Exception as e:
98115
print("Operation failed:", str(e))
99-
return False
100116
finally:
101117
if 'response' in locals():
102118
response.close()
119+
progress_bar.set_value(80, lv.ANIM.OFF)
120+
progress_bar.set_value(100, lv.ANIM.OFF)
121+
time.sleep(1)
122+
label.set_text("Uninstall")
123+
progress_bar.add_flag(lv.obj.FLAG.HIDDEN)
124+
install_button.add_flag(lv.obj.FLAG.CLICKABLE)
103125

104126

105127
def download_apps(json_url):
@@ -227,35 +249,21 @@ def show_app_detail(app):
227249

228250

229251
def toggle_install(download_url, fullname):
230-
global install_button, progress_bar, action_label_install, action_label_uninstall
252+
global install_button, action_label_install, action_label_uninstall
231253
print(f"Install button clicked for {download_url} and fullname {fullname}")
232254
label = install_button.get_child(0)
233255
if label.get_text() == action_label_install:
234-
install_button.remove_flag(lv.obj.FLAG.CLICKABLE) # TODO: change color so it's clear the button is not clickable
235-
label.set_text("Please wait...") # TODO: Put "Cancel" if cancellation is possible
236-
progress_bar.remove_flag(lv.obj.FLAG.HIDDEN)
237-
progress_bar.set_value(40, lv.ANIM.OFF)
238-
# TODO: do the download and install in a new thread with a few sleeps so it can be cancelled...
239-
#try:
240-
# _thread.stack_size(16384)
241-
# _thread.start_new_thread(execute_script, (scriptname, is_file, is_launcher, is_graphical))
242-
#except Exception as e:
243-
# print("Could not start download_and_unzip thread: ", e)
244-
download_and_unzip(download_url, f"/apps/{fullname}")
245-
progress_bar.set_value(80, lv.ANIM.OFF)
246-
time.sleep(1)
247-
progress_bar.set_value(100, lv.ANIM.OFF)
248-
time.sleep(1)
249-
label.set_text("Uninstall") # Opening doesn't work because it races along with the launcher to use the screen...
250-
progress_bar.add_flag(lv.obj.FLAG.HIDDEN)
251-
install_button.add_flag(lv.obj.FLAG.CLICKABLE)
252-
elif label.get_text() == "Open":
253-
print("Open button clicked, starting app...")
254-
start_app(f"/apps/{fullname}")
256+
try:
257+
_thread.stack_size(16384)
258+
_thread.start_new_thread(download_and_unzip, (download_url, f"/apps/{fullname}", label))
259+
except Exception as e:
260+
print("Could not start download_and_unzip thread: ", e)
255261
elif label.get_text() == action_label_uninstall:
256262
print("Uninstalling app....")
257-
else: # if the button text was "Please wait..." or "Uninstall" or "Installed!"
258-
label.set_text("Install")
263+
import shutil
264+
if fullname:
265+
shutil.rmtree(f"/apps/{fullname}")
266+
label.set_text("Install")
259267

260268

261269
def back_to_main(event):

internal_filesystem/lib/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ This /lib folder contains:
33
- traceback.mpy from https://github.com/micropython/micropython-lib
44
- https://github.com/glenn20/micropython-esp32-ota/ installed with import mip; mip.install('github:glenn20/micropython-esp32-ota/mip/ota')
55
- mip.install('github:jonnor/micropython-zipfile')
6+
- mip.install("shutil") for shutil.rmtree('/apps/com.example.files') # for rmtree()
67

0 commit comments

Comments
 (0)