package cn.flightfeather.thirdapp.module.inspection
|
|
import android.arch.lifecycle.MutableLiveData
|
import cn.flightfeather.thirdapp.bean.entity.*
|
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo
|
import cn.flightfeather.thirdapp.common.net.ResultCallBack
|
import cn.flightfeather.thirdapp.model.event.ProblemEvent
|
import cn.flightfeather.thirdapp.module.base.BaseViewModel
|
import cn.flightfeather.thirdapp.repository.ProblemRepository
|
import okhttp3.ResponseBody
|
import org.greenrobot.eventbus.EventBus
|
import org.jetbrains.anko.toast
|
import java.io.File
|
|
/**
|
* @author riku
|
* Date: 2019/8/1
|
*/
|
class MenuEvidenceViewModel : BaseViewModel() {
|
|
private val problemRepository = ProblemRepository()
|
|
//所有问题可能出现的位置
|
val locationList = MutableLiveData<ArrayList<Domainitem>>()
|
|
//所有问题的分类集合
|
val problemFatherType = MutableLiveData<ArrayList<String>>()
|
|
//当前所选问题分类下的详细问题集合
|
val problemType = MutableLiveData<ArrayList<Problemtype>>()
|
|
val suggestionList = MutableLiveData<ArrayList<String>>()
|
|
val problemMap = HashMap<String, ArrayList<Problemtype>>()
|
|
val mediaFileList = MutableLiveData<List<Mediafile>>()
|
|
//所有数据加载完成通知
|
val loadingOver = MutableLiveData<Boolean>()
|
//记录各个数据加载是否完成的状态
|
private val loadingStatus = BooleanArray(2)
|
|
/**
|
* 获取场景问题可选位置(目前只有工地,但所有场景都使用)
|
*/
|
fun getLocationList() {
|
problemRepository.getLocationList(object : ResultCallBack<ArrayList<Domainitem>> {
|
override fun onSuccess(result: ArrayList<Domainitem>?) {
|
result?.let {
|
locationList.value = it
|
onLoaded(0)
|
}
|
}
|
|
override fun onFailure() {
|
|
}
|
|
})
|
}
|
|
/**
|
* 获取对应场景下所有问题类型
|
*/
|
fun getProblemType(taskTypeId: Byte, cityCode: String, districtCode: String, sceneTypeId: Byte) {
|
problemRepository.getProblemType(taskTypeId, cityCode, districtCode, sceneTypeId, object : ResultCallBack<ArrayList<Problemtype>> {
|
override fun onSuccess(result: ArrayList<Problemtype>?) {
|
result?.let {
|
if (it.isEmpty()) {
|
it.add(Problemtype().apply {
|
guid = "0"
|
typename = "其他"
|
name = "其他"
|
})
|
}
|
|
val typeList = ArrayList<String>()
|
|
it.forEach {p ->
|
if (problemMap.containsKey(p.typename)) {
|
problemMap[p.typename]?.add(p)
|
} else {
|
typeList.add(p.typename)
|
problemMap[p.typename] = ArrayList<Problemtype>().apply { add(p) }
|
}
|
}
|
|
problemFatherType.value = typeList
|
|
onLoaded(1)
|
}
|
}
|
|
override fun onFailure() {
|
}
|
|
})
|
}
|
|
/**
|
* 获取问题对应的整改建议
|
*/
|
fun getSuggestionList(ptGuid: String) {
|
problemRepository.getSuggestion(ptGuid, object : ResultCallBack<ArrayList<ChangeAdvice>> {
|
override fun onSuccess(result: ArrayList<ChangeAdvice>?) {
|
result?.let {
|
val advices = ArrayList<String>()
|
it.forEach {c ->
|
advices.add(c.adName)
|
}
|
advices.add("暂无建议")
|
suggestionList.value = advices
|
|
}
|
}
|
|
override fun onFailure() {
|
}
|
|
})
|
}
|
|
/**
|
* 根据问题找到服务器和本地的所有图片
|
*/
|
fun getMediaFileList(p: ProblemlistVo) {
|
problemRepository.getMediaFileLocal(p.guid, object : ResultCallBack<List<Mediafile>> {
|
override fun onSuccess(result: List<Mediafile>?) {
|
result?.let {
|
p.mediafileList.addAll(it)
|
mediaFileList.value = p.mediafileList
|
}
|
}
|
|
override fun onFailure() {
|
}
|
})
|
}
|
|
/**
|
* 下载问题图片
|
* fixme: 2020/8/6 目前由于原程序设置图片的方式为手动下载图片,因此沿用,之后统一用Glide等第三方库代替
|
*/
|
fun downLoadMediaFile(mediaFile: Mediafile, s: (file: File) -> Unit) {
|
problemRepository.downloadMediaFile(mediaFile, object : ResultCallBack<File> {
|
override fun onSuccess(result: File?) {
|
result?.let {
|
s(it)
|
}
|
}
|
|
override fun onFailure() {
|
|
}
|
})
|
}
|
|
/**
|
* 删除问题图片
|
*/
|
fun deleteMediaFile(mediaFile: List<Mediafile>) {
|
mediaFile.forEach {
|
problemRepository.deleteMediaFile(it, object : ResultCallBack<Boolean> {
|
override fun onSuccess(result: Boolean?) {
|
|
}
|
|
override fun onFailure() {
|
}
|
})
|
}
|
}
|
|
/**
|
* 新增一个问题
|
*/
|
fun putProblem(problem: Problemlist) {
|
problemRepository.putOneProblemList(problem, object : ResultCallBack<ResponseBody> {
|
override fun onSuccess(result: ResponseBody?) {
|
result?.let {
|
application.toast("提交成功")
|
|
EventBus.getDefault().post(ProblemEvent(problem))
|
}
|
}
|
|
override fun onFailure() {
|
application.toast("提交失败")
|
}
|
|
})
|
}
|
|
/**
|
* 更新一个问题
|
*/
|
fun updateProblem(problem: ProblemlistVo) {
|
problemRepository.updateProblem(problem, object : ResultCallBack<ResponseBody> {
|
override fun onSuccess(result: ResponseBody?) {
|
result?.let {
|
application.toast("修改成功")
|
EventBus.getDefault().post(ProblemEvent(problem.voToEntity()))
|
}
|
}
|
|
override fun onFailure() {
|
application.toast("修改失败,请检查网络")
|
}
|
})
|
}
|
|
/**
|
* 新增本地多媒体文件记录
|
*/
|
fun putMediaFile(mediaFile: Mediafile) {
|
problemRepository.putMediaFileLocal(mediaFile, object : ResultCallBack<Int> {
|
override fun onSuccess(result: Int?) {
|
|
}
|
|
override fun onFailure() {
|
|
}
|
|
})
|
}
|
|
/**
|
* 下拉框选择一个问题类型时,需展示对应类别的具体问题
|
*/
|
fun refreshProblems(problemFatherType: String?) {
|
problemMap[problemFatherType]?.let {
|
problemType.value = it
|
}
|
}
|
|
/**
|
* 判断是否所有的数据都加载完成
|
*/
|
fun onLoaded(i: Int) {
|
if (i < loadingStatus.size) {
|
loadingStatus[i] = true
|
}
|
var isLoadOver = true
|
loadingStatus.forEach {
|
isLoadOver = isLoadOver.and(it)
|
}
|
if (isLoadOver) {
|
loadingOver.value = isLoadOver
|
for (y in loadingStatus.indices) {
|
loadingStatus[y] = false
|
}
|
}
|
}
|
}
|