From 000441e8d6ae4027752038d2479b806c58078675 Mon Sep 17 00:00:00 2001 From: hoangvu12 Date: Sat, 2 Dec 2023 16:52:36 +0700 Subject: [PATCH] feat(updater): add downloading progress --- src/ui/updater.tsx | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/src/ui/updater.tsx b/src/ui/updater.tsx index e87077c..af0e36f 100644 --- a/src/ui/updater.tsx +++ b/src/ui/updater.tsx @@ -92,6 +92,7 @@ const Updater = () => { const [release, setRelease] = useState(null); const [isLoading, setIsLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(null); + const [downloadPercentage, setDownloadPercentage] = useState(0); const bottomSheetRef = useRef(null); @@ -127,14 +128,26 @@ const Updater = () => { const downloadedApkUrl = `${FileSystem.cacheDirectory}${apkUrl.name}`; - const result = await FileSystem.downloadAsync( + const download = FileSystem.createDownloadResumable( apkUrl.browser_download_url, - downloadedApkUrl + downloadedApkUrl, + {}, + (downloadProgress) => { + setDownloadPercentage( + downloadProgress.totalBytesWritten / + downloadProgress.totalBytesExpectedToWrite + ); + } ); + const result = await download.downloadAsync(); + + if (!result) + throw new Error('Failed to download the apk (Task was cancelled)'); + if (result.status !== 200) throw new Error('Failed to download the apk'); - const contentUri = await FileSystem.getContentUriAsync(downloadedApkUrl); + const contentUri = await FileSystem.getContentUriAsync(result.uri); const startActivityAsync = await IntentLauncher.startActivityAsync( 'android.intent.action.INSTALL_PACKAGE', @@ -186,6 +199,25 @@ const Updater = () => { Update now + {isLoading ? ( + + Downloading progress: + + + + + + + {Math.round(downloadPercentage * 100).toFixed(0)}% + + + + + ) : null} + {errorMessage ? ( Error: {errorMessage}