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 a54dce392..48394680c 100644 --- a/app/src/main/java/io/legado/app/base/BaseViewModel.kt +++ b/app/src/main/java/io/legado/app/base/BaseViewModel.kt @@ -4,6 +4,7 @@ import android.app.Application import androidx.lifecycle.AndroidViewModel import io.legado.app.help.coroutine.Coroutine import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Deferred import kotlinx.coroutines.MainScope import kotlinx.coroutines.cancel import org.jetbrains.anko.AnkoLogger @@ -11,10 +12,15 @@ import org.jetbrains.anko.AnkoLogger open class BaseViewModel(application: Application) : AndroidViewModel(application), CoroutineScope by MainScope(), AnkoLogger { - fun execute(domain: suspend CoroutineScope.() -> T): Coroutine { - return Coroutine.with(this) { domain() } + fun execute(block: suspend CoroutineScope.() -> T): Coroutine { + return Coroutine.with(this) { block() } } + fun submit(block: suspend CoroutineScope.() -> Deferred): Coroutine { + return Coroutine.with(this) { block().await() } + } + + override fun onCleared() { super.onCleared() cancel() diff --git a/app/src/main/java/io/legado/app/help/permission/Request.kt b/app/src/main/java/io/legado/app/help/permission/Request.kt index ccd96741f..c13a84e82 100644 --- a/app/src/main/java/io/legado/app/help/permission/Request.kt +++ b/app/src/main/java/io/legado/app/help/permission/Request.kt @@ -127,19 +127,21 @@ internal class Request : OnRequestPermissionsResultCallback { private fun showSettingDialog(rationale: CharSequence, cancel: () -> Unit) { rationaleDialog?.dismiss() source?.context?.let { - rationaleDialog = AlertDialog.Builder(it) - .setTitle(R.string.dialog_title) - .setMessage(rationale) - .setPositiveButton(R.string.dialog_setting) { _, _ -> - it.startActivity( - Pair( - PermissionActivity.KEY_INPUT_REQUEST_TYPE, - TYPE_REQUEST_SETTING + runCatching { + rationaleDialog = AlertDialog.Builder(it) + .setTitle(R.string.dialog_title) + .setMessage(rationale) + .setPositiveButton(R.string.dialog_setting) { _, _ -> + it.startActivity( + Pair( + PermissionActivity.KEY_INPUT_REQUEST_TYPE, + TYPE_REQUEST_SETTING + ) ) - ) - } - .setNegativeButton(R.string.dialog_cancel) { _, _ -> cancel() } - .show() + } + .setNegativeButton(R.string.dialog_cancel) { _, _ -> cancel() } + .show() + } } } diff --git a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt index 376a47b76..4d32fb49d 100644 --- a/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt @@ -9,34 +9,44 @@ import io.legado.app.data.api.CommonHttpApi import io.legado.app.data.entities.SearchBook import io.legado.app.help.http.HttpHelper import kotlinx.coroutines.delay +import kotlinx.coroutines.launch + class SearchViewModel(application: Application) : BaseViewModel(application) { val searchBooks: LiveData> = MutableLiveData() fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { - execute { - val response: String = HttpHelper.getApiService( - "http://www.baidu.com" - ).get("http://www.baidu.com").await() + launch { + delay(1000L) - delay(4000L) - Log.e("TAG1", Thread.currentThread().name) + repeat(100) { + test(it) + } - response + } + } + + private fun test(index: Int) { + submit { + val response = HttpHelper.getApiService( + "http://www.baidu.com" + ).get("http://www.baidu.com") + + Log.e("TAG", "next: $index") + + response } - .timeout { 100L } - .onErrorReturn { "error return" } .onStart { - Log.e("TAG!", "start") + Log.e("TAG!", "start: $index") } .onSuccess { - Log.e("TAG!", "success: $it") + Log.e("TAG!", "success: $index --> $it") } .onError { - Log.e("TAG!", "error: $it") + Log.e("TAG!", "error: $index --> $it") } .onFinally { Log.e("TAG!", "finally")