From e233acc3afc67be2a592fe05b3e99895a0f1b3d8 Mon Sep 17 00:00:00 2001 From: 821938089 <821938089@qq.com> Date: Sun, 10 Jul 2022 08:35:23 +0800 Subject: [PATCH] fix block UI --- .../app/ui/book/read/ReadBookViewModel.kt | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index f1e58f667..59cf27d53 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -33,6 +33,8 @@ import io.legado.app.utils.postEvent import io.legado.app.utils.toStringArray import io.legado.app.utils.toastOnUi import kotlinx.coroutines.Dispatchers.IO +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** * 阅读界面数据处理 @@ -145,20 +147,24 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { } } else { ReadBook.bookSource?.let { - val preUpdateJs = it.ruleToc?.preUpdateJs - if (!preUpdateJs.isNullOrBlank()) { - AnalyzeRule(book, it).evalJS(preUpdateJs) - } - WebBook.getChapterList(viewModelScope, it, book) - .onSuccess(IO) { cList -> - appDb.bookChapterDao.insert(*cList.toTypedArray()) - appDb.bookDao.update(book) - ReadBook.chapterSize = cList.size - ReadBook.upMsg(null) - ReadBook.loadContent(resetPageOffset = true) - }.onError { - ReadBook.upMsg(context.getString(R.string.error_load_toc)) + viewModelScope.launch { + withContext(IO) { + val preUpdateJs = it.ruleToc?.preUpdateJs + if (!preUpdateJs.isNullOrBlank()) { + AnalyzeRule(book, it).evalJS(preUpdateJs) + } } + WebBook.getChapterList(viewModelScope, it, book) + .onSuccess(IO) { cList -> + appDb.bookChapterDao.insert(*cList.toTypedArray()) + appDb.bookDao.update(book) + ReadBook.chapterSize = cList.size + ReadBook.upMsg(null) + ReadBook.loadContent(resetPageOffset = true) + }.onError { + ReadBook.upMsg(context.getString(R.string.error_load_toc)) + } + } } } }