|
|
@@ -0,0 +1,293 @@
|
|
|
+package com.grkj.iscs.view.fragment
|
|
|
+
|
|
|
+import android.content.Context
|
|
|
+import android.content.res.ColorStateList
|
|
|
+import android.view.View
|
|
|
+import android.widget.TextView
|
|
|
+import androidx.core.content.ContextCompat
|
|
|
+import androidx.core.view.isVisible
|
|
|
+import androidx.lifecycle.ViewModelProvider
|
|
|
+import androidx.recyclerview.widget.RecyclerView
|
|
|
+import com.grkj.iscs.R
|
|
|
+import com.grkj.iscs.databinding.FragmentDeviceInputKeyAndLockBinding
|
|
|
+import com.grkj.iscs.extentions.setSelected
|
|
|
+import com.grkj.iscs.extentions.setVisibleWithHolder
|
|
|
+import com.grkj.iscs.modbus.ModBusController
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
|
|
|
+import com.grkj.iscs.model.bo.DockStatusBO
|
|
|
+import com.grkj.iscs.util.CommonUtils
|
|
|
+import com.grkj.iscs.util.SPUtils
|
|
|
+import com.grkj.iscs.view.base.BaseNavFragment
|
|
|
+import com.grkj.iscs.view.dialog.TipDialog
|
|
|
+import com.grkj.iscs.view.presenter.DeviceInputKeyAndLockViewModel
|
|
|
+import com.sik.sikcore.extension.setDebouncedClickListener
|
|
|
+import com.sik.sikcore.thread.ThreadUtils
|
|
|
+import com.zhy.adapter.recyclerview.CommonAdapter
|
|
|
+import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
|
|
|
+import com.zhy.adapter.recyclerview.base.ItemViewDelegate
|
|
|
+import com.zhy.adapter.recyclerview.base.ViewHolder
|
|
|
+import kotlinx.coroutines.Dispatchers
|
|
|
+import kotlinx.coroutines.delay
|
|
|
+import kotlinx.coroutines.withContext
|
|
|
+
|
|
|
+/**
|
|
|
+ * 新硬件录入页
|
|
|
+ */
|
|
|
+class DeviceInputKeyAndLockFragment :
|
|
|
+ BaseNavFragment<FragmentDeviceInputKeyAndLockBinding>() {
|
|
|
+ private val viewModel: DeviceInputKeyAndLockViewModel by lazy { ViewModelProvider(this)[DeviceInputKeyAndLockViewModel::class] }
|
|
|
+ private val tipDialog: TipDialog by lazy { TipDialog(requireContext()) }
|
|
|
+ private var mRowList = mutableListOf<DockStatusBO>()
|
|
|
+ private var isAlreadyInput: Boolean = false
|
|
|
+ override fun getLayoutId(): Int {
|
|
|
+ return R.layout.fragment_device_input_key_and_lock
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun initView() {
|
|
|
+ viewModel.loginUser = SPUtils.getLoginUser(requireContext())
|
|
|
+ viewModel.initData(mRowList)
|
|
|
+ binding.cbBack.setDebouncedClickListener {
|
|
|
+ navController.popBackStack()
|
|
|
+ }
|
|
|
+ binding.cbRescanOrInput.setDebouncedClickListener {
|
|
|
+ if (isAlreadyInput) {
|
|
|
+ mRowList.clear()
|
|
|
+ viewModel.initData(mRowList)
|
|
|
+ binding.scanTip.isVisible = mRowList.isEmpty()
|
|
|
+ binding.rvDock.isVisible = mRowList.isNotEmpty()
|
|
|
+ binding.cbRescanOrInput.isVisible = binding.rvDock.isVisible
|
|
|
+ isAlreadyInput = false
|
|
|
+ binding.cbRescanOrInput.setText(
|
|
|
+ if (isAlreadyInput) CommonUtils.getStr(R.string.rescan)
|
|
|
+ .toString() else CommonUtils.getStr(R.string.input_to_system).toString()
|
|
|
+ )
|
|
|
+ } else {
|
|
|
+ viewModel.deviceInputData(mRowList).observe(this) {
|
|
|
+ if (it.first) {
|
|
|
+ isAlreadyInput = true
|
|
|
+ binding.cbRescanOrInput.setText(
|
|
|
+ if (isAlreadyInput) CommonUtils.getStr(R.string.rescan)
|
|
|
+ .toString() else CommonUtils.getStr(R.string.input_to_system)
|
|
|
+ .toString()
|
|
|
+ )
|
|
|
+ tipDialog.setType(TipDialog.TYPE_CONFIRM)
|
|
|
+ tipDialog.setTip(
|
|
|
+ requireContext().getString(
|
|
|
+ R.string.already_input_device_tip,
|
|
|
+ it.second,
|
|
|
+ it.third
|
|
|
+ )
|
|
|
+ )
|
|
|
+ tipDialog.show()
|
|
|
+ } else {
|
|
|
+ tipDialog.setType(TipDialog.TYPE_ALL)
|
|
|
+ tipDialog.setTip(
|
|
|
+ requireContext().getString(R.string.input_device_error)
|
|
|
+ )
|
|
|
+ tipDialog.showCancelCountdown(10)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val adapter = MultiItemTypeAdapter(requireContext(), mRowList)
|
|
|
+ adapter.addItemViewDelegate(KeyDockItemDelegate(requireContext(), viewModel))
|
|
|
+ adapter.addItemViewDelegate(
|
|
|
+ LockDockItemDelegate(
|
|
|
+ requireContext(),
|
|
|
+ viewModel,
|
|
|
+ requireContext()
|
|
|
+ )
|
|
|
+ )
|
|
|
+ adapter.addItemViewDelegate(EmptyItemDelegate())
|
|
|
+ binding.rvDock?.adapter = adapter
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun onResume() {
|
|
|
+ super.onResume()
|
|
|
+ fun refreshAdapter() {
|
|
|
+ binding.scanTip.isVisible = mRowList.isEmpty()
|
|
|
+ binding.rvDock.isVisible = mRowList.isNotEmpty()
|
|
|
+ binding.cbRescanOrInput.isVisible = binding.rvDock.isVisible
|
|
|
+ binding.cbRescanOrInput.setText(
|
|
|
+ if (isAlreadyInput) CommonUtils.getStr(R.string.rescan)
|
|
|
+ .toString() else CommonUtils.getStr(R.string.input_to_system).toString()
|
|
|
+ )
|
|
|
+ ThreadUtils.runOnIO {
|
|
|
+ if (isResumed) {
|
|
|
+ withContext(Dispatchers.Main) {
|
|
|
+ binding.rvDock?.adapter?.notifyDataSetChanged()
|
|
|
+ }
|
|
|
+ delay(1000)
|
|
|
+ refreshAdapter()
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ refreshAdapter()
|
|
|
+ }
|
|
|
+
|
|
|
+ class KeyDockItemDelegate(
|
|
|
+ var context: Context,
|
|
|
+ var presenter: DeviceInputKeyAndLockViewModel?
|
|
|
+ ) :
|
|
|
+ ItemViewDelegate<DockStatusBO> {
|
|
|
+ private val statusCloseTintColor =
|
|
|
+ ContextCompat.getColor(context, R.color.common_status_red)
|
|
|
+ private val statusOpenTintColor =
|
|
|
+ ContextCompat.getColor(context, R.color.common_status_green)
|
|
|
+
|
|
|
+ override fun getItemViewLayoutId(): Int {
|
|
|
+ return R.layout.item_rv_key_dock_device_input
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun convert(holder: ViewHolder?, row: DockStatusBO, position: Int) {
|
|
|
+ holder?.setVisibleWithHolder(R.id.ll_left, row.dockList.any { it.column == "1" })
|
|
|
+ holder?.setVisibleWithHolder(R.id.ll_right, row.dockList.any { it.column == "2" })
|
|
|
+ holder?.setSelected(
|
|
|
+ R.id.iv_key_1,
|
|
|
+ ModBusController.isKeyExist(row.dockList.find { it.column == "1" }?.address, true)
|
|
|
+ )
|
|
|
+ holder?.setSelected(
|
|
|
+ R.id.iv_key_2,
|
|
|
+ ModBusController.isKeyExist(row.dockList.find { it.column == "1" }?.address, false)
|
|
|
+ )
|
|
|
+ holder?.setSelected(
|
|
|
+ R.id.iv_key_3,
|
|
|
+ ModBusController.isKeyExist(row.dockList.find { it.column == "2" }?.address, true)
|
|
|
+ )
|
|
|
+ holder?.setSelected(
|
|
|
+ R.id.iv_key_4,
|
|
|
+ ModBusController.isKeyExist(row.dockList.find { it.column == "2" }?.address, false)
|
|
|
+ )
|
|
|
+// holder?.getView<View>(R.id.v_buckle_status_1)?.backgroundTintList =
|
|
|
+// if (presenter?.getKeyBuckleLockEnabled(
|
|
|
+// row.dockList.find { it.column == "1" }?.address,
|
|
|
+// true
|
|
|
+// ) == true
|
|
|
+// ) ColorStateList.valueOf(statusCloseTintColor) else ColorStateList.valueOf(
|
|
|
+// statusOpenTintColor
|
|
|
+// )
|
|
|
+// holder?.getView<View>(R.id.v_buckle_status_2)?.backgroundTintList =
|
|
|
+// if (presenter?.getKeyBuckleLockEnabled(
|
|
|
+// row.dockList.find { it.column == "1" }?.address,
|
|
|
+// false
|
|
|
+// ) == true
|
|
|
+// ) ColorStateList.valueOf(statusCloseTintColor) else ColorStateList.valueOf(
|
|
|
+// statusOpenTintColor
|
|
|
+// )
|
|
|
+// holder?.getView<View>(R.id.v_buckle_status_3)?.backgroundTintList =
|
|
|
+// if (presenter?.getKeyBuckleLockEnabled(
|
|
|
+// row.dockList.find { it.column == "2" }?.address,
|
|
|
+// true
|
|
|
+// ) == true
|
|
|
+// ) ColorStateList.valueOf(statusCloseTintColor) else ColorStateList.valueOf(
|
|
|
+// statusOpenTintColor
|
|
|
+// )
|
|
|
+// holder?.getView<View>(R.id.v_buckle_status_4)?.backgroundTintList =
|
|
|
+// if (presenter?.getKeyBuckleLockEnabled(
|
|
|
+// row.dockList.find { it.column == "2" }?.address,
|
|
|
+// false
|
|
|
+// ) == true
|
|
|
+// ) ColorStateList.valueOf(statusCloseTintColor) else ColorStateList.valueOf(
|
|
|
+// statusOpenTintColor
|
|
|
+// )
|
|
|
+ holder?.getView<TextView>(R.id.tv_new_device_1)?.isVisible =
|
|
|
+ ModBusController.isKeyNewHardware(
|
|
|
+ row.dockList.find { it.column == "1" }?.address,
|
|
|
+ true
|
|
|
+ )
|
|
|
+ holder?.getView<TextView>(R.id.tv_new_device_2)?.isVisible =
|
|
|
+ ModBusController.isKeyNewHardware(
|
|
|
+ row.dockList.find { it.column == "1" }?.address,
|
|
|
+ false
|
|
|
+ )
|
|
|
+ holder?.getView<TextView>(R.id.tv_new_device_3)?.isVisible =
|
|
|
+ ModBusController.isKeyNewHardware(
|
|
|
+ row.dockList.find { it.column == "2" }?.address,
|
|
|
+ true
|
|
|
+ )
|
|
|
+ holder?.getView<TextView>(R.id.tv_new_device_4)?.isVisible =
|
|
|
+ ModBusController.isKeyNewHardware(
|
|
|
+ row.dockList.find { it.column == "2" }?.address,
|
|
|
+ false
|
|
|
+ )
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun isForViewType(item: DockStatusBO?, position: Int): Boolean {
|
|
|
+ return item?.dockList?.all { it.type == DOCK_TYPE_KEY } == true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class LockDockItemDelegate(
|
|
|
+ var context: Context,
|
|
|
+ var presenter: DeviceInputKeyAndLockViewModel?,
|
|
|
+ var ctx: Context
|
|
|
+ ) : ItemViewDelegate<DockStatusBO> {
|
|
|
+ private val statusCloseTintColor =
|
|
|
+ ContextCompat.getColor(context, R.color.common_status_red)
|
|
|
+ private val statusOpenTintColor =
|
|
|
+ ContextCompat.getColor(context, R.color.common_status_green)
|
|
|
+ private val statusNotLightTintColor =
|
|
|
+ ContextCompat.getColor(context, R.color.common_status_not_light)
|
|
|
+
|
|
|
+ override fun getItemViewLayoutId(): Int {
|
|
|
+ return R.layout.item_rv_lock_dock_status
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun convert(holder: ViewHolder?, row: DockStatusBO, position: Int) {
|
|
|
+ val rv = holder?.getView<RecyclerView>(R.id.rv_root)
|
|
|
+ rv?.adapter = object :
|
|
|
+ CommonAdapter<Int>(
|
|
|
+ ctx,
|
|
|
+ R.layout.item_rv_lock_dock_child_device_input,
|
|
|
+ row.dockList[0].deviceList
|
|
|
+ ) {
|
|
|
+ override fun convert(holder: ViewHolder?, lockIdx: Int, position: Int) {
|
|
|
+ holder?.setSelected(
|
|
|
+ R.id.root,
|
|
|
+ ModBusController.isLockExist(row.dockList[0].address, lockIdx)
|
|
|
+ )
|
|
|
+ holder?.getView<TextView>(R.id.tv_new_device)?.isVisible =
|
|
|
+ ModBusController.isLockNewHardware(
|
|
|
+ row.dockList[0].address, lockIdx
|
|
|
+ )
|
|
|
+ ColorStateList.valueOf(statusNotLightTintColor).let {
|
|
|
+ holder?.getView<View>(R.id.v_buckle_status_close)?.backgroundTintList = it
|
|
|
+ holder?.getView<View>(R.id.v_buckle_status_open)?.backgroundTintList = it
|
|
|
+ }
|
|
|
+// (presenter?.getLockBuckleLockEnabled(
|
|
|
+// row.dockList[0].address,
|
|
|
+// lockIdx
|
|
|
+// ) == true).let {
|
|
|
+// if (it) {
|
|
|
+// holder?.getView<View>(R.id.v_buckle_status_close)?.backgroundTintList =
|
|
|
+// ColorStateList.valueOf(statusCloseTintColor)
|
|
|
+// } else {
|
|
|
+// holder?.getView<View>(R.id.v_buckle_status_open)?.backgroundTintList =
|
|
|
+// ColorStateList.valueOf(statusOpenTintColor)
|
|
|
+// }
|
|
|
+// }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun isForViewType(item: DockStatusBO?, position: Int): Boolean {
|
|
|
+ return item?.dockList?.all { it.type == DOCK_TYPE_LOCK } == true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ class EmptyItemDelegate : ItemViewDelegate<DockStatusBO> {
|
|
|
+ override fun getItemViewLayoutId(): Int {
|
|
|
+ return R.layout.item_rv_empty_dock_status
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun convert(holder: ViewHolder?, row: DockStatusBO, position: Int) {
|
|
|
+ }
|
|
|
+
|
|
|
+ override fun isForViewType(item: DockStatusBO?, position: Int): Boolean {
|
|
|
+ return item?.dockList?.isEmpty() == true || item?.dockList?.none { it.type == DOCK_TYPE_KEY || it.type == DOCK_TYPE_LOCK } == true
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|