Skip to content

Commit 0977ab2

Browse files
AppStore: accurate progress bar for download
1 parent ffc0cd9 commit 0977ab2

File tree

1 file changed

+23
-8
lines changed
  • internal_filesystem/builtin/apps/com.micropythonos.appstore/assets

1 file changed

+23
-8
lines changed

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

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def show_app_detail(self, app):
177177
intent.putExtra("appstore", self)
178178
self.startActivity(intent)
179179

180-
async def download_url(self, url, outfile=None, total_size=None):
180+
async def download_url(self, url, outfile=None, total_size=None, progress_callback=None):
181181
print(f"Downloading {url}")
182182
#await TaskManager.sleep(4) # test slowness
183183
try:
@@ -218,7 +218,11 @@ async def download_url(self, url, outfile=None, total_size=None):
218218

219219
if chunk:
220220
partial_size += len(chunk)
221-
print(f"progress: {partial_size} / {total_size} bytes")
221+
progress_pct = round((partial_size * 100) / int(total_size))
222+
print(f"progress: {partial_size} / {total_size} bytes = {progress_pct}%")
223+
if progress_callback:
224+
await progress_callback(progress_pct)
225+
#await TaskManager.sleep(1) # test slowness
222226
if fd:
223227
fd.write(chunk)
224228
else:
@@ -513,14 +517,26 @@ async def uninstall_app(self, app_fullname):
513517
self.update_button.remove_flag(lv.obj.FLAG.HIDDEN)
514518
self.install_button.set_size(lv.pct(47), 40) # if a builtin app was removed, then it was overridden, and a new version is available, so make space for update button
515519

520+
async def pcb(self, percent):
521+
print(f"pcb called: {percent}")
522+
scaled_percent_start = 5 # before 5% is preparation
523+
scaled_percent_finished = 60 # after 60% is unzip
524+
scaled_percent_diff = scaled_percent_finished - scaled_percent_start
525+
scale = 100 / scaled_percent_diff # 100 / 55 = 1.81
526+
scaled_percent = round(percent / scale)
527+
scaled_percent += scaled_percent_start
528+
self.progress_bar.set_value(scaled_percent, True)
529+
516530
async def download_and_install(self, app_obj, dest_folder):
517531
zip_url = app_obj.download_url
518532
app_fullname = app_obj.fullname
519-
download_url_size = app_obj.download_url_size
533+
download_url_size = None
534+
if hasattr(app_obj, "download_url_size"):
535+
download_url_size = app_obj.download_url_size
520536
self.install_button.add_state(lv.STATE.DISABLED)
521537
self.install_label.set_text("Please wait...")
522538
self.progress_bar.remove_flag(lv.obj.FLAG.HIDDEN)
523-
self.progress_bar.set_value(20, True)
539+
self.progress_bar.set_value(5, True)
524540
await TaskManager.sleep(1) # seems silly but otherwise it goes so quickly that the user can't tell something happened and gets confused
525541
# Download the .mpk file to temporary location
526542
try:
@@ -532,17 +548,16 @@ async def download_and_install(self, app_obj, dest_folder):
532548
os.mkdir("tmp")
533549
except Exception:
534550
pass
535-
self.progress_bar.set_value(40, True)
536551
temp_zip_path = "tmp/temp.mpk"
537552
print(f"Downloading .mpk file from: {zip_url} to {temp_zip_path}")
538-
result = await self.appstore.download_url(zip_url, outfile=temp_zip_path, total_size=download_url_size)
553+
result = await self.appstore.download_url(zip_url, outfile=temp_zip_path, total_size=download_url_size, progress_callback=self.pcb)
539554
if result is not True:
540555
print("Download failed...") # Would be good to show an error to the user if this failed...
541556
else:
542-
self.progress_bar.set_value(60, True)
543557
print("Downloaded .mpk file, size:", os.stat(temp_zip_path)[6], "bytes")
544558
# Install it:
545-
PackageManager.install_mpk(temp_zip_path, dest_folder)
559+
PackageManager.install_mpk(temp_zip_path, dest_folder) # 60 until 90 percent is the unzip but no progress there...
560+
self.progress_bar.set_value(90, True)
546561
# Make sure there's no leftover file filling the storage:
547562
try:
548563
os.remove(temp_zip_path)

0 commit comments

Comments
 (0)