18034727885
2026-04-14 370014cbb9ec1ef64e7e42bb4f9a12671461d8e4
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package com.xw.yunwei.ui.task.accident.detail
 
import android.os.Bundle
import android.view.LayoutInflater
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.GridLayoutManager
import com.amap.api.location.AMapLocation
import com.amap.api.maps.model.LatLng
import com.xw.common.CommonDialog
import com.xw.common.base.BaseActivity
import com.xw.common.base.FileSelectBean
import com.xw.common.base.ImageEventListener
import com.xw.common.base.ImageSelectAdapter
import com.xw.common.base.ShowImgListDialog
import com.xw.common.utils.BindUtils.getUrls
import com.xw.common.utils.GpsHelper
import com.xw.common.utils.GpsHelper.Companion.navigate
import com.xw.common.utils.ToastUtilCommon
import com.xw.yunwei.BR
import com.xw.yunwei.MyApp
import com.xw.yunwei.R
import com.xw.yunwei.databinding.ActivityAccidentDetailBinding
import com.xw.yunwei.databinding.LayoutAccidentType1Binding
import com.xw.yunwei.databinding.LayoutAccidentType2Binding
import com.xw.yunwei.model.XpErrorType
import java.util.ArrayList
import kotlin.text.ifEmpty
 
 
/**
 *  事故详情
 */
class AccidentDetailActivity : BaseActivity<ActivityAccidentDetailBinding, AccidentDetailVM>(), GpsHelper.OnMapLocation {
    override fun getBindingVariable() = BR.viewModel
 
    lateinit var imageEvent: ImageEventListener<FileSelectBean>
    lateinit var adapter: ImageSelectAdapter
    var gpsHelper: GpsHelper? = null
    var savedInstanceState: Bundle?=null
 
    override fun myCreate(savedInstanceState: Bundle?) {
        super.myCreate(savedInstanceState)
        this.savedInstanceState = savedInstanceState
    }
 
    override fun init() {
        super.init()
        viewModel.taskId = intent.getLongExtra("taskId", 0L)
        viewModel.queryDetail()
        initAdapter()
    }
 
    override fun onResume() {
        super.onResume()
        viewModel.getAppPermission()
    }
 
    override fun setObservable() {
        super.setObservable()
        viewModel.taskDetail.observe(this) {
            it.taskImage?.getUrls()?.let {
                adapter.setList(it.map { FileSelectBean(url = it, type = "img") })
            }
            initTypeLayout(it.errorTypeList)
            if (viewModel.taskDetail.value?.status == 0) { // 待到达 才需要定位
                if (gpsHelper == null) {
                    gpsHelper = GpsHelper(
                        this,
                        this,
                        isOnceLocation = true,
                        isAutomatic = true,
                        savedInstanceState = savedInstanceState
                    )
                    lifecycle.addObserver(gpsHelper!!)
                }
            }
            viewModel.locationCheck()
            checkActionPermissions()
        }
        viewModel.canConfirmArrive.observe(this){
            if (it) {
                binding.tvControl.setBackgroundResource(R.drawable.shape_blue_5_bg_4)
            } else {
                binding.tvControl.setBackgroundResource(R.drawable.shape_blue_1_bg_4)
            }
        }
        MyApp.permissions.observe(this){
            checkActionPermissions()
        }
    }
 
    private fun checkActionPermissions() {
        when (viewModel.taskDetail.value?.status) {
            0 -> viewModel.actionPermissions.value =
                MyApp.findPermissionByName("到达事故现场")?.visible == "0"
 
            1 -> viewModel.actionPermissions.value =
                MyApp.findPermissionByName("处理事故")?.visible == "0"
        }
    }
 
    override fun setListener() {
        super.setListener()
        binding.nav.setOnClickListener {
            viewModel.taskDetail.value?.let { item ->
                if ((item.vehicleLat ?: 0.0) != 0.0 && (item.vehicleLon ?: 0.0) != 0.0) {
                    navigate(
                        end = LatLng(item.vehicleLat!!, item.vehicleLon!!),
                        context = this
                    )
                } else {
                    ToastUtilCommon.showError("数据异常")
                }
            }
        }
    }
 
    private fun initAdapter() {
        imageEvent = object : ImageEventListener<FileSelectBean>() {
            override fun viewImage(item: FileSelectBean) {
                val imgList: MutableList<String> = ArrayList()
                for (item1 in adapter.data) {
                    imgList.add(item1.localPath.ifEmpty { item1.url })
                }
                ShowImgListDialog(
                    this@AccidentDetailActivity,
                    imgList,
                    imgList.indexOf(item.localPath)
                ).show()
            }
        }
        adapter = ImageSelectAdapter(this, 9, 0, imageEvent)
        binding.rv.adapter = adapter
        binding.rv.layoutManager = GridLayoutManager(this, 5)
    }
 
    private fun initTypeLayout(errorTypeList: List<XpErrorType>?) {
        binding.accidentLayout.removeAllViews()
        errorTypeList?.forEach {
            val bind: LayoutAccidentType1Binding = DataBindingUtil.inflate(
                LayoutInflater.from(this),
                R.layout.layout_accident_type1,
                null,
                false
            )
            bind.name.text = it.errorName
            bind.icon.setImageResource(map[it.errorName] ?: R.mipmap.ic_task_accident1)
            it.child?.forEach {
                val tvBind: LayoutAccidentType2Binding = DataBindingUtil.inflate(
                    LayoutInflater.from(this),
                    R.layout.layout_accident_type2,
                    null,
                    false
                )
                tvBind.tv.text = it.errorName
                bind.tagLayout.addView(tvBind.root)
            }
            binding.accidentLayout.addView(bind.root)
        }
    }
 
    val map = mapOf(
        "车辆技术故障类" to R.mipmap.ic_task_accident1,
 
        "运行安全事故类" to R.mipmap.ic_task_accident2,
 
        "网络安全与数据安全事件类" to R.mipmap.ic_task_accident3,
 
        "自然灾害与恶劣天气类" to R.mipmap.ic_task_accident4,
 
        "人员安全与公共安全类" to R.mipmap.ic_task_accident5,
 
        "综合保障事件类" to R.mipmap.ic_task_accident6,
    )
 
    override fun location(pLocation: AMapLocation?) {
        if (pLocation == null) {
            CommonDialog.showTipsDialog(
                this@AccidentDetailActivity,
                "您未开启gps权限,无法获取当前定位,请打开后重新操作",
                null)
        } else {
            viewModel.location = pLocation
            viewModel.locationCheck()
        }
    }
}