Horis hace 3 años
padre
commit
1897db7d0d

+ 4 - 3
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)

+ 2 - 1
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<ArrayList<SearchBook>> {
-        return Coroutine.async(scope, context) {
+        return Coroutine.async(scope, context, executeContext = executeContext) {
             searchBookAwait(bookSource, key, page)
         }
     }

+ 9 - 11
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<Pair<List<BookChapter>, 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)
         }
     }