|
|
@@ -1,17 +1,146 @@
|
|
|
package com.grkj.iscs.view.fragment
|
|
|
|
|
|
+import android.view.LayoutInflater
|
|
|
+import android.view.View
|
|
|
+import android.view.ViewGroup
|
|
|
+import android.widget.PopupWindow
|
|
|
+import com.google.gson.Gson
|
|
|
+import com.google.gson.reflect.TypeToken
|
|
|
+import com.grkj.iscs.R
|
|
|
import com.grkj.iscs.databinding.FragmentSystemSettingBinding
|
|
|
+import com.grkj.iscs.databinding.LayoutSelectableinputSpinnerBinding
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_COLLECT
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_ELEC_LOCK_BOARD
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_KEY
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_LOCK
|
|
|
+import com.grkj.iscs.model.DeviceConst.DOCK_TYPE_PORTABLE
|
|
|
+import com.grkj.iscs.util.CommonUtils
|
|
|
+import com.grkj.iscs.util.SPUtils
|
|
|
+import com.grkj.iscs.util.ToastUtils
|
|
|
+import com.grkj.iscs.util.byteToHexString
|
|
|
import com.grkj.iscs.view.base.BaseFragment
|
|
|
+import com.zhy.adapter.recyclerview.CommonAdapter
|
|
|
+import com.zhy.adapter.recyclerview.base.ViewHolder
|
|
|
|
|
|
/**
|
|
|
* 系统设置页
|
|
|
*/
|
|
|
class SystemSettingFragment : BaseFragment<FragmentSystemSettingBinding>() {
|
|
|
|
|
|
+ private var mDockTypeList = mutableListOf<DockItem>()
|
|
|
+ private var mDropdownView: View? = null
|
|
|
+ private var mPopWindow: PopupWindow? = null
|
|
|
+ private var mDockList = mutableListOf<DockTestFragment.DockTestBean>()
|
|
|
+ private var mSelectedBoardType: Byte? = null
|
|
|
+
|
|
|
override val viewBinding: FragmentSystemSettingBinding
|
|
|
get() = FragmentSystemSettingBinding.inflate(layoutInflater)
|
|
|
|
|
|
override fun initView() {
|
|
|
+ val dockConfigJson = SPUtils.getDockConfig(requireActivity())
|
|
|
+ if (!dockConfigJson.isNullOrEmpty()) {
|
|
|
+ val tempList: MutableList<DockTestFragment.DockTestBean> =
|
|
|
+ Gson().fromJson(
|
|
|
+ dockConfigJson,
|
|
|
+ object : TypeToken<MutableList<DockTestFragment.DockTestBean>>() {}.type
|
|
|
+ )
|
|
|
+ if (tempList.isNotEmpty()) {
|
|
|
+ mDockList.addAll(tempList)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mDockTypeList.addAll(
|
|
|
+ mutableListOf(
|
|
|
+ DockItem(getString(R.string.board_type_key), DOCK_TYPE_KEY),
|
|
|
+ DockItem(getString(R.string.board_type_lock), DOCK_TYPE_LOCK),
|
|
|
+ DockItem(getString(R.string.board_type_portal), DOCK_TYPE_PORTABLE),
|
|
|
+ DockItem(getString(R.string.board_type_collect), DOCK_TYPE_COLLECT),
|
|
|
+ DockItem(getString(R.string.board_type_elec_lock), DOCK_TYPE_ELEC_LOCK_BOARD)
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ mBinding?.rlSpinner?.setOnClickListener {
|
|
|
+ showBoardTypeDropDown()
|
|
|
+ }
|
|
|
+
|
|
|
+ mBinding?.tvAdd?.setOnClickListener {
|
|
|
+ addBoard()
|
|
|
+ }
|
|
|
+
|
|
|
+ mBinding?.rvBoard?.adapter = object : CommonAdapter<DockTestFragment.DockTestBean>(
|
|
|
+ requireActivity(),
|
|
|
+ R.layout.item_rv_board,
|
|
|
+ mDockList
|
|
|
+ ) {
|
|
|
+ override fun convert(holder: ViewHolder, t: DockTestFragment.DockTestBean, position: Int) {
|
|
|
+ mDockTypeList.find { it.type == t.type }?.name?.let {
|
|
|
+ holder.setText(R.id.tv_board, it)
|
|
|
+ }?: {
|
|
|
+ holder.setText(R.id.tv_board, "")
|
|
|
+ }
|
|
|
+ holder.setText(R.id.tv_address, byteToHexString(t.address))
|
|
|
+ holder.setText(R.id.tv_action, getString(R.string.delete))
|
|
|
+ holder.setOnClickListener(R.id.tv_action) {
|
|
|
+ mDockList.removeAt(position)
|
|
|
+ mBinding?.rvBoard?.adapter?.notifyDataSetChanged()
|
|
|
+ SPUtils.saveDockConfig(requireActivity(), Gson().toJson(mDockList))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
+ private fun showBoardTypeDropDown() {
|
|
|
+ mDropdownView ?: let {
|
|
|
+ mDropdownView = LayoutInflater.from(requireActivity())
|
|
|
+ .inflate(R.layout.layout_selectableinput_spinner, null)
|
|
|
+ }
|
|
|
+
|
|
|
+ mPopWindow ?: let {
|
|
|
+ mPopWindow = PopupWindow(context)
|
|
|
+ mPopWindow?.contentView = mDropdownView
|
|
|
+ mPopWindow?.width = mBinding?.rlSpinner!!.width
|
|
|
+ mPopWindow?.height = ViewGroup.LayoutParams.WRAP_CONTENT
|
|
|
+ mPopWindow?.isOutsideTouchable = true
|
|
|
+ mPopWindow?.isFocusable = true
|
|
|
+
|
|
|
+ val popBinding = LayoutSelectableinputSpinnerBinding.bind(mDropdownView!!)
|
|
|
+ popBinding.rvOptions.adapter = object : CommonAdapter<DockItem>(
|
|
|
+ requireActivity(),
|
|
|
+ R.layout.item_rv_selectableinput_spinner,
|
|
|
+ mDockTypeList
|
|
|
+ ) {
|
|
|
+ override fun convert(holder: ViewHolder?, option: DockItem?, position: Int) {
|
|
|
+ holder?.setText(R.id.tv_option, option?.name)
|
|
|
+ holder?.setOnClickListener(R.id.tv_option) {
|
|
|
+ mBinding?.tvBoardType?.text = option?.name
|
|
|
+ mPopWindow?.dismiss()
|
|
|
+ mSelectedBoardType = option?.type
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ mPopWindow?.showAsDropDown(mBinding?.rlSpinner)
|
|
|
}
|
|
|
+
|
|
|
+ private fun addBoard() {
|
|
|
+ val address = mBinding?.etAddress?.text.toString()
|
|
|
+ if (mSelectedBoardType == null) {
|
|
|
+ ToastUtils.tip(R.string.please_select_board_type)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (address.isEmpty()) {
|
|
|
+ ToastUtils.tip(R.string.please_input_address)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (!CommonUtils.isValidHex(address)) {
|
|
|
+ ToastUtils.tip(R.string.please_input_valid_address)
|
|
|
+ return
|
|
|
+ }
|
|
|
+ mDockList.add(DockTestFragment.DockTestBean(address.toInt(16).toByte(), mSelectedBoardType!!))
|
|
|
+ mBinding?.rvBoard?.adapter?.notifyDataSetChanged()
|
|
|
+ SPUtils.saveDockConfig(requireActivity(), Gson().toJson(mDockList))
|
|
|
+ }
|
|
|
+
|
|
|
+ private data class DockItem(val name: String, val type: Byte)
|
|
|
}
|