Просмотр исходного кода

优化底座测试页显示;完成系统设置页主板设置部分

Frankensteinly 9 месяцев назад
Родитель
Сommit
e22bc043bb

+ 1 - 0
app/src/main/java/com/grkj/iscs/model/DeviceConst.kt

@@ -8,6 +8,7 @@ object DeviceConst {
     const val DOCK_TYPE_LOCK = 0x01.toByte()                // 锁具底座
     const val DOCK_TYPE_ELEC_LOCK_BOARD = 0x02.toByte()     // 电磁锁控制板
     const val DOCK_TYPE_PORTABLE = 0x03.toByte()            // 便携式底座
+    const val DOCK_TYPE_COLLECT = 0x04.toByte()             // 采集主板
 
     /***********************  底座设备类型  ***********************/
 

+ 7 - 0
app/src/main/java/com/grkj/iscs/util/CommonUtils.kt

@@ -15,6 +15,9 @@ import java.text.SimpleDateFormat
 import java.util.Locale
 
 object CommonUtils {
+
+    val hexRegex = "^[0-9A-Fa-f]+$".toRegex()
+
     fun dip2px(dpValue: Float): Int {
         val density = MyApplication.instance!!.resources.displayMetrics.density
         return (dpValue * density + 0.5f).toInt()
@@ -70,4 +73,8 @@ object CommonUtils {
         }
         return null
     }
+
+    fun isValidHex(hexString: String): Boolean {
+        return hexRegex.matches(hexString)
+    }
 }

+ 4 - 0
app/src/main/java/com/grkj/iscs/util/Funcs.kt

@@ -86,4 +86,8 @@ fun passwordStyle(view: EditText?, imageView: ImageView) {
     }
     view?.setSelection(view.text.toString().length)
     imageView.isSelected = !imageView.isSelected
+}
+
+fun byteToHexString(byte: Byte): String {
+    return "0x%02X".format(byte)
 }

+ 3 - 3
app/src/main/java/com/grkj/iscs/util/SPUtils.kt

