pull/32/head
Administrator 5 years ago
parent a34d66ca1f
commit a71c67b913
  1. 38
      app/src/main/java/io/legado/app/ui/search/SearchViewModel.kt

@ -9,47 +9,41 @@ import io.legado.app.data.api.CommonHttpApi
import io.legado.app.data.entities.SearchBook import io.legado.app.data.entities.SearchBook
import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.HttpHelper
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class SearchViewModel(application: Application) : BaseViewModel(application) { class SearchViewModel(application: Application) : BaseViewModel(application) {
val searchBooks: LiveData<List<SearchBook>> = MutableLiveData() val searchBooks: LiveData<List<SearchBook>> = MutableLiveData()
fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) {
launch { execute {
delay(1000L) val response: String = HttpHelper.getApiService<CommonHttpApi>(
repeat(100) {
test(it)
}
}
}
private fun test(index: Int) {
submit {
val response = HttpHelper.getApiService<CommonHttpApi>(
"http://www.baidu.com" "http://www.baidu.com"
).get("http://www.baidu.com") ).get("http://www.baidu.com").await()
Log.e("TAG", "next: $index") delay(4000L)
Log.e("TAG1", Thread.currentThread().name)
response response
} }
.timeout { 100L }
.onErrorReturn { "error return" }
.onStart { .onStart {
Log.e("TAG!", "start: $index") Log.e("TAG!", "start")
start?.let { it() }
} }
.onSuccess { .onSuccess {
Log.e("TAG!", "success: $index --> $it") Log.e("TAG!", "success: $it")
} }
.onError { .onError {
Log.e("TAG!", "error: $index --> $it") Log.e("TAG!", "error: $it")
} }
.onFinally { .onFinally {
Log.e("TAG!", "finally") Log.e("TAG!", "finally")
if (finally != null) {
finally()
}
} }
} }

Loading…
Cancel
Save