diff --git a/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt b/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt index dcc5dca75..fd37c76e7 100644 --- a/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt +++ b/app/src/main/java/io/legado/app/model/webBook/SearchModel.kt @@ -82,12 +82,13 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB source, searchKey, searchPage, - context = searchPool + context = searchPool, + executeContext = searchPool ).timeout(30000L) - .onSuccess(searchPool) { + .onSuccess { onSuccess(searchId, it) } - .onFinally(searchPool) { + .onFinally { onFinally(searchId) } tasks.add(task) diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 07e8eafe6..ecd9841b8 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -30,8 +30,9 @@ object WebBook { key: String, page: Int? = 1, context: CoroutineContext = Dispatchers.IO, + executeContext: CoroutineContext = Dispatchers.Main, ): Coroutine> { - return Coroutine.async(scope, context) { + return Coroutine.async(scope, context, executeContext = executeContext) { searchBookAwait(bookSource, key, page) } } diff --git a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt index 11a00d04e..58d493d4a 100644 --- a/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/changesource/ChangeBookSourceViewModel.kt @@ -192,7 +192,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a searchIndex++ } val source = bookSourceList[searchIndex] - val task = Coroutine.async(scope = viewModelScope, context = searchPool!!) { + val task = execute(context = searchPool!!, executeContext = searchPool!!) { val resultBooks = WebBook.searchBookAwait(source, name) resultBooks.forEach { searchBook -> if (searchBook.name != name) { @@ -336,8 +336,8 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a searchIndex++ } val searchBook = searchBookList[searchIndex] - val task = Coroutine.async(scope = viewModelScope, context = searchPool!!) { - val source = appDb.bookSourceDao.getBookSource(searchBook.origin) ?: return@async + val task = execute(context = searchPool!!, executeContext = searchPool!!) { + val source = appDb.bookSourceDao.getBookSource(searchBook.origin) ?: return@execute loadBookInfo(source, searchBook.toBook()) }.timeout(60000L) .onError { @@ -438,15 +438,13 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a suspend fun getToc(book: Book): Result, BookSource>> { return kotlin.runCatching { - withContext(IO) { - val source = appDb.bookSourceDao.getBookSource(book.origin) - ?: throw NoStackTraceException("书源不存在") - if (book.tocUrl.isEmpty()) { - WebBook.getBookInfoAwait(source, book) - } - val toc = WebBook.getChapterListAwait(source, book).getOrThrow() - Pair(toc, source) + val source = appDb.bookSourceDao.getBookSource(book.origin) + ?: throw NoStackTraceException("书源不存在") + if (book.tocUrl.isEmpty()) { + WebBook.getBookInfoAwait(source, book) } + val toc = WebBook.getChapterListAwait(source, book).getOrThrow() + Pair(toc, source) } }