diff --git a/app/src/main/java/io/legado/app/base/BaseViewModel.kt b/app/src/main/java/io/legado/app/base/BaseViewModel.kt index 48394680c..f915480d0 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -13,11 +13,11 @@ open class BaseViewModel(application: Application) : AndroidViewModel(applicatio AnkoLogger { fun execute(block: suspend CoroutineScope.() -> T): Coroutine { - return Coroutine.with(this) { block() } + return Coroutine.launch(this) { block() } } fun submit(block: suspend CoroutineScope.() -> Deferred): Coroutine { - return Coroutine.with(this) { block().await() } + return Coroutine.launch(this) { block().await() } } diff --git a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt index d6aec0d0c..f337b30f7 100644 --- a/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt +++ b/app/src/main/java/io/legado/app/help/coroutine/Coroutine.kt @@ -2,11 +2,13 @@ package io.legado.app.help.coroutine import kotlinx.coroutines.* -class Coroutine(scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) { +class Coroutine(private val scope: CoroutineScope, private val block: suspend CoroutineScope.() -> T) { companion object { - fun with(scope: CoroutineScope, block: suspend CoroutineScope.() -> T): Coroutine { + private val DEFAULT = MainScope() + + fun launch(scope: CoroutineScope = DEFAULT, block: suspend CoroutineScope.() -> T): Coroutine { return Coroutine(scope, block) } } @@ -56,6 +58,10 @@ class Coroutine(scope: CoroutineScope, private val block: suspend CoroutineSc return this@Coroutine } + fun cancel(cause: CancellationException? = null) { + scope.cancel(cause) + } + private suspend fun executeInternal(block: suspend CoroutineScope.() -> T) { tryCatch( { 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 b6f4b90dc..1c5bae931 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -8,13 +8,11 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.HttpHelper import io.legado.app.model.analyzeRule.AnalyzeUrl 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> { - return Coroutine.with(this) { + return Coroutine.launch { bookSource.getSearchRule().searchUrl?.let { searchUrl -> val analyzeUrl = AnalyzeUrl(searchUrl) val response = when { @@ -31,9 +29,9 @@ class WebBook(private val bookSource: BookSource) : CoroutineScope by MainScope( else -> HttpHelper.getApiService(analyzeUrl.baseUrl) .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() + return@launch mutableListOf() } } diff --git a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt index 9ec26f599..fd4f601ef 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt @@ -1,5 +1,5 @@ package io.legado.app.model.webbook -class BookChapterList { +object BookChapterList { } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt index 76473a236..8a037108b 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt @@ -1,5 +1,5 @@ package io.legado.app.model.webbook -class BookContent { +object BookContent { } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt b/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt index f08400e74..9bc05a2e0 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookInfo.kt @@ -1,5 +1,5 @@ package io.legado.app.model.webbook -class BookInfo { +object BookInfo { } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/webbook/BookList.kt b/app/src/main/java/io/legado/app/model/webbook/BookList.kt index 14fd3001f..72ed43060 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookList.kt @@ -11,7 +11,7 @@ import io.legado.app.utils.NetworkUtils import org.mozilla.javascript.NativeObject import retrofit2.Response -class BookList { +object BookList { @Throws(Exception::class) fun analyzeBookList(