package com.example.smart.ui.project
|
|
import androidx.lifecycle.MutableLiveData
|
import androidx.lifecycle.viewModelScope
|
import com.example.basecore.base.BaseContextViewModel
|
import com.example.basecore.net.http.Result
|
import com.example.basecore.util.ToastUtils
|
import com.example.smart.MyApp
|
import com.example.smart.reqeust.RetrofitClient
|
import com.example.smart.ui.project.bean.TaskDetailInfo
|
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.launch
|
|
|
class ProjectViewModel: BaseContextViewModel() {
|
|
val taskInfo = MutableLiveData<TaskDetailInfo>()
|
|
val addressInfo = MutableLiveData<String>()
|
|
val closeResultBean = MutableLiveData<String>()
|
|
|
/**
|
* 任务详情
|
*/
|
fun requestTaskInfo(taskId: String) {
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
execute(RetrofitClient.api.taskDetails(taskId)) }.apply {
|
if (this is Result.Success) {
|
taskInfo.value = data
|
} else if (this is Result.Error) {
|
ToastUtils.showToast(MyApp.app, exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
|
/**
|
* 编辑任务,此处只更新地址
|
*/
|
fun editTask(bean: TaskDetailInfo) {
|
viewModelScope.launch(Dispatchers.Main) {
|
val result = safeCall { callNoResult(RetrofitClient.api.editTask(bean)) }
|
if (result is Result.SuccessNone) {
|
addressInfo.value = "成功"
|
} else if (result is Result.Error) {
|
ToastUtils.showToast(MyApp.app, result.exception.message ?: "未知错误")
|
}
|
}
|
}
|
|
|
|
/**
|
* 关闭任务
|
*/
|
fun closeProject(projectId: String, fail: (String) -> Unit) {
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
callNoResult(RetrofitClient.api.closeTask(projectId)) }.apply {
|
if (this is Result.SuccessNone) {
|
closeResultBean.value = status
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
|
/**
|
* 关闭复核任务
|
*/
|
fun closeReviewProject(projectId: String, fail: (String) -> Unit) {
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
callNoResult(RetrofitClient.api.closeReviewTask(projectId)) }.apply {
|
if (this is Result.SuccessNone) {
|
closeResultBean.value = status
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
}
|