This commit is contained in:
Horis
2023-07-24 22:49:14 +08:00
parent 61f929777e
commit 77ec679448
3 changed files with 15 additions and 15 deletions
@@ -82,12 +82,13 @@ class SearchModel(private val scope: CoroutineScope, private val callBack: CallB
source, source,
searchKey, searchKey,
searchPage, searchPage,
context = searchPool context = searchPool,
executeContext = searchPool
).timeout(30000L) ).timeout(30000L)
.onSuccess(searchPool) { .onSuccess {
onSuccess(searchId, it) onSuccess(searchId, it)
} }
.onFinally(searchPool) { .onFinally {
onFinally(searchId) onFinally(searchId)
} }
tasks.add(task) tasks.add(task)
@@ -30,8 +30,9 @@ object WebBook {
key: String, key: String,
page: Int? = 1, page: Int? = 1,
context: CoroutineContext = Dispatchers.IO, context: CoroutineContext = Dispatchers.IO,
executeContext: CoroutineContext = Dispatchers.Main,
): Coroutine<ArrayList<SearchBook>> { ): Coroutine<ArrayList<SearchBook>> {
return Coroutine.async(scope, context) { return Coroutine.async(scope, context, executeContext = executeContext) {
searchBookAwait(bookSource, key, page) searchBookAwait(bookSource, key, page)
} }
} }
@@ -192,7 +192,7 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
searchIndex++ searchIndex++
} }
val source = bookSourceList[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) val resultBooks = WebBook.searchBookAwait(source, name)
resultBooks.forEach { searchBook -> resultBooks.forEach { searchBook ->
if (searchBook.name != name) { if (searchBook.name != name) {
@@ -336,8 +336,8 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
searchIndex++ searchIndex++
} }
val searchBook = searchBookList[searchIndex] val searchBook = searchBookList[searchIndex]
val task = Coroutine.async(scope = viewModelScope, context = searchPool!!) { val task = execute(context = searchPool!!, executeContext = searchPool!!) {
val source = appDb.bookSourceDao.getBookSource(searchBook.origin) ?: return@async val source = appDb.bookSourceDao.getBookSource(searchBook.origin) ?: return@execute
loadBookInfo(source, searchBook.toBook()) loadBookInfo(source, searchBook.toBook())
}.timeout(60000L) }.timeout(60000L)
.onError { .onError {
@@ -438,15 +438,13 @@ open class ChangeBookSourceViewModel(application: Application) : BaseViewModel(a
suspend fun getToc(book: Book): Result<Pair<List<BookChapter>, BookSource>> { suspend fun getToc(book: Book): Result<Pair<List<BookChapter>, BookSource>> {
return kotlin.runCatching { return kotlin.runCatching {
withContext(IO) { val source = appDb.bookSourceDao.getBookSource(book.origin)
val source = appDb.bookSourceDao.getBookSource(book.origin) ?: throw NoStackTraceException("书源不存在")
?: throw NoStackTraceException("书源不存在") if (book.tocUrl.isEmpty()) {
if (book.tocUrl.isEmpty()) { WebBook.getBookInfoAwait(source, book)
WebBook.getBookInfoAwait(source, book)
}
val toc = WebBook.getChapterListAwait(source, book).getOrThrow()
Pair(toc, source)
} }
val toc = WebBook.getChapterListAwait(source, book).getOrThrow()
Pair(toc, source)
} }
} }