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)