package com.example.smart.ui.project.check import androidx.lifecycle.MutableLiveData import androidx.lifecycle.viewModelScope import com.example.basecore.base.BaseContextViewModel import com.example.basecore.net.http.Result import com.example.smart.reqeust.RetrofitClient import com.example.smart.reqeust.RetrofitClient.getBody import com.example.smart.ui.home.page.data.PageReq import com.example.smart.ui.project.check.bean.CheckBean import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch class CheckViewModel : BaseContextViewModel() { val checkList = MutableLiveData>() fun getDataList(id: String, mine: Boolean, fail: (String) -> Unit) { val params = PageReq() viewModelScope.launch(Dispatchers.Main) { safeCall { execute(RetrofitClient.api.checkList(params, id, mine)) }.apply { if (this is Result.Success) { checkList.value = data.list ?: ArrayList() } else if (this is Result.Error) { fail(exception.message ?: "未知错误") } } } } fun addCheckInfo(item: CheckBean, suc: () -> Unit) { val map = mapOf( "checkTaskIdId" to item.checkTaskId, "title" to item.title, "safetySlip" to item.safetySlip, "checkUserImg" to item.checkUserImg, "integrityNotification" to item.integrityNotification, ) viewModelScope.launch(Dispatchers.Main) { safeCall { callNoResult(RetrofitClient.api.addCheck(getBody(map))) }.apply { if (this is Result.SuccessNone) { suc.invoke() } else if (this is Result.Error) { com.blankj.utilcode.util.ToastUtils.showLong(exception.message ?: "未知错误") } } } } fun editCheckInfo(item: CheckBean, suc: () -> Unit) { val map = mapOf( "id" to item.id, "title" to item.title, "safetySlip" to item.safetySlip, "checkUserImg" to item.checkUserImg, "integrityNotification" to item.integrityNotification, ) viewModelScope.launch(Dispatchers.Main) { safeCall { callNoResult(RetrofitClient.api.editCheck(getBody(map))) }.apply { if (this is Result.SuccessNone) { suc.invoke() } else if (this is Result.Error) { com.blankj.utilcode.util.ToastUtils.showLong(exception.message ?: "未知错误") } } } } fun deleteCheckInfo(item: CheckBean, suc: () -> Unit) { val map = mapOf( "ids" to arrayOf(item.id) ) viewModelScope.launch(Dispatchers.Main) { safeCall { callNoResult(RetrofitClient.api.deleteCheck(getBody(map))) }.apply { if (this is Result.SuccessNone) { suc.invoke() } else if (this is Result.Error) { com.blankj.utilcode.util.ToastUtils.showLong(exception.message ?: "未知错误") } } } } }