diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index a09e9f326..9201fd542 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -11,6 +11,8 @@ import io.legado.app.model.webbook.BookContent import io.legado.app.model.webbook.BookInfo import io.legado.app.model.webbook.BookList import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlin.coroutines.CoroutineContext class WebBook(val bookSource: BookSource) { @@ -20,9 +22,13 @@ class WebBook(val bookSource: BookSource) { /** * 搜索 */ - fun searchBook(key: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT) - : Coroutine> { - return Coroutine.async(scope) { + fun searchBook( + key: String, + page: Int? = 1, + scope: CoroutineScope = Coroutine.DEFAULT, + context: CoroutineContext = Dispatchers.IO + ): Coroutine> { + return Coroutine.async(scope, context) { bookSource.searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl( ruleUrl = searchUrl, @@ -40,9 +46,13 @@ class WebBook(val bookSource: BookSource) { /** * 发现 */ - fun exploreBook(url: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT) - : Coroutine> { - return Coroutine.async(scope) { + fun exploreBook( + url: String, + page: Int? = 1, + scope: CoroutineScope = Coroutine.DEFAULT, + context: CoroutineContext = Dispatchers.IO + ): Coroutine> { + return Coroutine.async(scope, context) { val analyzeUrl = AnalyzeUrl( ruleUrl = url, page = page, @@ -57,8 +67,12 @@ class WebBook(val bookSource: BookSource) { /** * 书籍信息 */ - fun getBookInfo(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine { - return Coroutine.async(scope) { + fun getBookInfo( + book: Book, + scope: CoroutineScope = Coroutine.DEFAULT, + context: CoroutineContext = Dispatchers.IO + ): Coroutine { + return Coroutine.async(scope, context) { val analyzeUrl = AnalyzeUrl( book = book, ruleUrl = book.bookUrl, @@ -76,9 +90,10 @@ class WebBook(val bookSource: BookSource) { */ fun getChapterList( book: Book, - scope: CoroutineScope = Coroutine.DEFAULT + scope: CoroutineScope = Coroutine.DEFAULT, + context: CoroutineContext = Dispatchers.IO ): Coroutine> { - return Coroutine.async(scope) { + return Coroutine.async(scope, context) { val analyzeUrl = AnalyzeUrl( book = book, ruleUrl = book.tocUrl, @@ -97,9 +112,10 @@ class WebBook(val bookSource: BookSource) { book: Book, bookChapter: BookChapter, nextChapterUrl: String? = null, - scope: CoroutineScope = Coroutine.DEFAULT + scope: CoroutineScope = Coroutine.DEFAULT, + context: CoroutineContext = Dispatchers.IO ): Coroutine { - return Coroutine.async(scope) { + return Coroutine.async(scope, context) { val analyzeUrl = AnalyzeUrl( book = book, diff --git a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt index b754f91f1..a9e84bf0d 100644 --- a/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt @@ -29,12 +29,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application) { searchKey = key startTime = System.currentTimeMillis() start?.invoke() - task = execute(context = searchPool) { + task = execute { //onCleared时自动取消 val bookSourceList = App.db.bookSourceDao().allEnabled for (item in bookSourceList) { //task取消时自动取消 by (scope = this@execute) - WebBook(item).searchBook(key, searchPage, scope = this@execute) + WebBook(item).searchBook( + key, + searchPage, + scope = this@execute, + context = searchPool + ) .timeout(30000L) .onSuccess(Dispatchers.IO) { it?.let { list -> diff --git a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt index 265dcc227..9843ec994 100644 --- a/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.kt @@ -54,12 +54,12 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio } fun search() { - task = execute(context = searchPool) { + task = execute { searchStateData.postValue(true) val bookSourceList = App.db.bookSourceDao().allEnabled for (item in bookSourceList) { //task取消时自动取消 by (scope = this@execute) - WebBook(item).searchBook(name, scope = this@execute) + WebBook(item).searchBook(name, scope = this@execute, context = searchPool) .timeout(30000L) .onSuccess(IO) { it?.forEach { searchBook ->