This commit is contained in:
Horis
2025-06-01 18:37:51 +08:00
parent 14d6c35067
commit 801d2bf52b
2 changed files with 10 additions and 8 deletions
@@ -11,6 +11,7 @@ import io.legado.app.data.entities.BookSource
import io.legado.app.exception.ConcurrentException import io.legado.app.exception.ConcurrentException
import io.legado.app.help.book.BookHelp import io.legado.app.help.book.BookHelp
import io.legado.app.help.book.isLocal import io.legado.app.help.book.isLocal
import io.legado.app.help.coroutine.CompositeCoroutine
import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.Coroutine
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.service.CacheBookService import io.legado.app.service.CacheBookService
@@ -96,15 +97,11 @@ object CacheBook {
} }
} }
fun clear() {
successDownloadSet.clear()
errorDownloadMap.clear()
}
fun close() { fun close() {
cacheBookMap.forEach { it.value.stop() } cacheBookMap.forEach { it.value.stop() }
cacheBookMap.clear() cacheBookMap.clear()
clear() successDownloadSet.clear()
errorDownloadMap.clear()
} }
val downloadSummary: String val downloadSummary: String
@@ -146,6 +143,7 @@ object CacheBook {
private val waitDownloadSet = linkedSetOf<Int>() private val waitDownloadSet = linkedSetOf<Int>()
private val onDownloadSet = linkedSetOf<Int>() private val onDownloadSet = linkedSetOf<Int>()
private val tasks = CompositeCoroutine()
private var isStopped = false private var isStopped = false
private var waitingRetry = false private var waitingRetry = false
@@ -158,7 +156,7 @@ object CacheBook {
@Synchronized @Synchronized
fun isRun(): Boolean { fun isRun(): Boolean {
return waitDownloadSet.size > 0 || onDownloadSet.size > 0 return waitDownloadSet.isNotEmpty() || onDownloadSet.isNotEmpty()
} }
@Synchronized @Synchronized
@@ -169,6 +167,7 @@ object CacheBook {
@Synchronized @Synchronized
fun stop() { fun stop() {
waitDownloadSet.clear() waitDownloadSet.clear()
tasks.clear()
isStopped = true isStopped = true
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl) postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
} }
@@ -285,6 +284,8 @@ object CacheBook {
onCancel(chapterIndex) onCancel(chapterIndex)
}.onFinally { }.onFinally {
onFinally() onFinally()
}.let {
tasks.add(it)
} }
return return
} }
@@ -309,6 +310,8 @@ object CacheBook {
onCancel(chapterIndex) onCancel(chapterIndex)
}.onFinally { }.onFinally {
onFinally() onFinally()
}.apply {
tasks.add(this)
}.start() }.start()
} }
@@ -65,7 +65,6 @@ class CacheBookService : BaseService() {
override fun onCreate() { override fun onCreate() {
super.onCreate() super.onCreate()
isRun = true isRun = true
CacheBook.clear()
lifecycleScope.launch { lifecycleScope.launch {
while (isActive) { while (isActive) {
delay(1000) delay(1000)