yijianlianUI/app/src/main/java/com/yingmao/clash/base/BaseViewMode.kt

285 lines
10 KiB
Kotlin
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package com.yingmao.clash.base
import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.google.gson.Gson
import com.yingmao.clash.common.log.Log
import com.yingmao.clash.net.http.HttpReqType
import com.yingmao.clash.net.http.HttpStatus
import com.yingmao.clash.net.http.HttpStatusDownLoad
import com.yingmao.clash.net.http.entity.BaseRsp
import com.yingmao.clash.util.AESUtil
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.async
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
open class BaseViewMode : ViewModel() {
val statusData = MutableLiveData<HttpStatus>()
val downStatusData = MutableLiveData<HttpStatusDownLoad>()
fun doAsync(reqType: HttpReqType, doInAsync: suspend () -> Any) {
viewModelScope.launch(
CoroutineExceptionHandler { _,
exception ->
Log.e("BaseViewMode_doAsync:${exception.toString()}", exception)
statusData.value =
if (exception.message.isNullOrBlank()) HttpStatus.Failure(
reqType,
"网络异常"
) else HttpStatus.Failure(
reqType,
"$exception"
)
})
{
try {
val result = async {
statusData.value = HttpStatus.Loading(reqType)
doInAsync()
}
val rsp = result.await()
Log.i("请求结果:$rsp")
statusData.value = HttpStatus.Success(reqType, rsp)
} catch (e: Exception) {
Log.e( "网络请求异常",e)
statusData.value = HttpStatus.Failure(reqType, e.toString())
}
}
}
fun doAsync2(reqType: HttpReqType, doInAsync: suspend () -> Any) {
viewModelScope.launch(
CoroutineExceptionHandler { _,
exception ->
Log.e("BaseViewMode_doAsync:${exception.toString()}", exception)
statusData.value =
if (exception.message.isNullOrBlank()) HttpStatus.Failure(
reqType,
"网络异常"
) else HttpStatus.Failure(
reqType,
"$exception"
)
})
{
try {
val result = launch {
statusData.value = HttpStatus.Loading(reqType)
doInAsync()
}
val rsp = result
Log.i("请求结果:$rsp")
statusData.value = HttpStatus.Success(reqType, rsp)
} catch (e: Exception) {
Log.e( "网络请求异常",e)
statusData.value = HttpStatus.Failure(reqType, e.toString())
}
}
}
// fun doAsyncDown(reqType: HttpReqType, doInAsync: suspend () -> ResponseBody) {
// viewModelScope.launch(
// CoroutineExceptionHandler { _,
// exception ->
// Log.e("BaseViewMode_doAsync:${exception.toString()}", exception)
// downStatusData.value =
// if (exception.message.isNullOrBlank()) HttpStatusDownLoad.Failure(
// reqType,
// "网络异常"
// ) else HttpStatusDownLoad.Failure(
// reqType,
// "$exception"
// )
// })
// {
// try {
//
//
// val rsp = result.await()
// Log.i("请求结果:$rsp")
// downStatusData.value = HttpStatusDownLoad.Success(reqType, rsp)
// } catch (e: Exception) {
// Log.e( "网络请求异常",e)
// downStatusData.value = HttpStatusDownLoad.Failure(reqType, e.toString())
// }
// }
// }
fun doAsyncDelay(
reqType: HttpReqType,
delayTimeMillis: Long,
doInAsync: suspend () -> BaseRsp
) {
viewModelScope.launch(
CoroutineExceptionHandler { _,
exception ->
Log.e("BaseViewMode_doAsyncDelay:${exception.toString()}", exception)
statusData.value =
if (exception.message.isNullOrBlank()) HttpStatus.Failure(
reqType,
"网络异常"
) else HttpStatus.Failure(
reqType,
"${exception.message}"
)
})
{
try {
statusData.value = HttpStatus.Loading(reqType)
delay(delayTimeMillis)
val result = async {
doInAsync()
}
val rsp = result.await()
Log.i("请求结果:$rsp")
statusData.value = HttpStatus.Success(reqType, rsp)
} catch (e: Exception) {
Log.e( "网络请求异常",e)
// statusData.value = HttpStatus.Failure(reqType,e.toString())
}
}
}
fun doAsyncStrResult(reqType: HttpReqType, doInAsync: suspend () -> String) {
viewModelScope.launch(
CoroutineExceptionHandler { _,
exception ->
Log.e("BaseViewMode_doAsyncStrResult:${exception.toString()}", exception)
})
{
try {
val result = async { doInAsync() }
val rsp = result.await()
Log.i("请求结果:$rsp")
} catch (e: Exception) {
Log.e( "网络请求异常",e)
}
}
}
fun doAsyncEncodeStr(
reqType: HttpReqType,
classOfT: Class<out BaseRsp>,
doInAsync: suspend () -> String
) {
viewModelScope.launch(
CoroutineExceptionHandler() { _,
exception ->
Log.e("BaseViewMode_doAsyncEncodeStr:${exception.toString()}", exception)
statusData.value =
if (exception.message.isNullOrBlank()) HttpStatus.Failure(
reqType,
"网络异常"
) else HttpStatus.Failure(
reqType,
"${exception.message}"
)
})
{
try {
statusData.value = HttpStatus.Loading(reqType)
val result = async { doInAsync() }
val rspStr = result.await()
Log.i("doAsyncEncodeStr 加密请求结果:$rspStr")
if (!TextUtils.isEmpty(rspStr)) {
var decodeRspStr = AESUtil.decrypt(rspStr)
Log.d("doAsyncEncodeStr 请求结果:$decodeRspStr")
if (decodeRspStr.contains(",\"data\":\"\"")) {
decodeRspStr = decodeRspStr.replace(",\"data\":\"\"", ",\"data\":null")
}
Log.d("doAsyncEncodeStr Replace请求结果$decodeRspStr")
val rsp = Gson().fromJson(decodeRspStr, classOfT)
statusData.value = HttpStatus.Success(reqType, rsp)
}
} catch (e: Exception) {
Log.e("网络请求异常",e)
throw e
statusData.value = HttpStatus.Failure(reqType,e.toString())
}
}
}
suspend fun doAsyncEncodeStrBg(
classOfT: Class<out BaseRsp>,
doInAsync: suspend () -> String
): BaseRsp? {
return viewModelScope.async(
CoroutineExceptionHandler() { _,
exception ->
Log.e("BaseViewMode_doAsyncEncodeStrBg:${exception.toString()}", exception)
})
{
try {
val result = async { doInAsync() }
val rspStr = result.await()
Log.i("加密请求结果:$rspStr")
val decodeRspStr = AESUtil.decrypt(rspStr)
Log.d("请求结果:$decodeRspStr")
val rsp = Gson().fromJson(decodeRspStr, classOfT)
return@async rsp
} catch (e: Exception) {
Log.e("网络请求异常",e)
}
return@async null
}.await()
}
fun doAsyncNoNeedReturn(reqType: HttpReqType, doInAsync: suspend () -> Unit) {
viewModelScope.launch()
{
try {
val result = async {
doInAsync()
}
result.await()
} catch (e: Exception) {
Log.e("网络请求异常",e)
}
}
}
// fun <T>doAsyncs(doInAsyncs :Map<T,suspend () -> T>) {
// viewModelScope.launch (
// CoroutineExceptionHandler(){
// _,
// exception ->
// statusData.value = if (exception.message.isNullOrBlank()) HttpStatus.Failure("登录异常") else HttpStatus.Failure("登录异常:${exception.message}")
//
// })
// {
// statusData.value = HttpStatus.Loading
// doInAsyncs.forEach {
// it.key
// val result =async{
// it.value
// }
// val rsp = result.await()
// }
// val result = async { doInAsync()}
// val rsp :T = result.await()
// tData.value = rsp
// statusData.value = HttpStatus.Success
// }
// }
// inner class Factory(private val dataMap: HashMap<String, Any>):ViewModelProvider.Factory{
//
// override fun <T : ViewModel?> create(modelClass: Class<T>): T {
//
// return BaseViewMode2(dataMap) as T
// }
//
// }
}