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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package com.xw.yunwei.ui.work.car.manage
 
import android.annotation.SuppressLint
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.animation.DecelerateInterpolator
import android.widget.LinearLayout
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.LinearLayoutManager
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.customview.getCustomView
import com.amap.api.maps.model.LatLng
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.xw.common.CommonDialog
import com.xw.common.R
import com.xw.common.base.BaseActivity
import com.xw.common.base.OnItemClickListener
import com.xw.common.utils.BindUtils
import com.xw.common.utils.ToastUtilCommon
import com.xw.common.viewAdapter.ViewAdapter.Companion.showAnim
import com.xw.yunwei.BR
import com.xw.yunwei.databinding.ActivityCarManageBinding
import com.xw.yunwei.databinding.DialogCarFilterBinding
import com.xw.yunwei.model.CarModel
import com.xw.yunwei.ui.work.AMapFragment
import com.xw.yunwei.ui.work.car.detail.CarDetailActivity
 
/**
 * 车辆管理
 */
class CarManageActivity : BaseActivity<ActivityCarManageBinding, CarManageVM>(),
    OnItemClickListener<CarModel> {
    override fun getBindingVariable() = BR.viewModel
 
    val mapFrag: AMapFragment by lazy { AMapFragment() }
 
    val adapter by lazy {
        CarAdapter(this).apply {
            setHasStableIds(true)
            setEmptyView(BindUtils.getEmptyView(this@CarManageActivity, "暂无符合条件的车辆"))
//            setEmptyView(BindUtils.getEmptyView(this@CarManageActivity, "暂无搜索结果"))
        }
    }
 
    private var filterDialog: MaterialDialog? = null
 
    private val bottomSheetBehavior: BottomSheetBehavior<LinearLayout> by lazy {
        BottomSheetBehavior.from(
            binding.bottomSheet
        )
    }
 
    override fun init() {
        super.init()
        val transaction = supportFragmentManager.beginTransaction()
        transaction.add(com.xw.yunwei.R.id.map_container, mapFrag)
        transaction.commit()
        initAdapter()
        setBehavior()
    }
 
    override fun onResume() {
        super.onResume()
        viewModel.queryCars()
    }
 
    @SuppressLint("NotifyDataSetChanged")
    override fun setListener() {
        super.setListener()
        binding.filter.setOnClickListener {
            viewModel.queryAreas { showFilterDialog() }
        }
        binding.location.setOnClickListener { mapFrag.moveToLocation() }
        binding.searchEt.setOnKeyListener { _, keyCode, event ->
            if (keyCode == KeyEvent.KEYCODE_ENTER && event.action == KeyEvent.ACTION_UP) {
//                if (viewModel.search.value.isNullOrEmpty()) {
//                    ToastUtilCommon.show("请输入搜索内容")
//                } else {
                    viewModel.queryCars()
//                }
            }
            false
        }
        binding.arrow.setOnClickListener { toggleBottomSheetState() }
        mapFrag.locationChangeCallback = { location ->
            if (location == null) {
                CommonDialog.showTipsDialog(
                    this@CarManageActivity,
                    "您未开启gps权限,无法获取当前定位,请打开后重新操作",
                    null)
            } else {
                if (viewModel.fences.value.isNullOrEmpty()) {
                    val cityCode = location.adCode.substring(0, 4) + "00"
                    viewModel.getFenceByCode(cityCode)
                }
            }
        }
        mapFrag.markerClickCallback = { id ->
            adapter.data.find { "${it.id}" == id }?.let {
                adapter.remove(it)
                adapter.addData(0, it)
                adapter.selectItem = it
                adapter.notifyDataSetChanged()
            }
        }
    }
 
    override fun setObservable() {
        super.setObservable()
        viewModel.carList.observe(this) {
            if (it.size == 1 && !viewModel.search.value.isNullOrEmpty()) { // 搜索出只有一个 高亮
                adapter.selectItem = it[0]
                mapFrag.moveTo(it[0].latitude ?: 0.0, it[0].longitude ?: 0.0)
            } else {
                adapter.selectItem = null
            }
            adapter.setList(it)
            mapFrag.clearMarks()
            it.forEach {
                val resId = if (it.operatingStatus == 1) com.xw.yunwei.R.mipmap.ic_map_car_g else com.xw.yunwei.R.mipmap.ic_map_car_r
                mapFrag.drawCustomMarker(
                    LatLng(it.latitude ?: 0.0, it.longitude ?: 0.0),
                    resId,
                    "${it.id}",
                    it.plateNumber
                )
            }
        }
        viewModel.fences.observe(this) {
            val areas = mutableListOf<List<LatLng>>()
            it.forEach {
                val area = mutableListOf<LatLng>()
                it.points?.forEach {
                    area.add(LatLng(it.lat ?: 0.0, it.lng ?: 0.0))
                }
                if (area.isNotEmpty()) {
                    areas.add(area)
                }
            }
            if(areas.isNotEmpty()){
                mapFrag.drawPolygon(areas)
            }
        }
    }
 
    private fun setBehavior() {
        bottomSheetBehavior.apply {
            // 默认显示半展开状态(显示列表)
            state = BottomSheetBehavior.STATE_HALF_EXPANDED
 
            isHideable = false
            skipCollapsed = false  // 允许进入折叠状态
            halfExpandedRatio = 0.58f
 
            // 展开时距离顶部距离(留出状态栏和搜索区域)
            // expandedOffset = resources.getDimensionPixelSize(R.dimen.dp_50)
            expandedOffset = 0 // 顶部有定位图标占位 暂不需要设置
 
            addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
                override fun onStateChanged(bottomSheet: View, newState: Int) {
                    when (newState) {
                        BottomSheetBehavior.STATE_EXPANDED -> {
                            // 完全展开
                            showAnim(binding.arrow, false)
                            showAnim(binding.title1, true)
                            binding.location.visibility = View.INVISIBLE
                            arrow(0f)
                        }
 
                        BottomSheetBehavior.STATE_HALF_EXPANDED -> {
                            // 半展开 - 显示列表
                            if (!binding.arrow.isVisible) {
                                showAnim(binding.arrow, true)
                            }
                            if (binding.title1.isVisible) {
                                showAnim(binding.title1, false)
                            }
                            binding.location.visibility = View.VISIBLE
                            arrow(0f)
                        }
 
                        BottomSheetBehavior.STATE_COLLAPSED -> {
                            // 折叠 - 只显示指示器
                            if (!binding.arrow.isVisible) {
                                showAnim(binding.arrow, true)
                            }
                            if (binding.title1.isVisible) {
                                showAnim(binding.title1, false)
                            }
                            binding.location.visibility = View.VISIBLE
                            arrow(180f)
                        }
 
                        else -> {}
                    }
                }
 
                override fun onSlide(bottomSheet: View, slideOffset: Float) {
                    // 0 ~ 0.5: 从折叠到半展开
                    // 0.5 ~ 1: 从半展开到完全展开
//                    LogUtils.e(TAG, "onSlide: slideOffset = $slideOffset")
//                    if (slideOffset > 0.5f) { // 有点偏差
//                        if (binding.arrow.isVisible) {
//                            showAnim(binding.arrow, false)
//                        }
//                        if (!binding.title1.isVisible) {
//                            showAnim(binding.title1, true)
//                        }
//                    } else {
//                        if (!binding.arrow.isVisible) {
//                            showAnim(binding.arrow, true)
//                        }
//                        if (binding.title1.isVisible) {
//                            showAnim(binding.title1, false)
//                        }
//                        binding.arrow.rotation = 360f * (0.5f - slideOffset)
//                    }
                }
            })
        }
    }
 
    private fun toggleBottomSheetState() {
        when (bottomSheetBehavior.state) {
            BottomSheetBehavior.STATE_EXPANDED -> bottomSheetBehavior.state =
                BottomSheetBehavior.STATE_HALF_EXPANDED
 
            BottomSheetBehavior.STATE_HALF_EXPANDED -> bottomSheetBehavior.state =
                BottomSheetBehavior.STATE_COLLAPSED
 
            BottomSheetBehavior.STATE_COLLAPSED -> bottomSheetBehavior.state =
                BottomSheetBehavior.STATE_HALF_EXPANDED
 
            else -> bottomSheetBehavior.state = BottomSheetBehavior.STATE_HALF_EXPANDED
        }
    }
 
    private fun arrow(value: Float) {
        binding.arrow.animate()
            .rotation(value)
            .setDuration(300)
            .setInterpolator(DecelerateInterpolator())
            .start()
    }
 
    private fun initAdapter() {
        binding.carRv.adapter = adapter
        binding.carRv.layoutManager = LinearLayoutManager(this)
    }
 
    private fun showFilterDialog() {
        if (filterDialog == null) {
            filterDialog = MaterialDialog(this).apply {
                customView(
                    com.xw.yunwei.R.layout.dialog_car_filter,
                    noVerticalPadding = true,
                    horizontalPadding = false
                )
                cancelable(true)
                cornerRadius(12f)
 
                val bind: DialogCarFilterBinding? = DataBindingUtil.bind(getCustomView())
 
                with(bind!!) {
 
                    bind.stllArea.remove()
                    bind.stllStatus.remove()
 
                    bind.stllArea.setData(
                        viewModel.filterAreas.map { it.electronicName?:"" },
                        this@CarManageActivity,
                        ArrayList(),
                        this@CarManageActivity.resources.getColor(R.color.blue_5),
                        "shape_blue1_bg_4"
                    )
                    bind.stllStatus.setData(
                        viewModel.filterStatus,
                        this@CarManageActivity,
                        arrayListOf("0"),
                        this@CarManageActivity.resources.getColor(R.color.blue_5),
                        "shape_blue1_bg_4"
                    )
 
                    bind.stllArea.setListener {
//                        val index = it.tag as Int // 当前点击的下标
//                        if (bind.stllArea.selectIndexList.contains(index)) {
//                            viewModel.selectArea.remove(viewModel.filterAreas[index])
//                        } else {
//                            viewModel.selectArea.add(viewModel.filterAreas[index])
//                        }
//                        bind.stllArea.onTvClick(index)
                        // 做的时候说多选  做完后又改单选 无语
                        for (index in bind.stllArea.selectIndexList) { // 先取消选中状态
                            bind.stllArea.onTvClick(index)
                        }
                        val index = it.tag as Int // 当前点击的下标
                        bind.stllArea.onTvClick(index)
                        viewModel.selectArea.clear()
                        viewModel.selectArea.add(viewModel.filterAreas[index])
                    }
 
                    bind.stllStatus.setListener {
//                        val index = it.tag as Int // 当前点击的下标
//                        if (index == 0) { // 选择全部 取消其他
//                            val needRemove = mutableListOf<Int>()
//                            for (index in bind.stllStatus.selectIndexList) { // 先取消选中状态
//                                needRemove.add(index)
//                            }
//                            needRemove.forEach { bind.stllStatus.onTvClick(it) }
//                            viewModel.selectStatus.clear()
//
//                            bind.stllStatus.onTvClick(index)
//                        } else {
//                            if (bind.stllStatus.selectIndexList.contains(0)) {
//                                bind.stllStatus.onTvClick(0)
//                                viewModel.selectStatus.clear()
//                            }
//                            if (bind.stllStatus.selectIndexList.contains(index)) {
//                                viewModel.selectStatus.remove(viewModel.filterStatus[index])
//                            } else {
//                                viewModel.selectStatus.add(viewModel.filterStatus[index])
//                            }
//                            bind.stllStatus.onTvClick(index)
//                        }
                        // 做的时候说多选  做完后又改单选 无语
                        for (index in bind.stllStatus.selectIndexList) { // 先取消选中状态
                            bind.stllStatus.onTvClick(index)
                        }
                        val index = it.tag as Int // 当前点击的下标
                        bind.stllStatus.onTvClick(index)
                        viewModel.selectStatus.clear()
                        viewModel.selectStatus.add(viewModel.filterStatus[index])
                    }
 
                    bind.close.setOnClickListener { dismiss() }
                    bind.btnLeft.setOnClickListener {
                        val odlIndex = mutableListOf<Int>()
                        for (index in bind.stllArea.selectIndexList) { // 先取消选中状态
                            odlIndex.add(index)
                        }
                        odlIndex.forEach { bind.stllArea.onTvClick(it) }
                        viewModel.selectArea.clear()
 
                        odlIndex.clear()
                        for (index in bind.stllStatus.selectIndexList) { // 先取消选中状态
                            odlIndex.add(index)
                        }
                        odlIndex.forEach { bind.stllStatus.onTvClick(it) }
                        viewModel.selectStatus.clear()
 
                        bind.stllStatus.onTvClick(0)
                        viewModel.selectStatus.add(viewModel.filterStatus[0])
                    }
                    bind.btnRight.setOnClickListener {
                        viewModel.queryCars()
                        dismiss()
                    }
                }
            }
        }
 
        filterDialog?.show()
        // 关键:让软键盘把 BottomSheet 顶上去
        filterDialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        filterDialog?.window?.setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
        filterDialog?.window?.setGravity(Gravity.BOTTOM)
        filterDialog?.window?.setWindowAnimations(R.style.AnimUp)
 
    }
 
 
    override fun onItemClick(item: CarModel) {
        startActivity(CarDetailActivity::class.java, bundleOf("carId" to item.id))
    }
 
}