@@ -64,7 +64,7 @@ object SPUtils {
         cardInfoRespVO.roleKeyList?.let {
             edit.putString(KEY_LOGIN_USER_ROLE_KEY, it.toString().replace("[", "").replace("]", ""))
         }
-        edit.apply()
+        edit.commit()
     }
 
     fun clearLoginUser(context: Context) : Boolean {
@@ -83,7 +83,7 @@ object SPUtils {
         val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE)
         val edit = sp.edit()
         edit.putString(KEY_DOCK_CONFIG, config)
-        edit.apply()
+        edit.commit()
     }
 
     fun getDockConfig(context: Context): String? {
@@ -95,7 +95,7 @@ object SPUtils {
         val sp = context.getSharedPreferences(SP_CONFIG_NAME, Context.MODE_PRIVATE)
         val edit = sp.edit()
         edit.putString(KEY_PORT_CONFIG, config)
-        edit.apply()
+        edit.commit()
     }
 
     fun getPortConfig(context: Context): String? {

+ 7 - 0
app/src/main/java/com/grkj/iscs/view/fragment/DockTestFragment.kt

@@ -1,5 +1,6 @@
 package com.grkj.iscs.view.fragment
 
+import android.view.View
 import com.google.gson.Gson
 import com.google.gson.reflect.TypeToken
 import com.grkj.iscs.R
@@ -32,6 +33,12 @@ class DockTestFragment : BaseFragment<FragmentDockTestBinding>() {
                 mKeyDockList.addAll(tempList.filter { it.type == DOCK_TYPE_KEY })
                 mLockDockList.addAll(tempList.filter { it.type == DOCK_TYPE_LOCK })
             }
+            if (mKeyDockList.isEmpty()) {
+                mBinding?.tvKeyTitle?.visibility = View.GONE
+            }
+            if (mLockDockList.isEmpty()) {
+                mBinding?.tvLockTitle?.visibility = View.GONE
+            }
             mKeyDockList.forEach { dock ->
                 dock.deviceList = mutableListOf(1, 2)
             }

+ 129 - 0
app/src/main/java/com/grkj/iscs/view/fragment/SystemSettingFragment.kt

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

+ 3 - 1
app/src/main/res/layout/fragment_dock_test.xml

@@ -31,7 +31,7 @@
         android:layout_above="@id/cb_close"
         android:layout_marginBottom="@dimen/common_spacing"
         android:background="@drawable/item_rv_technology_sop_bg_normal"
-        android:padding="5dp">
+        android:padding="@dimen/common_spacing_small">
 
         <LinearLayout
             android:layout_width="match_parent"
@@ -39,6 +39,7 @@
             android:orientation="vertical">
 
             <TextView
+                android:id="@+id/tv_key_title"
                 style="@style/CommonTextView"
                 android:layout_margin="@dimen/common_spacing_small"
                 android:text="@string/key_dock" />
@@ -48,6 +49,7 @@
                 style="@style/CommonRecyclerView" />
 
             <TextView
+                android:id="@+id/tv_lock_title"
                 style="@style/CommonTextView"
                 android:layout_margin="@dimen/common_spacing_small"
                 android:text="@string/lock_dock" />

+ 97 - 2
app/src/main/res/layout/fragment_system_setting.xml

@@ -1,8 +1,103 @@
 <?xml version="1.0" encoding="utf-8"?>
-<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
+    android:background="@drawable/item_rv_technology_sop_bg_normal"
+    android:orientation="horizontal"
+    android:padding="@dimen/common_spacing_small"
     tools:context=".view.fragment.SystemSettingFragment">
 
-</RelativeLayout>
+    <!-- 主板设置 -->
+    <LinearLayout
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:orientation="vertical">
+
+        <TextView
+            style="@style/CommonTextView"
+            android:text="@string/dock_config"
+            android:textSize="@dimen/common_text_size_big" />
+
+        <LinearLayout
+            android:layout_width="match_parent"
+            android:layout_height="wrap_content"
+            android:layout_marginVertical="@dimen/common_spacing_small"
+            android:gravity="center_vertical"
+            android:orientation="horizontal">
+
+            <TextView
+                style="@style/CommonTextView"
+                android:text="@string/type" />
+
+            <RelativeLayout
+                android:id="@+id/rl_spinner"
+                android:layout_width="100dp"
+                android:layout_height="wrap_content"
+                android:layout_marginHorizontal="@dimen/common_spacing_small"
+                android:background="@drawable/item_rv_technology_sop_bg_normal"
+                android:padding="@dimen/common_spacing_small">
+
+                <TextView
+                    android:id="@+id/tv_board_type"
+                    style="@style/CommonTextView"
+                    android:layout_centerVertical="true" />
+
+                <ImageView
+                    android:layout_width="@dimen/common_icon_size"
+                    android:layout_height="@dimen/common_icon_size"
+                    android:layout_alignParentRight="true"
+                    android:layout_centerVertical="true"
+                    android:rotation="90"
+                    android:src="@mipmap/arrow" />
+            </RelativeLayout>
+
+            <TextView
+                style="@style/CommonTextView"
+                android:text="0x" />
+
+            <EditText
+                android:id="@+id/et_address"
+                style="@style/CommonEdit"
+                android:layout_width="30dp"
+                android:layout_marginLeft="@dimen/common_spacing_small"
+                android:gravity="center"
+                android:maxLength="2"/>
+
+            <TextView
+                android:id="@+id/tv_add"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:layout_marginLeft="@dimen/common_spacing_small"
+                android:background="@drawable/common_btn_bg"
+                android:gravity="center"
+                android:paddingHorizontal="@dimen/common_spacing_big"
+                android:paddingVertical="@dimen/common_text_padding"
+                android:text="@string/add"
+                android:textColor="@color/white"
+                android:textSize="@dimen/common_text_size" />
+        </LinearLayout>
+
+        <include layout="@layout/item_rv_board" />
+
+        <androidx.recyclerview.widget.RecyclerView
+            android:id="@+id/rv_board"
+            style="@style/CommonRecyclerView"
+            android:layout_height="wrap_content" />
+
+    </LinearLayout>
+
+    <!-- 串口地址设置 -->
+    <LinearLayout
+        android:layout_width="0dp"
+        android:layout_height="match_parent"
+        android:layout_weight="1"
+        android:orientation="vertical">
+
+        <TextView
+            style="@style/CommonTextView"
+            android:text="@string/port_address_config"
+            android:textSize="@dimen/common_text_size_big" />
+    </LinearLayout>
+</LinearLayout>

+ 52 - 0
app/src/main/res/layout/item_rv_board.xml

@@ -0,0 +1,52 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:id="@+id/root"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:orientation="vertical">
+
+    <LinearLayout
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:gravity="center_vertical"
+        android:orientation="horizontal">
+
+        <TextView
+            android:id="@+id/tv_board"
+            style="@style/CommonTextView"
+            android:layout_width="0dp"
+            android:layout_weight="2"
+            android:text="@string/type" />
+
+        <View
+            android:layout_width="@dimen/divider_line_width"
+            android:layout_height="match_parent"
+            android:layout_marginVertical="@dimen/common_spacing_small"
+            android:background="@color/common_bg_white_30" />
+
+        <TextView
+            android:id="@+id/tv_address"
+            style="@style/CommonTextView"
+            android:layout_width="0dp"
+            android:layout_weight="1"
+            android:text="@string/address" />
+
+        <View
+            android:layout_width="@dimen/divider_line_width"
+            android:layout_height="match_parent"
+            android:layout_marginVertical="@dimen/common_spacing_small"
+            android:background="@color/common_bg_white_30" />
+
+        <TextView
+            android:id="@+id/tv_action"
+            style="@style/CommonTextView"
+            android:layout_width="0dp"
+            android:layout_weight="1"
+            android:text="@string/action" />
+    </LinearLayout>
+
+    <View
+        android:layout_width="match_parent"
+        android:layout_height="@dimen/divider_line_width"
+        android:background="@color/common_bg_white_30" />
+</LinearLayout>

+ 15 - 1
app/src/main/res/values/strings.xml

@@ -174,7 +174,7 @@
     <string name="not_involved_in_ticket">您不参与该工票</string>
     <string name="you_are_not_locker_tip">您不是上锁人,无法执行此操作</string>
     <string name="open_all_docks">全仓开</string>
-    <string name="close_all_docks">全仓关</string>\
+    <string name="close_all_docks">全仓关</string>
     <string name="key_dock">钥匙仓</string>
     <string name="lock_dock">锁仓</string>
     <string name="address">地址</string>
@@ -183,4 +183,18 @@
     <string name="system_setting">系统设置</string>
     <string name="turn_on">开</string>
     <string name="turn_off">关</string>
+    <string name="dock_config">主板设置</string>
+    <string name="port_address_config">串口地址设置</string>
+    <string name="type">类型</string>
+    <string name="add">添加</string>
+    <string name="board_type_key">钥匙主板</string>
+    <string name="board_type_lock">挂锁主板</string>
+    <string name="board_type_portal">一体机主板</string>
+    <string name="board_type_collect">采集主板</string>
+    <string name="board_type_elec_lock">电磁锁控制主板</string>
+    <string name="action">操作</string>
+    <string name="delete">删除</string>
+    <string name="please_select_board_type">请选择主板类型</string>
+    <string name="please_input_address">请输入地址</string>
+    <string name="please_input_valid_address">请输入合法地址</string>
 </resources>