This commit is contained in:
Horis
2025-11-22 19:56:28 +08:00
parent 0dc41f5018
commit 025f28cf85
3 changed files with 10 additions and 4 deletions
@@ -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"
@@ -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(
@@ -300,6 +300,9 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
notifyItemChanged(it)
}
observeEvent<String>(EventBus.UP_DOWNLOAD) {
notifyItemChanged(it)
}
observeEvent<String>(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<ActivityCacheBookBinding, CacheViewModel>()
}
menu?.applyTint(this)
}
notifyItemChanged(it)
}
observeEvent<Pair<Book, BookChapter>>(EventBus.SAVE_CONTENT) { (book, chapter) ->
viewModel.cacheChapters[book.bookUrl]?.add(chapter.url)