Skip to content

Commit

Permalink
fix(MediaDownloadsService): exceptions processing logic
Browse files Browse the repository at this point in the history
  • Loading branch information
urFate committed Oct 13, 2024
1 parent bbfc645 commit 1a1f444
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,12 @@ class DownloadsServiceHelper(

while (queryListIterator.hasNext()) {
val downloadQuery = queryListIterator.next()
var exception: Exception? = null

val queryIterator = downloadQuery.query.listIterator()

while (queryIterator.hasNext()) {
val index = queryIterator.nextIndex()
val enqueuedTask = queryIterator.next()
val exceptions: MutableMap<EnqueuedTask, Exception> = mutableMapOf()

if (enqueuedTask.stopState.value) continue

Expand All @@ -97,22 +96,22 @@ class DownloadsServiceHelper(
StreamProtocol.MPEG -> download(enqueuedTask)
StreamProtocol.HLS -> downloadHLS(enqueuedTask)
}

} catch (ex: Exception) {
ex.printStackTrace()
exception = ex
exceptions[enqueuedTask] = ex
} finally {
listeners.forEach { it.onTaskFinish(enqueuedTask, exception) }
listeners.forEach {
it.onTaskFinish(
enqueuedTask,
exceptions.getOrDefault(enqueuedTask, null)
)
}
enqueuedTask.stopState.emit(true)
}
}

listeners.forEach {
it.onQueryFinish(
downloadQuery = downloadQuery,
exception = exception
)

it.onQueryFinish(downloadQuery = downloadQuery)
downloadQuery.query.clear()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class MediaDownloadsService : Service() {
private val db = AppDatabase.getAppDataBase(service)!!
private val job = SupervisorJob()
private val scope = CoroutineScope(Dispatchers.IO + job)
private var exception: Exception? = null

private val notificationId = 1

Expand Down Expand Up @@ -138,18 +139,21 @@ class MediaDownloadsService : Service() {
}
}
}

this.exception = if (exception !is DownloadsServiceHelper.ForcedInterruptionException) {
exception
} else this.exception
}

override fun onQueryFinish(
downloadQuery: DownloadsServiceHelper.DownloadQuery,
exception: Exception?
downloadQuery: DownloadsServiceHelper.DownloadQuery
) {
val finishNotificationId = System.currentTimeMillis().div(1000).toInt()

baseBuilder
.setProgress(0, 0, false)
.apply {
if (exception == null || exception is DownloadsServiceHelper.ForcedInterruptionException) {
if (exception == null) {
setContentText(service.getString(R.string.episodes_downloading_finished))
} else {
setContentText(service.getString(R.string.episodes_downloading_failed))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import org.shirabox.app.service.media.DownloadsServiceHelper
interface DownloadsListener {
fun onCurrentTaskChanged(task: EnqueuedTask, position: Int, querySize: Int)
fun onTaskFinish(task: EnqueuedTask, exception: Exception?)
fun onQueryFinish(downloadQuery: DownloadsServiceHelper.DownloadQuery, exception: Exception?)
fun onQueryFinish(downloadQuery: DownloadsServiceHelper.DownloadQuery)
fun onLifecycleEnd()
}

0 comments on commit 1a1f444

Please sign in to comment.