package cn.flightfeather.thirdapp.module.inspection
|
|
import android.arch.lifecycle.MutableLiveData
|
import cn.flightfeather.thirdapp.bean.entity.Mediafile
|
import cn.flightfeather.thirdapp.common.net.ResultCallBack
|
import cn.flightfeather.thirdapp.model.enumreation.MediaFileType
|
import cn.flightfeather.thirdapp.model.enumreation.SceneType
|
import cn.flightfeather.thirdapp.module.base.BaseViewModel
|
import cn.flightfeather.thirdapp.repository.CommonRepository
|
import cn.flightfeather.thirdapp.repository.InspectionRepository
|
import cn.flightfeather.thirdapp.repository.ProblemRepository
|
import org.jetbrains.anko.collections.forEachByIndex
|
import org.jetbrains.anko.collections.forEachWithIndex
|
import org.jetbrains.anko.toast
|
|
/**
|
* @author riku
|
* Date: 2019/8/2
|
*/
|
class MenuCameraViewModel : BaseViewModel() {
|
|
class MediaData {
|
var type: MediaFileType? = null
|
var alias: String? = null
|
var dataList = ArrayList<Mediafile>()
|
}
|
|
private val inspectionRepository = InspectionRepository()
|
private val problemRepository = ProblemRepository()
|
private val commonRepository = CommonRepository()
|
|
//常规记录图片
|
val routineRecordList = MutableLiveData<ArrayList<Mediafile>>().apply { value = ArrayList() }
|
//铭牌图片
|
val nameplateList = MutableLiveData<ArrayList<Mediafile>>().apply { value = ArrayList() }
|
//监测设备图片
|
val monitorDeviceList = MutableLiveData<ArrayList<Mediafile>>().apply { value = ArrayList() }
|
|
val fileList = MutableLiveData<MutableList<MediaData>>().apply { value = mutableListOf() }
|
private var loadedCount = 0
|
|
private val dataSet = listOf(
|
Pair(MediaFileType.RoutineRecord, routineRecordList),
|
Pair(MediaFileType.Nameplate, nameplateList),
|
Pair(MediaFileType.MonitorDevice, monitorDeviceList)
|
)
|
|
/**
|
* 获取任意拍照图片
|
*/
|
fun getMediaFile(inspectionId: String, sceneTypeId: Int) {
|
commonRepository.getMediaFileTypes(sceneTypeId, object : ResultCallBack<List<MediaFileType>> {
|
override fun onSuccess(types: List<MediaFileType>?) {
|
fileList.value?.clear()
|
loadedCount = 0
|
types?.forEachWithIndex { i, type ->
|
inspectionRepository.getMediaFile(inspectionId, type.value, object : ResultCallBack<ArrayList<Mediafile>> {
|
override fun onSuccess(result: ArrayList<Mediafile>?) {
|
commonRepository.getAlias(sceneTypeId, type, object : ResultCallBack<String> {
|
override fun onSuccess(alias: String?) {
|
result?.let {
|
fileList.value?.add(MediaData().apply {
|
this.type = type
|
this.alias = if (alias.isNullOrBlank()) null else alias
|
dataList = it
|
dataList.add(0, Mediafile())
|
})
|
onMediaFileGet(types.size, sceneTypeId)
|
}
|
}
|
|
override fun onFailure() {
|
}
|
})
|
}
|
|
override fun onFailure() {
|
|
}
|
|
})
|
}
|
}
|
|
override fun onFailure() {
|
|
}
|
})
|
|
|
dataSet.forEach { p ->
|
|
}
|
}
|
|
/**
|
* 新增本地多媒体文件记录
|
*/
|
fun putMediaFile(mediaFile: Mediafile) {
|
problemRepository.putMediaFileLocal(mediaFile, object : ResultCallBack<Int> {
|
override fun onSuccess(result: Int?) {
|
|
}
|
|
override fun onFailure() {
|
|
}
|
|
})
|
}
|
|
fun updateAlias(sceneTypeId: Int, mediaFileType: MediaFileType, alias: String, s: (s: String) -> Unit) {
|
commonRepository.updateAlias(sceneTypeId, mediaFileType, alias, object : ResultCallBack<Boolean> {
|
override fun onSuccess(result: Boolean?) {
|
application.toast("修改成功")
|
s(alias)
|
}
|
|
override fun onFailure() {
|
application.toast("修改失败")
|
}
|
})
|
}
|
|
private fun onMediaFileGet(total: Int, sceneTypeId: Int) {
|
loadedCount++
|
if (loadedCount == total) {
|
val list = fileList.value?.sortedBy {
|
it.type?.value
|
}?.toMutableList() ?: mutableListOf()
|
if (sceneTypeId == SceneType.Construction.value
|
|| sceneTypeId == SceneType.Wharf.value
|
|| sceneTypeId == SceneType.MixingPlant.value
|
|| sceneTypeId == SceneType.StorageYard.value) {
|
fileList.value?.clear()
|
for (i in list.indices) {
|
if (list[i].type == MediaFileType.RoutineRecord) {
|
fileList.value?.add(list[i])
|
list.removeAt(i)
|
break
|
}
|
}
|
for (i in list.indices) {
|
if (list[i].type == MediaFileType.MonitorDevice) {
|
fileList.value?.add(list[i])
|
list.removeAt(i)
|
break
|
}
|
}
|
for (i in list.indices) {
|
if (list[i].type == MediaFileType.Nameplate) {
|
fileList.value?.add(list[i])
|
list.removeAt(i)
|
break
|
}
|
}
|
fileList.value?.addAll(list)
|
fileList.value = fileList.value
|
} else {
|
fileList.value = list
|
}
|
}
|
}
|
|
}
|