|
|
|
@ -38,6 +38,7 @@ import okio.buffer
|
|
|
|
import okio.sink
|
|
|
|
import okio.sink
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.R
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions
|
|
|
|
import org.koitharu.kotatsu.core.exceptions.TooManyRequestExceptions
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.model.ids
|
|
|
|
import org.koitharu.kotatsu.core.network.CommonHeaders
|
|
|
|
import org.koitharu.kotatsu.core.network.CommonHeaders
|
|
|
|
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
|
|
|
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
|
|
|
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
|
|
|
import org.koitharu.kotatsu.core.parser.MangaDataRepository
|
|
|
|
@ -46,7 +47,6 @@ import org.koitharu.kotatsu.core.prefs.AppSettings
|
|
|
|
import org.koitharu.kotatsu.core.util.Throttler
|
|
|
|
import org.koitharu.kotatsu.core.util.Throttler
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitFinishedWorkInfosByTag
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitFinishedWorkInfosByTag
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitUpdateWork
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitUpdateWork
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitWorkInfoById
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitWorkInfosByTag
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.awaitWorkInfosByTag
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.deleteAwait
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.deleteAwait
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.deleteWork
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.deleteWork
|
|
|
|
@ -106,10 +106,10 @@ class DownloadWorker @AssistedInject constructor(
|
|
|
|
val mangaId = inputData.getLong(MANGA_ID, 0L)
|
|
|
|
val mangaId = inputData.getLong(MANGA_ID, 0L)
|
|
|
|
val manga = mangaDataRepository.findMangaById(mangaId) ?: return Result.failure()
|
|
|
|
val manga = mangaDataRepository.findMangaById(mangaId) ?: return Result.failure()
|
|
|
|
val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() }
|
|
|
|
val chaptersIds = inputData.getLongArray(CHAPTERS_IDS)?.takeUnless { it.isEmpty() }
|
|
|
|
val downloadedIds = getDoneChapters()
|
|
|
|
val downloadedIds = getDoneChapters(manga)
|
|
|
|
lastPublishedState = DownloadState(manga, isIndeterminate = true)
|
|
|
|
publishState(DownloadState(manga, isIndeterminate = true))
|
|
|
|
return try {
|
|
|
|
return try {
|
|
|
|
downloadMangaImpl(chaptersIds, downloadedIds)
|
|
|
|
downloadMangaImpl(manga, chaptersIds, downloadedIds)
|
|
|
|
Result.success(currentState.toWorkData())
|
|
|
|
Result.success(currentState.toWorkData())
|
|
|
|
} catch (e: CancellationException) {
|
|
|
|
} catch (e: CancellationException) {
|
|
|
|
withContext(NonCancellable) {
|
|
|
|
withContext(NonCancellable) {
|
|
|
|
@ -147,10 +147,11 @@ class DownloadWorker @AssistedInject constructor(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun downloadMangaImpl(
|
|
|
|
private suspend fun downloadMangaImpl(
|
|
|
|
|
|
|
|
subject: Manga,
|
|
|
|
includedIds: LongArray?,
|
|
|
|
includedIds: LongArray?,
|
|
|
|
excludedIds: LongArray,
|
|
|
|
excludedIds: Set<Long>,
|
|
|
|
) {
|
|
|
|
) {
|
|
|
|
var manga = currentState.manga
|
|
|
|
var manga = subject
|
|
|
|
val chaptersToSkip = excludedIds.toMutableSet()
|
|
|
|
val chaptersToSkip = excludedIds.toMutableSet()
|
|
|
|
withMangaLock(manga) {
|
|
|
|
withMangaLock(manga) {
|
|
|
|
ContextCompat.registerReceiver(
|
|
|
|
ContextCompat.registerReceiver(
|
|
|
|
@ -178,16 +179,9 @@ class DownloadWorker @AssistedInject constructor(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val chapters = getChapters(mangaDetails, includedIds)
|
|
|
|
val chapters = getChapters(mangaDetails, includedIds)
|
|
|
|
publishState(
|
|
|
|
|
|
|
|
currentState.copy(scheduledChapters = LongArray(chapters.size) { i -> chapters[i].id }),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
|
|
|
for ((chapterIndex, chapter) in chapters.withIndex()) {
|
|
|
|
if (chaptersToSkip.remove(chapter.id)) {
|
|
|
|
if (chaptersToSkip.remove(chapter.id)) {
|
|
|
|
publishState(
|
|
|
|
publishState(currentState.copy(downloadedChapters = currentState.downloadedChapters + 1))
|
|
|
|
currentState.copy(
|
|
|
|
|
|
|
|
downloadedChapters = currentState.downloadedChapters + chapter.id,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
val pages = runFailsafe(pausingHandle) {
|
|
|
|
val pages = runFailsafe(pausingHandle) {
|
|
|
|
@ -225,11 +219,7 @@ class DownloadWorker @AssistedInject constructor(
|
|
|
|
localStorageChanges.emit(LocalMangaInput.of(output.rootFile).getManga())
|
|
|
|
localStorageChanges.emit(LocalMangaInput.of(output.rootFile).getManga())
|
|
|
|
}.onFailure(Throwable::printStackTraceDebug)
|
|
|
|
}.onFailure(Throwable::printStackTraceDebug)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
publishState(
|
|
|
|
publishState(currentState.copy(downloadedChapters = currentState.downloadedChapters + 1))
|
|
|
|
currentState.copy(
|
|
|
|
|
|
|
|
downloadedChapters = currentState.downloadedChapters + chapter.id,
|
|
|
|
|
|
|
|
),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
publishState(currentState.copy(isIndeterminate = true, eta = -1L))
|
|
|
|
publishState(currentState.copy(isIndeterminate = true, eta = -1L))
|
|
|
|
output.mergeWithExisting()
|
|
|
|
output.mergeWithExisting()
|
|
|
|
@ -336,11 +326,9 @@ class DownloadWorker @AssistedInject constructor(
|
|
|
|
setProgress(state.toWorkData())
|
|
|
|
setProgress(state.toWorkData())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private suspend fun getDoneChapters(): LongArray {
|
|
|
|
private suspend fun getDoneChapters(manga: Manga) = runCatchingCancellable {
|
|
|
|
val work = WorkManager.getInstance(applicationContext).awaitWorkInfoById(id)
|
|
|
|
localMangaRepository.getDetails(manga).chapters?.ids()
|
|
|
|
?: return LongArray(0)
|
|
|
|
}.getOrNull().orEmpty()
|
|
|
|
return DownloadState.getDownloadedChapters(work.progress)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private fun getChapters(
|
|
|
|
private fun getChapters(
|
|
|
|
manga: Manga,
|
|
|
|
manga: Manga,
|
|
|
|
|