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))
|
}
|
|
}
|