package com.example.smart.ui.item
|
|
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.bean.CommonOptionsBean
|
import com.example.smart.reqeust.RetrofitClient
|
import com.example.smart.ui.item.bean.ProjectInfoParam
|
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.launch
|
|
|
class CreateItemViewModel() : BaseContextViewModel() {
|
|
val projectOptionsBean = MutableLiveData<List<CommonOptionsBean>>()
|
|
val projectUsageOptionsBean = MutableLiveData<List<CommonOptionsBean>>()
|
|
val createResult = MutableLiveData<ProjectInfoParam>()
|
|
val editResult = MutableLiveData<String>()
|
|
val projectBean = MutableLiveData<ProjectInfoParam>()
|
|
fun requestProjectData(projectId: Int, fail: (String) -> Unit) {
|
|
viewModelScope.launch(Dispatchers.Main) {
|
delay(500)
|
safeCall {
|
execute(RetrofitClient.api.projectDetails(projectId)) }.apply {
|
if (this is Result.Success) {
|
projectBean.value = data
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
/**
|
* 项目类型下拉框
|
*/
|
fun requestProjectOptions(fail: (String) -> Unit) {
|
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
callList(RetrofitClient.api.projectTypeOptions())
|
}.apply {
|
if (this is Result.Success) {
|
projectOptionsBean.value = data
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
/**
|
* 类型用途
|
*/
|
fun requestUseTypeOptions(id: String, fail: (String) -> Unit) {
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
callList(RetrofitClient.api.projectUsageTypeDetails(id))
|
}.apply {
|
if (this is Result.Success) {
|
projectUsageOptionsBean.value = data
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
/**
|
* 新建项目接口
|
*/
|
fun createItem(param: ProjectInfoParam, fail: (String) -> Unit) {
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
execute(RetrofitClient.api.createItem(param)) }.apply {
|
if (this is Result.Success) {
|
createResult.value = data
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
/**
|
* 项目编辑
|
*/
|
fun editItem(param: ProjectInfoParam, fail: (String) -> Unit) {
|
|
viewModelScope.launch(Dispatchers.Main) {
|
safeCall {
|
callNoResult(RetrofitClient.api.editItem(param)) }.apply {
|
if (this is Result.SuccessNone) {
|
editResult.value = status
|
} else if (this is Result.Error) {
|
fail(exception.message ?: "未知错误")
|
}
|
}
|
}
|
}
|
|
|
}
|