This commit is contained in:
kunfei
2022-07-27 22:11:25 +08:00
parent 04eaa29052
commit 13d0adc1b1
3 changed files with 13 additions and 12 deletions
@@ -143,6 +143,10 @@ object CacheBook {
val successCount get() = successDownloadSet.size
val errorCount get() = errorDownloadMap.size
init {
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
}
@Synchronized
fun isRun(): Boolean {
return waitDownloadSet.size > 0 || onDownloadSet.size > 0
@@ -214,9 +218,9 @@ object CacheBook {
@Synchronized
private fun onFinally() {
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) {
postEvent(EventBus.UP_DOWNLOAD, "")
cacheBookMap.remove(book.bookUrl)
}
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
}
/**
@@ -224,6 +228,7 @@ object CacheBook {
*/
@Synchronized
fun download(scope: CoroutineScope, context: CoroutineContext) {
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
val chapterIndex = waitDownloadSet.firstOrNull()
if (chapterIndex == null) {
if (onDownloadSet.isEmpty()) {
@@ -276,6 +281,7 @@ object CacheBook {
if (onDownloadSet.contains(chapter.index)) {
return
}
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
onDownloadSet.add(chapter.index)
waitDownloadSet.remove(chapter.index)
WebBook.getContent(scope, bookSource, book, chapter)
@@ -288,9 +294,7 @@ object CacheBook {
}.onCancel {
onCancel(chapter.index)
}.onFinally {
if (waitDownloadSet.isEmpty() && onDownloadSet.isEmpty()) {
postEvent(EventBus.UP_DOWNLOAD, "")
}
postEvent(EventBus.UP_DOWNLOAD, book.bookUrl)
}
}
@@ -232,12 +232,15 @@ class CacheActivity : VMBaseActivity<ActivityCacheBookBinding, CacheViewModel>()
}
menu?.applyTint(this)
}
adapter.notifyItemRangeChanged(0, adapter.itemCount, true)
adapter.getItems().forEachIndexed { index, book ->
if (book.bookUrl == it) {
adapter.notifyItemChanged(index, true)
}
}
}
observeEvent<BookChapter>(EventBus.SAVE_CONTENT) {
adapter.cacheChapters[it.bookUrl]?.add(it.url)
}
postEventDelay(EventBus.UP_DOWNLOAD, "", 100)
}
override fun export(position: Int) {
@@ -186,12 +186,6 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
private fun cacheBook() {
cacheBookJob?.cancel()
cacheBookJob = viewModelScope.launch(upTocPool) {
launch {
while (isActive) {
postEvent(EventBus.UP_DOWNLOAD, "")
delay(1000)
}
}
while (isActive) {
if (CacheBookService.isRun || !CacheBook.isRun) {
cacheBookJob?.cancel()