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(), GpsHelper.OnMapLocation { override fun getBindingVariable() = BR.viewModel lateinit var imageEvent: ImageEventListener 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() { override fun viewImage(item: FileSelectBean) { val imgList: MutableList = 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?) { 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() } } }