pull/32/head
Administrator 5 years ago
parent 8b0e86726d
commit 587540f73d
  1. 4
      app/src/main/java/io/legado/app/base/BaseViewModel.kt
  2. 10
      app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt
  3. 10
      app/src/main/java/io/legado/app/model/WebBook.kt
  4. 2
      app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt
  5. 2
      app/src/main/java/io/legado/app/model/webbook/BookContent.kt
  6. 2
      app/src/main/java/io/legado/app/model/webbook/BookInfo.kt
  7. 2
      app/src/main/java/io/legado/app/model/webbook/BookList.kt

@ -13,11 +13,11 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio
AnkoLogger { AnkoLogger {
fun <T> execute(block: suspend CoroutineScope.() -> T): Coroutine<T> { fun <T> execute(block: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine.with(this) { block() } return Coroutine.launch(this) { block() }
} }
fun <T> submit(block: suspend CoroutineScope.() -> Deferred<T>): Coroutine<T> { fun <T> submit(block: suspend CoroutineScope.() -> Deferred<T>): Coroutine<T> {
return Coroutine.with(this) { block().await() } return Coroutine.launch(this) { block().await() }
} }

@ -2,11 +2,13 @@ package io.legado.app.help.coroutine
import kotlinx.coroutines.* import kotlinx.coroutines.*
class Coroutine<T>(scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) { class Coroutine<T>(private val scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) {
companion object { companion object {
fun <T> with(scope: CoroutineScope, block: suspend CoroutineScope.() -> T): Coroutine<T> { private val DEFAULT = MainScope()
fun <T> launch(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine<T> {
return Coroutine(scope, block) return Coroutine(scope, block)
} }
} }
@ -56,6 +58,10 @@ class Coroutine<T>(scope: CoroutineScope, private val block: suspend CoroutineSc
return this@Coroutine return this@Coroutine
} }
fun cancel(cause: CancellationException? = null) {
scope.cancel(cause)
}
private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) {
tryCatch( tryCatch(
{ {

@ -8,13 +8,11 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.webbook.BookList import io.legado.app.model.webbook.BookList
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.MainScope
class WebBook(private val bookSource: BookSource) : CoroutineScope by MainScope() { class WebBook(private val bookSource: BookSource) {
fun searchBook(key: String, page: Int?): Coroutine<List<SearchBook>> { fun searchBook(key: String, page: Int?): Coroutine<List<SearchBook>> {
return Coroutine.with(this) { return Coroutine.launch {
bookSource.getSearchRule().searchUrl?.let { searchUrl -> bookSource.getSearchRule().searchUrl?.let { searchUrl ->
val analyzeUrl = AnalyzeUrl(searchUrl) val analyzeUrl = AnalyzeUrl(searchUrl)
val response = when { val response = when {
@ -31,9 +29,9 @@ class WebBook(private val bookSource: BookSource) : CoroutineScope by MainScope(
else -> HttpHelper.getApiService<IHttpGetApi>(analyzeUrl.baseUrl) else -> HttpHelper.getApiService<IHttpGetApi>(analyzeUrl.baseUrl)
.getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await() .getMap(analyzeUrl.url, analyzeUrl.fieldMap, analyzeUrl.headerMap).await()
} }
return@with BookList().analyzeBookList(response, bookSource, analyzeUrl) return@launch BookList.analyzeBookList(response, bookSource, analyzeUrl)
} }
return@with ArrayList<SearchBook>() return@launch mutableListOf()
} }
} }

@ -1,5 +1,5 @@
package io.legado.app.model.webbook package io.legado.app.model.webbook
class BookChapterList { object BookChapterList {
} }

@ -1,5 +1,5 @@
package io.legado.app.model.webbook package io.legado.app.model.webbook
class BookContent { object BookContent {
} }

@ -1,5 +1,5 @@
package io.legado.app.model.webbook package io.legado.app.model.webbook
class BookInfo { object BookInfo {
} }

@ -11,7 +11,7 @@ import io.legado.app.utils.NetworkUtils
import org.mozilla.javascript.NativeObject import org.mozilla.javascript.NativeObject
import retrofit2.Response import retrofit2.Response
class BookList { object BookList {
@Throws(Exception::class) @Throws(Exception::class)
fun analyzeBookList( fun analyzeBookList(

Loading…
Cancel
Save