18034727885
2026-05-14 dad2ded3e9e9d2edf55c7ce10e791588b526961f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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 ?: "未知错误")
                }
            }
        }
    }
 
 
}