pull/36/head
kunfei 5 years ago
parent 423cff9d95
commit 3e808b12dc
  1. 40
      app/src/main/java/io/legado/app/model/WebBook.kt
  2. 9
      app/src/main/java/io/legado/app/ui/book/search/SearchViewModel.kt
  3. 4
      app/src/main/java/io/legado/app/ui/changesource/ChangeSourceViewModel.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<List<SearchBook>> {
return Coroutine.async(scope) {
fun searchBook(
key: String,
page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<SearchBook>> {
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<List<SearchBook>> {
return Coroutine.async(scope) {
fun exploreBook(
url: String,
page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<SearchBook>> {
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<Book> {
return Coroutine.async(scope) {
fun getBookInfo(
book: Book,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<Book> {
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<List<BookChapter>> {
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<String> {
return Coroutine.async(scope) {
return Coroutine.async(scope, context) {
val analyzeUrl =
AnalyzeUrl(
book = book,

@ -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 ->

@ -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 ->

Loading…
Cancel
Save