@@ -44,6 +44,7 @@ import de.marmaro.krt.ffupdater.utils.visibleAfterExecution
44
44
import de.marmaro.krt.ffupdater.utils.visibleDuringExecution
45
45
import kotlinx.coroutines.Deferred
46
46
import kotlinx.coroutines.Dispatchers
47
+ import kotlinx.coroutines.async
47
48
import kotlinx.coroutines.channels.Channel
48
49
import kotlinx.coroutines.launch
49
50
import kotlinx.coroutines.withContext
@@ -83,7 +84,7 @@ class DownloadActivity : AppCompatActivity() {
83
84
}
84
85
85
86
fun isDownloadForCurrentAppRunning (status : InstalledAppStatus ): Boolean {
86
- return this .status == status && deferred?.isActive == true
87
+ return this .status?.app == status.app && this .status?.latestVersion == status.latestVersion && deferred?.isActive == true
87
88
}
88
89
89
90
fun clear () {
@@ -282,7 +283,12 @@ class DownloadActivity : AppCompatActivity() {
282
283
private suspend fun reuseCurrentDownload (status : InstalledAppStatus ): Boolean {
283
284
debug(" start reusing existing download" )
284
285
return try {
285
- reuseCurrentDownloadWithoutErrorChecking(status)
286
+ findViewById<View >(R .id.downloadingFile).visibleDuringExecution {
287
+ findViewById<View >(R .id.downloadedFile).visibleAfterExecution {
288
+ reuseCurrentDownloadWithoutErrorChecking(status)
289
+ }
290
+ }
291
+
286
292
true
287
293
} catch (e: Exception ) {
288
294
debug(" reusing the existing download of $[app.name} failed" , e)
@@ -297,19 +303,29 @@ class DownloadActivity : AppCompatActivity() {
297
303
debug(" reuse existing download" )
298
304
gui.setText(R .id.downloadingFileUrl, status.latestVersion.downloadUrl)
299
305
gui.setText(R .id.downloadingFileText, getString(download_activity__download_app_with_status))
300
-
301
306
findViewById<View >(R .id.downloadingFile).visibleDuringExecution {
302
- gui.showDownloadProgress(downloadViewModel.progressChannel!! )
303
- // NPE was thrown in https://github.com/Tobi823/ffupdater/issues/359 - it should be safe to ignore null values
304
- downloadViewModel.deferred?.await()
307
+ for (update in downloadViewModel.progressChannel!! ) {
308
+ withContext(Dispatchers .Main ) {
309
+ gui.updateDownloadProgressIndication(update)
310
+ }
311
+ }
305
312
}
313
+ downloadViewModel.deferred!! .await()
306
314
}
307
315
308
316
@MainThread
309
317
private suspend fun startDownload (status : InstalledAppStatus ): Boolean {
310
- debug(" start download (1/3 )" )
318
+ debug(" start download (1/2 )" )
311
319
return try {
312
- startDownloadWithoutErrorChecking(status)
320
+ gui.setText(R .id.downloadingFileUrl, status.latestVersion.downloadUrl)
321
+ gui.setText(R .id.downloadedFileUrl, status.latestVersion.downloadUrl)
322
+ gui.setText(R .id.downloadingFileText, getString(download_activity__download_app_with_status))
323
+ findViewById<View >(R .id.downloadingFile).visibleDuringExecution {
324
+ findViewById<View >(R .id.downloadedFile).visibleAfterExecution {
325
+ downloadWithUiProgressIndication(status)
326
+ }
327
+ }
328
+ true
313
329
} catch (e: Exception ) {
314
330
debug(" download failed" , e)
315
331
if (e !is DisplayableException ) throw e
@@ -319,30 +335,24 @@ class DownloadActivity : AppCompatActivity() {
319
335
}
320
336
321
337
@MainThread
322
- private suspend fun startDownloadWithoutErrorChecking (status : InstalledAppStatus ): Boolean {
323
- debug(" start downloading (2/3)" )
324
- gui.setText(R .id.downloadingFileUrl, status.latestVersion.downloadUrl)
325
- gui.setText(R .id.downloadedFileUrl, status.latestVersion.downloadUrl)
326
- gui.setText(R .id.downloadingFileText, getString(download_activity__download_app_with_status))
327
- findViewById<View >(R .id.downloadingFile).visibleDuringExecution {
328
- findViewById<View >(R .id.downloadedFile).visibleAfterExecution {
329
- startDownloadInternal(status)
338
+ private suspend fun downloadWithUiProgressIndication (status : InstalledAppStatus ) {
339
+ debug(" start downloading (2/2)" )
340
+
341
+ val viewModelDownload = downloadViewModel.viewModelScope.launch {
342
+ val channel = Channel <DownloadStatus >()
343
+ val download = async {
344
+ appImpl.download(applicationContext, status.latestVersion, channel)
330
345
}
346
+ downloadViewModel.storeNewRunningDownload(status, download, channel)
331
347
}
332
- return true
333
- }
334
348
335
- @MainThread
336
- private suspend fun startDownloadInternal (status : InstalledAppStatus ) {
337
- debug(" start downloading (3/3)" )
338
- val appImpl = app.findImpl()
339
- val coroutineContext = downloadViewModel.viewModelScope.coroutineContext
340
- withContext(coroutineContext) {
341
- appImpl.download(applicationContext, status.latestVersion) { deferred, progressChannel ->
342
- downloadViewModel.storeNewRunningDownload(status, deferred, progressChannel)
343
- gui.showDownloadProgress(progressChannel)
349
+ for (update in downloadViewModel.progressChannel!! ) {
350
+ withContext(Dispatchers .Main ) {
351
+ gui.updateDownloadProgressIndication(update)
344
352
}
345
353
}
354
+
355
+ viewModelDownload.join()
346
356
}
347
357
348
358
@MainThread
0 commit comments