From 025f28cf85f9c310b768a7ca81b8c7a80c1ef6a3 Mon Sep 17 00:00:00 2001 From: Horis <8674809+821938089@users.noreply.github.com> Date: Sat, 22 Nov 2025 19:56:28 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20#5570?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/java/io/legado/app/constant/EventBus.kt | 1 + app/src/main/java/io/legado/app/model/CacheBook.kt | 9 ++++++--- .../java/io/legado/app/ui/book/cache/CacheActivity.kt | 4 +++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/legado/app/constant/EventBus.kt b/app/src/main/java/io/legado/app/constant/EventBus.kt index a04563743..ec0abd3fc 100644 --- a/app/src/main/java/io/legado/app/constant/EventBus.kt +++ b/app/src/main/java/io/legado/app/constant/EventBus.kt @@ -21,6 +21,7 @@ object EventBus { const val NOTIFY_MAIN = "notifyMain" const val WEB_SERVICE = "webService" const val UP_DOWNLOAD = "upDownload" + const val UP_DOWNLOAD_STATE = "upDownloadState" const val SAVE_CONTENT = "saveContent" const val CHECK_SOURCE = "checkSource" const val CHECK_SOURCE_DONE = "checkSourceDone" diff --git a/app/src/main/java/io/legado/app/model/CacheBook.kt b/app/src/main/java/io/legado/app/model/CacheBook.kt index fec488ff1..aa12fdf54 100644 --- a/app/src/main/java/io/legado/app/model/CacheBook.kt +++ b/app/src/main/java/io/legado/app/model/CacheBook.kt @@ -30,6 +30,8 @@ import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.collect import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.onCompletion +import kotlinx.coroutines.flow.onStart import kotlinx.coroutines.isActive import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Semaphore @@ -140,10 +142,14 @@ object CacheBook { delay(1000) } } + }.onStart { + postEvent(EventBus.UP_DOWNLOAD_STATE, "") }.onEachParallel(AppConfig.threadCount) { coroutineScope { it.download(this, context) } + }.onCompletion { + postEvent(EventBus.UP_DOWNLOAD_STATE, "") }.collect() } @@ -298,7 +304,6 @@ object CacheBook { */ @Synchronized fun download(scope: CoroutineScope, context: CoroutineContext) { - postEvent(EventBus.UP_DOWNLOAD, book.bookUrl) val chapterIndex = waitDownloadSet.firstOrNull() if (chapterIndex == null) { if (!isLoading && onDownloadSet.isEmpty()) { @@ -374,7 +379,6 @@ object CacheBook { } suspend fun downloadAwait(chapter: BookChapter): String { - postEvent(EventBus.UP_DOWNLOAD, book.bookUrl) synchronized(this) { onDownloadSet.add(chapter.index) waitDownloadSet.remove(chapter.index) @@ -408,7 +412,6 @@ object CacheBook { if (onDownloadSet.contains(chapter.index)) { return } - postEvent(EventBus.UP_DOWNLOAD, book.bookUrl) onDownloadSet.add(chapter.index) waitDownloadSet.remove(chapter.index) WebBook.getContent( diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt index c83be1357..409312120 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheActivity.kt @@ -300,6 +300,9 @@ class CacheActivity : VMBaseActivity() notifyItemChanged(it) } observeEvent(EventBus.UP_DOWNLOAD) { + notifyItemChanged(it) + } + observeEvent(EventBus.UP_DOWNLOAD_STATE) { if (!CacheBook.isRun) { menu?.findItem(R.id.menu_download)?.let { item -> item.setIconCompat(R.drawable.ic_play_24dp) @@ -313,7 +316,6 @@ class CacheActivity : VMBaseActivity() } menu?.applyTint(this) } - notifyItemChanged(it) } observeEvent>(EventBus.SAVE_CONTENT) { (book, chapter) -> viewModel.cacheChapters[book.bookUrl]?.add(chapter.url)