65 lines
1.4 KiB
Kotlin
65 lines
1.4 KiB
Kotlin
package com.xscm.moduleutil.base
|
|
|
|
import androidx.lifecycle.*
|
|
import com.xscm.moduleutil.http.RetrofitClient
|
|
import com.xscm.moduleutil.widget.room.PassRoomException
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.launch
|
|
import okhttp3.MultipartBody
|
|
|
|
|
|
/**
|
|
*
|
|
*/
|
|
open class BaseViewModel : ViewModel(), LifecycleObserver {
|
|
private var clickTime: Long = 0
|
|
var baseRepository = RetrofitClient.getInstance()
|
|
|
|
private val passRoom by lazy { MutableLiveData<Exception>() }
|
|
|
|
private val error by lazy { MutableLiveData<Exception>() }
|
|
|
|
private val finally by lazy { MutableLiveData<Int>() }
|
|
|
|
|
|
|
|
|
|
//进入房间
|
|
|
|
var imgListData: MutableLiveData<List<String>> = MutableLiveData()
|
|
var addImgData = MutableLiveData<Any>()
|
|
|
|
|
|
//运行在UI线程的协程
|
|
fun launchUI(block: suspend CoroutineScope.() -> Unit) = viewModelScope.launch {
|
|
try {
|
|
block()
|
|
} catch (e: Exception) {
|
|
if (e is PassRoomException) {
|
|
passRoom.value = e
|
|
} else {
|
|
error.value = e
|
|
// throw e
|
|
}
|
|
|
|
} finally {
|
|
finally.value = 200
|
|
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 请求失败,出现异常
|
|
*/
|
|
fun getError(): LiveData<Exception> {
|
|
return error
|
|
}
|
|
|
|
/**
|
|
* 请求完成,在此处做一些关闭操作
|
|
*/
|
|
fun getFinally(): LiveData<Int> {
|
|
return finally
|
|
}
|
|
|
|
} |