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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package cn.flightfeather.thirdapp.module.inspection
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdapp.CommonApplication
import cn.flightfeather.thirdapp.bean.Subtask
import cn.flightfeather.thirdapp.bean.vo.DayTaskProgressVo
import cn.flightfeather.thirdapp.bean.vo.TaskVo
import cn.flightfeather.thirdapp.common.net.ResultCallBack
import cn.flightfeather.thirdapp.module.base.BaseViewModel
import cn.flightfeather.thirdapp.repository.InspectionRepository
import cn.flightfeather.thirdapp.repository.SubTaskRepository
import okhttp3.ResponseBody
import org.jetbrains.anko.toast
 
/**
 * @author riku
 * Date: 2019/7/29
 */
class InspectionViewModel : BaseViewModel() {
 
    private val inspectionRepository = InspectionRepository()
 
    private val subTaskRepository = SubTaskRepository()
 
    var monthTaskList = MutableLiveData<ArrayList<TaskVo>>()
 
    var dayTaskList = MutableLiveData<ArrayList<DayTaskProgressVo>>()
 
    var subTaskList = MutableLiveData<ArrayList<Subtask>>()
 
    var hasUnUploadImage = MutableLiveData<Boolean>()
 
    /**
     * 获取月任务
     * @param yearMonth 年月
     */
    fun getMonthTask(yearMonth: String) {
        inspectionRepository.getMonthDayTask(yearMonth, userId, userTypeId.toString(),
                object : ResultCallBack<ArrayList<TaskVo>> {
                    override fun onSuccess(result: ArrayList<TaskVo>?) {
                        result?.let {
                            monthTaskList.value = it
                        }
                    }
 
                    override fun onFailure() {
 
                    }
                })
    }
 
    /**
     * 获取日任务
     * @param taskId 总任务id
     */
    fun getDayTask(taskId: String) {
        inspectionRepository.getDayTask(taskId, userId, userTypeId.toString(),
                object : ResultCallBack<ArrayList<DayTaskProgressVo>> {
                    override fun onSuccess(result: ArrayList<DayTaskProgressVo>?) {
                        result?.let {
                            dayTaskList.value = it
                        }
                    }
 
                    override fun onFailure() {
                    }
 
                })
    }
 
    /**
     * 获取子任务
     * @param dayTaskId 日任务id
     */
    fun getSubTask(dayTaskId: String) {
        inspectionRepository.getSubTask(dayTaskId, userId, userTypeId.toString(),
                object : ResultCallBack<ArrayList<Subtask>> {
                    override fun onSuccess(result: ArrayList<Subtask>?) {
                        result?.let {
                            subTaskList.value = it
                        }
                    }
 
                    override fun onFailure() {
                    }
 
                })
    }
 
    /**
     * 删除子任务
     */
    fun deleteSubTask(subTaskId: String, onSuccess: () -> Unit) {
        subTaskRepository.deleteSubTask(subTaskId, object : ResultCallBack<ResponseBody> {
            override fun onSuccess(result: ResponseBody?) {
                if (result != null) {
                    CommonApplication.getInstance().toast("删除成功")
                    onSuccess()
                } else {
                    CommonApplication.getInstance().toast("删除失败")
                }
            }
 
            override fun onFailure() {
                CommonApplication.getInstance().toast("网络错误")
            }
        })
    }
 
    /**
     * 查询本地是否有图片未上传
     */
    fun checkUnUploadImage() {
        inspectionRepository.checkUnUploadImage(object : ResultCallBack<Boolean> {
            override fun onSuccess(result: Boolean?) {
                result?.let {
                    hasUnUploadImage.value = it
                }
            }
 
            override fun onFailure() {
 
            }
 
        })
    }
}