From c2645de9c63e97797849e57f4232df5fddb4a2ca Mon Sep 17 00:00:00 2001 From: kunfei Date: Thu, 20 Oct 2022 14:08:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/ui/book/cache/CacheActivity.kt | 41 ++++++++----------- .../app/ui/book/cache/CacheViewModel.kt | 3 +- 2 files changed, 18 insertions(+), 26 deletions(-) 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 320a078c8..b66432541 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 @@ -27,7 +27,6 @@ import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.document.HandleFileContract import io.legado.app.utils.* import io.legado.app.utils.viewbindingdelegate.viewBinding -import kotlinx.coroutines.Dispatchers.Default import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Job import kotlinx.coroutines.flow.* @@ -42,6 +41,7 @@ class CacheActivity : VMBaseActivity() private val exportBookPathKey = "exportBookPath" private val exportTypes = arrayListOf("txt", "epub") + private val layoutManager by lazy { LinearLayoutManager(this) } private val adapter by lazy { CacheAdapter(this, this) } private var booksFlowJob: Job? = null private var menu: Menu? = null @@ -147,7 +147,7 @@ class CacheActivity : VMBaseActivity() } private fun initRecyclerView() { - binding.recyclerView.layoutManager = LinearLayoutManager(this) + binding.recyclerView.layoutManager = layoutManager binding.recyclerView.adapter = adapter } @@ -192,18 +192,21 @@ class CacheActivity : VMBaseActivity() } } - override fun observeLiveBus() { - viewModel.upAdapterLiveData.observe(this) { - launch(Default) { - adapter.getItems().forEachIndexed { index, book -> - if (book.bookUrl == it) { - binding.recyclerView.post { - adapter.notifyItemChanged(index, true) - } - } + private fun notifyItemChanged(bookUrl: String) { + kotlin.runCatching { + adapter.getItems().forEachIndexed { index, book -> + if (bookUrl == book.bookUrl) { + adapter.notifyItemChanged(index, true) + return } } } + } + + override fun observeLiveBus() { + viewModel.upAdapterLiveData.observe(this) { + notifyItemChanged(it) + } observeEvent(EventBus.UP_DOWNLOAD) { if (!CacheBook.isRun) { menu?.findItem(R.id.menu_download)?.let { item -> @@ -218,23 +221,11 @@ class CacheActivity : VMBaseActivity() } menu?.applyTint(this) } - adapter.getItems().forEachIndexed { index, book -> - if (book.bookUrl == it) { - adapter.notifyItemChanged(index, true) - } - } + notifyItemChanged(it) } observeEvent>(EventBus.SAVE_CONTENT) { (book, chapter) -> viewModel.cacheChapters[book.bookUrl]?.add(chapter.url) - launch(Default) { - adapter.getItems().forEachIndexed { index, item -> - if (book.bookUrl == item.bookUrl) { - binding.recyclerView.post { - adapter.notifyItemChanged(index, true) - } - } - } - } + notifyItemChanged(book.bookUrl) } } diff --git a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt index f149471c9..ca2c7501b 100644 --- a/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/cache/CacheViewModel.kt @@ -21,6 +21,7 @@ import io.legado.app.exception.NoStackTraceException import io.legado.app.help.AppWebDav import io.legado.app.help.book.BookHelp import io.legado.app.help.book.ContentProcessor +import io.legado.app.help.book.isLocal import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.coroutine.OrderCoroutine @@ -58,7 +59,7 @@ class CacheViewModel(application: Application) : BaseViewModel(application) { loadChapterCoroutine?.cancel() loadChapterCoroutine = execute { books.forEach { book -> - if (!cacheChapters.contains(book.bookUrl)) { + if (!book.isLocal && !cacheChapters.contains(book.bookUrl)) { val chapterCaches = hashSetOf() val cacheNames = BookHelp.getChapterFiles(book) if (cacheNames.isNotEmpty()) {