0
0
mirror of https://github.com/mediathekview/zapp.git synced 2024-09-19 20:02:17 +02:00

Move download to background thread

This commit is contained in:
Christine Coenen 2022-10-28 20:35:30 +02:00
parent 922b80934c
commit 85f218b2b8
2 changed files with 9 additions and 9 deletions

View File

@ -18,7 +18,7 @@
<PersistentState>
<option name="values">
<map>
<entry key="url" value="jar:file:/C:/Program%20Files/Android/Android%20Studio1/plugins/android/lib/android.jar!/images/material/icons/materialicons/delete_outline/baseline_delete_outline_24.xml" />
<entry key="url" value="file:/$PROJECT_DIR$/../../../../../tools/android-sdk/icons/material/materialiconsoutlined/check/outline_check_24.xml" />
</map>
</option>
</PersistentState>
@ -29,7 +29,7 @@
<option name="values">
<map>
<entry key="color" value="ffffff" />
<entry key="outputName" value="ic_baseline_delete_outline_24" />
<entry key="outputName" value="ic_outline_check_24" />
<entry key="sourceFile" value="C:\Users\Tine" />
</map>
</option>

View File

@ -71,11 +71,11 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
appContext, title, persistedShowId, getCancelIntent(),
)
override suspend fun doWork(): Result {
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
reportProgress()
if (sourceUrl == null || targetFileUri == null || persistedShowId == -1) {
return failure()
return@withContext failure()
}
val request = Request.Builder().url(sourceUrl!!).build()
@ -83,7 +83,7 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
if (!response.isSuccessful || response.body() == null) {
Timber.w("server response not successful")
return failure()
return@withContext failure()
}
val body = response.body()!!
@ -91,7 +91,7 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
downloadFileInfoManager.openOutputStream(targetFileUri!!).use { outputSream ->
if (outputSream == null) {
Timber.w("fileoutputstream not readable")
return failure()
return@use failure()
}
body.byteStream().use { inputStream ->
@ -100,16 +100,16 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
}
} catch (e: CancellationException) {
// cancelled - no not show any notification
return Result.failure()
return@withContext Result.failure()
} catch (e: Exception) {
Timber.w(e)
return failure()
return@withContext failure()
}
progress = 100
reportProgress()
return success()
return@withContext success()
}
private fun success(): Result {