Downloads scheduler fixes

This commit is contained in:
Koitharu
2023-07-12 10:58:02 +03:00
parent b75220a1b7
commit 9610caf002
2 changed files with 24 additions and 13 deletions

View File

@@ -141,11 +141,13 @@ class DownloadsViewModel @Inject constructor(
fun remove(ids: Set<Long>) { fun remove(ids: Set<Long>) {
launchJob(Dispatchers.Default) { launchJob(Dispatchers.Default) {
val snapshot = works.value ?: return@launchJob val snapshot = works.value ?: return@launchJob
val uuids = HashSet<UUID>(ids.size)
for (work in snapshot) { for (work in snapshot) {
if (work.id.mostSignificantBits in ids) { if (work.id.mostSignificantBits in ids) {
workScheduler.delete(work.id) uuids.add(work.id)
} }
} }
workScheduler.delete(uuids)
onActionDone.call(ReversibleAction(R.string.downloads_removed, null)) onActionDone.call(ReversibleAction(R.string.downloads_removed, null))
} }
} }

View File

@@ -45,6 +45,7 @@ import org.koitharu.kotatsu.core.util.WorkManagerHelper
import org.koitharu.kotatsu.core.util.ext.deleteAwait import org.koitharu.kotatsu.core.util.ext.deleteAwait
import org.koitharu.kotatsu.core.util.ext.getDisplayMessage import org.koitharu.kotatsu.core.util.ext.getDisplayMessage
import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty import org.koitharu.kotatsu.core.util.ext.ifNullOrEmpty
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
import org.koitharu.kotatsu.core.util.ext.writeAllCancellable import org.koitharu.kotatsu.core.util.ext.writeAllCancellable
import org.koitharu.kotatsu.core.util.progress.TimeLeftEstimator import org.koitharu.kotatsu.core.util.progress.TimeLeftEstimator
import org.koitharu.kotatsu.download.domain.DownloadState import org.koitharu.kotatsu.download.domain.DownloadState
@@ -60,7 +61,6 @@ import org.koitharu.kotatsu.parsers.model.MangaSource
import org.koitharu.kotatsu.parsers.util.await import org.koitharu.kotatsu.parsers.util.await
import org.koitharu.kotatsu.parsers.util.mapToSet import org.koitharu.kotatsu.parsers.util.mapToSet
import org.koitharu.kotatsu.parsers.util.runCatchingCancellable import org.koitharu.kotatsu.parsers.util.runCatchingCancellable
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
import java.io.File import java.io.File
import java.util.UUID import java.util.UUID
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
@@ -322,9 +322,9 @@ class DownloadWorker @AssistedInject constructor(
manga: Manga, manga: Manga,
includedIds: LongArray?, includedIds: LongArray?,
): List<MangaChapter> { ): List<MangaChapter> {
val chapters = checkNotNull(manga.chapters?.toMutableList()) { val chapters = checkNotNull(manga.chapters) {
"Chapters list must not be null" "Chapters list must not be null"
} }.toMutableList()
if (includedIds != null) { if (includedIds != null) {
val chaptersIdsSet = includedIds.toMutableSet() val chaptersIdsSet = includedIds.toMutableSet()
chapters.retainAll { x -> chaptersIdsSet.remove(x.id) } chapters.retainAll { x -> chaptersIdsSet.remove(x.id) }
@@ -399,6 +399,13 @@ class DownloadWorker @AssistedInject constructor(
WorkManagerHelper(workManager).deleteWork(id) WorkManagerHelper(workManager).deleteWork(id)
} }
suspend fun delete(ids: Collection<UUID>) {
val wm = workManager
val helper = WorkManagerHelper(wm)
ids.forEach { id -> wm.cancelWorkById(id).await() }
helper.deleteWorks(ids)
}
suspend fun removeCompleted() { suspend fun removeCompleted() {
val helper = WorkManagerHelper(workManager) val helper = WorkManagerHelper(workManager)
val finishedWorks = helper.getFinishedWorkInfosByTag(TAG) val finishedWorks = helper.getFinishedWorkInfosByTag(TAG)
@@ -406,10 +413,7 @@ class DownloadWorker @AssistedInject constructor(
} }
suspend fun updateConstraints() { suspend fun updateConstraints() {
val constraints = Constraints.Builder() val constraints = createConstraints()
.setRequiresStorageNotLow(true)
.setRequiredNetworkType(if (settings.isDownloadsWiFiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
.build()
val helper = WorkManagerHelper(workManager) val helper = WorkManagerHelper(workManager)
val works = helper.getWorkInfosByTag(TAG) val works = helper.getWorkInfosByTag(TAG)
for (work in works) { for (work in works) {
@@ -418,6 +422,7 @@ class DownloadWorker @AssistedInject constructor(
} }
val request = OneTimeWorkRequestBuilder<DownloadWorker>() val request = OneTimeWorkRequestBuilder<DownloadWorker>()
.setConstraints(constraints) .setConstraints(constraints)
.addTag(TAG)
.setId(work.id) .setId(work.id)
.build() .build()
helper.updateWork(request) helper.updateWork(request)
@@ -425,15 +430,15 @@ class DownloadWorker @AssistedInject constructor(
} }
private suspend fun scheduleImpl(data: Collection<Data>) { private suspend fun scheduleImpl(data: Collection<Data>) {
val constraints = Constraints.Builder() if (data.isEmpty()) {
.setRequiresStorageNotLow(true) return
.setRequiredNetworkType(if (settings.isDownloadsWiFiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED) }
.build() val constraints = createConstraints()
val requests = data.map { inputData -> val requests = data.map { inputData ->
OneTimeWorkRequestBuilder<DownloadWorker>() OneTimeWorkRequestBuilder<DownloadWorker>()
.setConstraints(constraints) .setConstraints(constraints)
.addTag(TAG) .addTag(TAG)
.keepResultsForAtLeast(7, TimeUnit.DAYS) .keepResultsForAtLeast(30, TimeUnit.DAYS)
.setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS) .setBackoffCriteria(BackoffPolicy.LINEAR, 10, TimeUnit.SECONDS)
.setInputData(inputData) .setInputData(inputData)
.setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST) .setExpedited(OutOfQuotaPolicy.RUN_AS_NON_EXPEDITED_WORK_REQUEST)
@@ -441,6 +446,10 @@ class DownloadWorker @AssistedInject constructor(
} }
workManager.enqueue(requests).await() workManager.enqueue(requests).await()
} }
private fun createConstraints() = Constraints.Builder()
.setRequiredNetworkType(if (settings.isDownloadsWiFiOnly) NetworkType.UNMETERED else NetworkType.CONNECTED)
.build()
} }
private companion object { private companion object {