riku
2025-10-17 fbae5f3ea74727ccadc48314a864a1ea0099a945
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package cn.flightfeather.thirdappmodule.module.inspection
 
import android.arch.lifecycle.MutableLiveData
import cn.flightfeather.thirdappmodule.bean.entity.Mediafile
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.model.enumreation.MediaFileType
import cn.flightfeather.thirdappmodule.model.enumreation.SceneType
import cn.flightfeather.thirdappmodule.module.base.BaseViewModel
import cn.flightfeather.thirdappmodule.repository.CommonRepository
import cn.flightfeather.thirdappmodule.repository.InspectionRepository
import cn.flightfeather.thirdappmodule.repository.ProblemRepository
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.instance
 
    //常规记录图片
    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
 
    /**
     * 获取任意拍照图片
     */
    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?.forEach { t ->
                    inspectionRepository.getMediaFile(inspectionId, t.value, object : ResultCallBack<ArrayList<Mediafile>> {
                        override fun onSuccess(result: ArrayList<Mediafile>?) {
                            commonRepository.getAlias(sceneTypeId, t.value, object : ResultCallBack<String> {
                                override fun onSuccess(alias: String?) {
                                    result?.let {
                                        fileList.value?.add(MediaData().apply {
                                            this.type = t
                                            this.alias = if (alias.isNullOrBlank()) null else alias
                                            dataList = it
                                            dataList.add(0, Mediafile())
                                        })
                                        onMediaFileGet(types.size, types)
                                    }
                                }
 
                                override fun onFailure() {
                                }
                            })
                        }
 
                        override fun onFailure() {
 
                        }
 
                    })
                }
            }
 
            override fun onFailure() {
 
            }
        })
    }
 
    /**
     * 新增本地多媒体文件记录
     */
    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, types: List<MediaFileType>) {
        loadedCount++
        // 当每种类型的数据都获取完毕后,进行重排序操作
        if (loadedCount == total) {
            val tempList = mutableListOf<MediaData>()
            types.forEach { t ->
                fileList.value?.find { it.type?.value == t.value }?.let { tempList.add(it) }
            }
            fileList.value = tempList
 
//            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
//            }
        }
    }
 
}