parent
78db8194dc
commit
9bff78b22f
@ -0,0 +1,89 @@ |
||||
package io.legado.app.help.coroutine |
||||
|
||||
import kotlinx.coroutines.* |
||||
|
||||
class Coroutine<T>(private val domain: (suspend CoroutineScope.() -> T)? = null) : CoroutineScope by MainScope() { |
||||
|
||||
companion object { |
||||
|
||||
fun <T> of(value: suspend CoroutineScope.() -> T): Coroutine<T> { |
||||
return Coroutine(value) |
||||
} |
||||
} |
||||
|
||||
private var start: (() -> Unit)? = null |
||||
private var success: ((T?) -> Unit)? = null |
||||
private var error: ((Throwable) -> Unit)? = null |
||||
private var finally: (() -> Unit)? = null |
||||
|
||||
private var value: T? = null |
||||
|
||||
init { |
||||
val job: Job = launch { |
||||
tryCatch( |
||||
{ |
||||
start?.let { it() } |
||||
|
||||
|
||||
val result: T? = withContext(Dispatchers.IO) { |
||||
domain?.let { |
||||
value = it() |
||||
return@let value |
||||
} |
||||
} |
||||
|
||||
success?.let { it(result) } |
||||
|
||||
}, |
||||
{ e -> |
||||
error?.let { it(e) } |
||||
}, |
||||
{ |
||||
finally?.let { it() } |
||||
}) |
||||
} |
||||
} |
||||
|
||||
fun onStart(start: (() -> Unit)): Coroutine<T> { |
||||
this.start = start |
||||
return this@Coroutine |
||||
} |
||||
// |
||||
// fun <U> map(func: Function<T, U>): Coroutine<U> { |
||||
// return of { func.apply(value) } |
||||
// } |
||||
// |
||||
// fun <U> flatMap(func: Function<T, Coroutine<U>>): Coroutine<U> { |
||||
// return func.apply(value) |
||||
// } |
||||
|
||||
fun onSuccess(success: (T?) -> Unit): Coroutine<T> { |
||||
this.success = success |
||||
return this@Coroutine |
||||
} |
||||
|
||||
fun onError(error: (Throwable) -> Unit): Coroutine<T> { |
||||
this.error = error |
||||
return this@Coroutine |
||||
} |
||||
|
||||
fun onFinally(finally: () -> Unit): Coroutine<T> { |
||||
this.finally = finally |
||||
return this@Coroutine |
||||
} |
||||
|
||||
private suspend fun tryCatch( |
||||
tryBlock: suspend CoroutineScope.() -> Unit, |
||||
errorBlock: (suspend CoroutineScope.(Throwable) -> Unit)? = null, |
||||
finallyBlock: (suspend CoroutineScope.() -> Unit)? = null |
||||
) { |
||||
try { |
||||
coroutineScope { tryBlock() } |
||||
} catch (e: Throwable) { |
||||
coroutineScope { errorBlock?.let { it(e) } } |
||||
} finally { |
||||
coroutineScope { finallyBlock?.let { it() } } |
||||
} |
||||
} |
||||
|
||||
} |
@ -0,0 +1,7 @@ |
||||
package io.legado.app.help.coroutine |
||||
|
||||
interface Function<T, R> { |
||||
|
||||
fun apply(t: T?): R |
||||
|
||||
} |
@ -1,44 +1,65 @@ |
||||
package io.legado.app.ui.search |
||||
|
||||
import android.app.Application |
||||
import android.util.Log |
||||
import androidx.lifecycle.LiveData |
||||
import androidx.lifecycle.MutableLiveData |
||||
import io.legado.app.base.BaseViewModel |
||||
import io.legado.app.data.api.CommonHttpApi |
||||
import io.legado.app.data.entities.SearchBook |
||||
import io.legado.app.help.coroutine.Coroutine |
||||
import io.legado.app.help.coroutine.Function |
||||
import io.legado.app.help.http.HttpHelper |
||||
import kotlinx.coroutines.* |
||||
import kotlinx.coroutines.Dispatchers.IO |
||||
import kotlinx.coroutines.coroutineScope |
||||
import kotlinx.coroutines.launch |
||||
import kotlinx.coroutines.withContext |
||||
import org.jetbrains.anko.error |
||||
import java.lang.StringBuilder |
||||
|
||||
class SearchViewModel(application: Application) : BaseViewModel(application) { |
||||
|
||||
val searchBooks: LiveData<List<SearchBook>> = MutableLiveData() |
||||
|
||||
fun search(start: () -> Unit, finally: () -> Unit) { |
||||
launchOnUI( |
||||
{ |
||||
start() |
||||
val searchResponse = withContext(IO) { |
||||
HttpHelper.getApiService<CommonHttpApi>( |
||||
"http:www.baidu.com" |
||||
).post("", mutableMapOf(Pair("key", "value"))) |
||||
} |
||||
fun search(start: (() -> Unit)? = null, finally: (() -> Unit)? = null) { |
||||
// launchOnUI( |
||||
// { |
||||
// start?.let { it() } |
||||
// val searchResponse = withContext(IO) { |
||||
// val response: Deferred<String> = HttpHelper.getApiService<CommonHttpApi>( |
||||
// "http://www.baidu.com" |
||||
// ).get("http://www.baidu.com") |
||||
// |
||||
// } |
||||
// |
||||
//// val result = searchResponse.await() |
||||
//// |
||||
//// println(result) |
||||
// }, |
||||
// { error { "${it.message}" } }, |
||||
// { finally?.let { it() } }) |
||||
|
||||
val result = searchResponse.await() |
||||
}, |
||||
{ error { "${it.message}" } }, |
||||
{ finally() }) |
||||
val task = Coroutine.of { |
||||
val response: String = HttpHelper.getApiService<CommonHttpApi>( |
||||
"http://www.baidu.com" |
||||
).get("http://www.baidu.com").await() |
||||
|
||||
launchOnUI({ |
||||
//TODO |
||||
}) |
||||
|
||||
launch { |
||||
Log.e("TAG1", Thread.currentThread().name) |
||||
|
||||
response |
||||
} |
||||
.onStart { |
||||
Log.e("TAG!", "start") |
||||
} |
||||
.onSuccess { |
||||
Log.e("TAG!", "success: $it") |
||||
} |
||||
.onError { |
||||
error { "${it.message}" } |
||||
} |
||||
.onFinally { |
||||
Log.e("TAG!", "finally") |
||||
} |
||||
|
||||
// task.cancel() |
||||
} |
||||
|
||||
} |
||||
|
Loading…
Reference in new issue