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() } private val error by lazy { MutableLiveData() } private val finally by lazy { MutableLiveData() } //进入房间 var imgListData: MutableLiveData> = MutableLiveData() var addImgData = MutableLiveData() //运行在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 { return error } /** * 请求完成,在此处做一些关闭操作 */ fun getFinally(): LiveData { return finally } }