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.BookInfo
import io.legado.app.model.webbook.BookList import io.legado.app.model.webbook.BookList
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlin.coroutines.CoroutineContext
class WebBook(val bookSource: BookSource) { 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) fun searchBook(
: Coroutine<List<SearchBook>> { key: String,
return Coroutine.async(scope) { page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<SearchBook>> {
return Coroutine.async(scope, context) {
bookSource.searchUrl?.let { searchUrl -> bookSource.searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
ruleUrl = searchUrl, ruleUrl = searchUrl,
@ -40,9 +46,13 @@ class WebBook(val bookSource: BookSource) {
/** /**
* 发现 * 发现
*/ */
fun exploreBook(url: String, page: Int? = 1, scope: CoroutineScope = Coroutine.DEFAULT) fun exploreBook(
: Coroutine<List<SearchBook>> { url: String,
return Coroutine.async(scope) { page: Int? = 1,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<SearchBook>> {
return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
ruleUrl = url, ruleUrl = url,
page = page, page = page,
@ -57,8 +67,12 @@ class WebBook(val bookSource: BookSource) {
/** /**
* 书籍信息 * 书籍信息
*/ */
fun getBookInfo(book: Book, scope: CoroutineScope = Coroutine.DEFAULT): Coroutine<Book> { fun getBookInfo(
return Coroutine.async(scope) { book: Book,
scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<Book> {
return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
book = book, book = book,
ruleUrl = book.bookUrl, ruleUrl = book.bookUrl,
@ -76,9 +90,10 @@ class WebBook(val bookSource: BookSource) {
*/ */
fun getChapterList( fun getChapterList(
book: Book, book: Book,
scope: CoroutineScope = Coroutine.DEFAULT scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<List<BookChapter>> { ): Coroutine<List<BookChapter>> {
return Coroutine.async(scope) { return Coroutine.async(scope, context) {
val analyzeUrl = AnalyzeUrl( val analyzeUrl = AnalyzeUrl(
book = book, book = book,
ruleUrl = book.tocUrl, ruleUrl = book.tocUrl,
@ -97,9 +112,10 @@ class WebBook(val bookSource: BookSource) {
book: Book, book: Book,
bookChapter: BookChapter, bookChapter: BookChapter,
nextChapterUrl: String? = null, nextChapterUrl: String? = null,
scope: CoroutineScope = Coroutine.DEFAULT scope: CoroutineScope = Coroutine.DEFAULT,
context: CoroutineContext = Dispatchers.IO
): Coroutine<String> { ): Coroutine<String> {
return Coroutine.async(scope) { return Coroutine.async(scope, context) {
val analyzeUrl = val analyzeUrl =
AnalyzeUrl( AnalyzeUrl(
book = book, book = book,

@ -29,12 +29,17 @@ class SearchViewModel(application: Application) : BaseViewModel(application) {
searchKey = key searchKey = key
startTime = System.currentTimeMillis() startTime = System.currentTimeMillis()
start?.invoke() start?.invoke()
task = execute(context = searchPool) { task = execute {
//onCleared时自动取消 //onCleared时自动取消
val bookSourceList = App.db.bookSourceDao().allEnabled val bookSourceList = App.db.bookSourceDao().allEnabled
for (item in bookSourceList) { for (item in bookSourceList) {
//task取消时自动取消 by (scope = this@execute) //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) .timeout(30000L)
.onSuccess(Dispatchers.IO) { .onSuccess(Dispatchers.IO) {
it?.let { list -> it?.let { list ->

@ -54,12 +54,12 @@ class ChangeSourceViewModel(application: Application) : BaseViewModel(applicatio
} }
fun search() { fun search() {
task = execute(context = searchPool) { task = execute {
searchStateData.postValue(true) searchStateData.postValue(true)
val bookSourceList = App.db.bookSourceDao().allEnabled val bookSourceList = App.db.bookSourceDao().allEnabled
for (item in bookSourceList) { for (item in bookSourceList) {
//task取消时自动取消 by (scope = this@execute) //task取消时自动取消 by (scope = this@execute)
WebBook(item).searchBook(name, scope = this@execute) WebBook(item).searchBook(name, scope = this@execute, context = searchPool)
.timeout(30000L) .timeout(30000L)
.onSuccess(IO) { .onSuccess(IO) {
it?.forEach { searchBook -> it?.forEach { searchBook ->

Loading…
Cancel
Save