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

Avoid showing old notifications

This commit is contained in:
Christine Coenen 2022-10-27 18:46:00 +02:00
parent 163dadf258
commit 3f6bc37968
2 changed files with 42 additions and 18 deletions

View File

@ -6,8 +6,6 @@ import androidx.core.app.NotificationManagerCompat
import androidx.lifecycle.asFlow
import androidx.work.*
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.exceptions.DownloadException
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.revisited.DownloadCompletedEventNotification
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.revisited.DownloadFailedEventNotification
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.revisited.DownloadQueuedEventNotification
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.revisited.DownloadWorker
import de.christinecoenen.code.zapp.app.settings.repository.SettingsRepository
@ -208,26 +206,25 @@ class WorkManagerDownloadController(
val notificationTitle = show.mediathekShow.title
val notification = when (show.downloadStatus) {
DownloadStatus.COMPLETED -> DownloadCompletedEventNotification(
applicationContext,
notificationTitle
)
DownloadStatus.QUEUED -> DownloadQueuedEventNotification(
applicationContext,
notificationTitle
)
DownloadStatus.FAILED -> DownloadFailedEventNotification(
applicationContext,
notificationTitle
)
DownloadStatus.CANCELLED -> {
notificationManager.cancel(show.downloadId)
null
}
else -> null
DownloadStatus.FAILED,
DownloadStatus.COMPLETED -> {
// will be handled in worker
null
}
else -> {
// no notification needed
null
}
}
// TODO: this will let notifications reappear if previously dismissed by the user
notification?.let {
notificationManager.notify(show.downloadId, it.build())
}

View File

@ -2,10 +2,10 @@ package de.christinecoenen.code.zapp.app.mediathek.controller.downloads.revisite
import android.app.PendingIntent
import android.content.Context
import androidx.core.app.NotificationManagerCompat
import androidx.work.*
import de.christinecoenen.code.zapp.app.mediathek.controller.downloads.DownloadFileInfoManager
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.coroutines.*
import okhttp3.OkHttpClient
import okhttp3.Request
import org.koin.core.component.KoinComponent
@ -13,6 +13,7 @@ import org.koin.core.component.inject
import timber.log.Timber
import java.io.InputStream
import java.io.OutputStream
import kotlin.time.Duration.Companion.milliseconds
class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
CoroutineWorker(appContext, workerParams), KoinComponent {
@ -23,6 +24,7 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
private const val TargetFileUriKey = "TargetFileUri"
private const val TitleKey = "Title"
private const val BufferSize = DEFAULT_BUFFER_SIZE
private val NotificationDelay = 100.milliseconds
fun constructInputData(sourceUrl: String, targetFileUri: String, title: String) =
workDataOf(
@ -40,9 +42,12 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
private val httpClient: OkHttpClient by inject()
private val downloadFileInfoManager: DownloadFileInfoManager by inject()
private val notificationManager = NotificationManagerCompat.from(applicationContext)
private val sourceUrl by lazy { inputData.getString(SourceUrlKey) }
private val targetFileUri by lazy { inputData.getString(TargetFileUriKey) }
private val title by lazy { inputData.getString(TitleKey) ?: "" }
private val notificationId by lazy { id.hashCode() }
private var progress = 0
@ -54,7 +59,7 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
reportProgress()
if (sourceUrl == null || targetFileUri == null) {
return Result.failure()
return failure()
}
val request = Request.Builder().url(sourceUrl!!).build()
@ -62,7 +67,7 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
if (!response.isSuccessful || response.body() == null) {
Timber.w("server response not successful")
return Result.failure()
return failure()
}
val body = response.body()!!
@ -70,7 +75,7 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
downloadFileInfoManager.openOutputStream(targetFileUri!!).use { outputSream ->
if (outputSream == null) {
Timber.w("fileoutputstream not readable")
return Result.failure()
return failure()
}
body.byteStream().use { inputStream ->
@ -79,15 +84,37 @@ class DownloadWorker(appContext: Context, workerParams: WorkerParameters) :
}
} catch (e: Exception) {
Timber.w(e)
return Result.failure()
return failure()
}
progress = 100
reportProgress()
return success()
}
private fun success(): Result {
MainScope().launch {
delay(NotificationDelay)
val notification = DownloadCompletedEventNotification(applicationContext, title)
notificationManager.notify(notificationId, notification.build())
}
return Result.success()
}
private fun failure(): Result {
MainScope().launch {
delay(NotificationDelay)
val notification = DownloadFailedEventNotification(applicationContext, title)
notificationManager.notify(notificationId, notification.build())
}
return Result.failure()
}
private suspend fun download(
inputStream: InputStream,
outputStream: OutputStream,