317 lines
13 KiB
Kotlin
317 lines
13 KiB
Kotlin
//package com.sw.platecabinet.fragment
|
|
//
|
|
//import android.annotation.SuppressLint
|
|
//import android.os.Bundle
|
|
//import android.text.Editable
|
|
//import android.text.TextUtils
|
|
//import android.text.TextWatcher
|
|
//import android.view.inputmethod.EditorInfo
|
|
//import androidx.fragment.app.viewModels
|
|
//import androidx.lifecycle.lifecycleScope
|
|
//import androidx.recyclerview.widget.GridLayoutManager
|
|
//import androidx.recyclerview.widget.LinearLayoutManager
|
|
//import androidx.recyclerview.widget.RecyclerView
|
|
//import com.sw.plate.utils.ToastUtils
|
|
//import com.sw.plate.utils.comn.SerialApi
|
|
//import com.sw.plate.utils.comn.SerialPortManager
|
|
//import com.sw.platecabinet.R
|
|
//import com.sw.platecabinet.activity.MainActivity
|
|
//import com.sw.platecabinet.activity.PageType
|
|
//import com.sw.platecabinet.adapter.GenericItemAdapter
|
|
//import com.sw.platecabinet.adapter.GridSpacingItemDecoration
|
|
//import com.sw.platecabinet.adapter.dpToPx
|
|
//import com.sw.platecabinet.databinding.FragmentBindPlateBinding
|
|
//import com.sw.platecabinet.databinding.ItemSearchUserInfoBinding
|
|
//import com.sw.platecabinet.dialog.UserBindRemindDialog
|
|
//import com.sw.platecabinet.ext.maskName
|
|
//import com.sw.platecabinet.ext.maskPhone
|
|
//import com.sw.platecabinet.model.ErrorInfo
|
|
//import com.sw.platecabinet.model.request.BindParam
|
|
//import com.sw.platecabinet.model.response.EquipmentUserInfo
|
|
//import com.sw.platecabinet.model.response.SearchResult
|
|
//import com.sw.platecabinet.utils.Debouncer
|
|
//import com.sw.platecabinet.utils.KeyboardUtils
|
|
//import com.sw.platecabinet.viewmodel.SettingViewModel
|
|
//import com.sw.platecabinet.viewmodel.UserViewModel
|
|
//import kotlinx.coroutines.launch
|
|
//import timber.log.Timber
|
|
//
|
|
///**
|
|
// * 餐盘柜绑定
|
|
// */
|
|
//class BindPlateFragment : BaseFragment<FragmentBindPlateBinding>(
|
|
// FragmentBindPlateBinding::inflate
|
|
//) {
|
|
// private lateinit var adapter: GenericItemAdapter<SearchResult.Member, ItemSearchUserInfoBinding>
|
|
// internal val KEY_ACTION_TYPE = "actionType"
|
|
// private var info: EquipmentUserInfo? = null
|
|
// private var checkedItem: SearchResult.Member? = null
|
|
// private var lastText = ""
|
|
// private val debouncer = Debouncer(2000)
|
|
//
|
|
// private val userViewModel by viewModels<UserViewModel>()
|
|
//
|
|
// companion object {
|
|
// @JvmStatic
|
|
// fun newInstance(userInfo: EquipmentUserInfo?) =
|
|
// BindPlateFragment().apply {
|
|
// arguments = Bundle().apply {
|
|
// putParcelable(KEY_ACTION_TYPE, userInfo)
|
|
// }
|
|
// }
|
|
// }
|
|
//
|
|
// @SuppressLint("SetTextI18n")
|
|
// override fun initialize() {
|
|
// arguments?.let { args ->
|
|
// info = args.getParcelable(KEY_ACTION_TYPE)
|
|
// info?.let {
|
|
// binding.tvNum.text = "${it.equipmentName}-${it.equipmentBoxCode}"
|
|
// if (!TextUtils.isEmpty(it.plateNumber)) {
|
|
//// binding.etCode.text = Editable.Factory.getInstance().newEditable(it.plateNumber)
|
|
// binding.etCode.apply {
|
|
// setText(it.plateNumber)
|
|
// setSelection(length())
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// initView()
|
|
// initListener()
|
|
// }
|
|
//
|
|
// private fun initView() {
|
|
// adapter = createAdapter()
|
|
// binding.recyclerview.layoutManager = GridLayoutManager(context, 2)
|
|
// // 添加间距装饰(12dp)
|
|
// binding.recyclerview.addItemDecoration(
|
|
// GridSpacingItemDecoration(
|
|
// spanCount = 2,
|
|
// spacing = requireContext().dpToPx(6),
|
|
// includeEdge = false // 包含边缘间距
|
|
// )
|
|
// )
|
|
// binding.recyclerview.adapter = adapter
|
|
// binding.recyclerview.addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
|
// override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
|
|
// super.onScrolled(recyclerView, dx, dy)
|
|
// val layoutManager = recyclerView.layoutManager as LinearLayoutManager
|
|
// val lastVisibleItem = layoutManager.findLastVisibleItemPosition()
|
|
// val totalItems = layoutManager.itemCount
|
|
//
|
|
// if (!viewModel.isLoading && viewModel.canLoadMore
|
|
// && lastVisibleItem >= totalItems - 3
|
|
// ) {
|
|
// viewModel.getSearchMemberList(
|
|
// param = lastText,
|
|
// pageNum = viewModel.currentPage
|
|
// )
|
|
// }
|
|
// }
|
|
// })
|
|
// (activity as MainActivity).registerKeyEventInfo {
|
|
//// binding.etCode.text.clear()
|
|
//// binding.etCode.postDelayed({
|
|
//// binding.etCode.text.clear()
|
|
//// binding.etCode.text = Editable.Factory.getInstance().newEditable(it)
|
|
// activity?.runOnUiThread {
|
|
// if (binding.etUserInfo.isFocused) {
|
|
// return@runOnUiThread
|
|
// }
|
|
// binding.etCode.apply {
|
|
// if (it.length > 6) {
|
|
// setText(it.substring(0, 6))
|
|
// } else {
|
|
// setText(it)
|
|
// }
|
|
// if (length() > 0) {
|
|
// setSelection(length())
|
|
// }
|
|
// }
|
|
// binding.etUserInfo.requestFocus()
|
|
// }
|
|
//// }, 50)
|
|
// }
|
|
// }
|
|
//
|
|
// private fun initListener() {
|
|
// binding.etCode.addTextChangedListener(object : TextWatcher {
|
|
// override fun beforeTextChanged(
|
|
// s: CharSequence?,
|
|
// start: Int,
|
|
// count: Int,
|
|
// after: Int
|
|
// ) {
|
|
// Timber.d("beforeTextChanged s = ${s.toString()}, start = $start, count = $count, after = $after")
|
|
// }
|
|
//
|
|
// override fun onTextChanged(
|
|
// s: CharSequence?,
|
|
// start: Int,
|
|
// before: Int,
|
|
// count: Int
|
|
// ) {
|
|
// Timber.d("onTextChanged s = ${s.toString()}, start = $start, count = $count, before = $before")
|
|
// }
|
|
//
|
|
// override fun afterTextChanged(s: Editable?) {
|
|
// Timber.d("afterTextChanged s = ${s.toString()}")
|
|
// }
|
|
// })
|
|
// binding.etUserInfo.setOnEditorActionListener { _, actionId, keyEvent ->
|
|
// if (actionId == EditorInfo.IME_ACTION_SEARCH) {
|
|
// val currentText = binding.etUserInfo.text.toString()
|
|
// val containsNewlines = currentText.contains('\n') || currentText.contains('\r')
|
|
// if (containsNewlines) {
|
|
// Timber.d("非用户主动搜索")
|
|
// true
|
|
// }
|
|
// debouncer.debounce {
|
|
// // 执行搜索操作
|
|
// viewModel.getSearchMemberList(param = currentText, pageNum = 1)
|
|
// // 隐藏键盘
|
|
// KeyboardUtils.hideKeyboard(requireActivity())
|
|
// }
|
|
// true // 表示已处理该事件
|
|
// } else {
|
|
// false // 未处理其他动作
|
|
// }
|
|
// }
|
|
// binding.ivSearch.setOnClickListener {
|
|
// debouncer.debounce {
|
|
// // 执行搜索操作
|
|
// viewModel.getSearchMemberList(
|
|
// param = binding.etUserInfo.text.toString(),
|
|
// pageNum = 1
|
|
// )
|
|
// // 隐藏键盘
|
|
// KeyboardUtils.hideKeyboard(requireActivity())
|
|
// }
|
|
// }
|
|
// binding.llBind.setOnClickListener {
|
|
// if (equipmentBoxCode.isNullOrBlank().not()) {
|
|
// showBindDialog()
|
|
// return@setOnClickListener
|
|
// }
|
|
// if (info == null || info!!.equipmentCode?.isEmpty() == true || info!!.equipmentBoxCode?.isEmpty() == true) {
|
|
// ToastUtils.showToast("传入的设备信息异常")
|
|
// return@setOnClickListener
|
|
// }
|
|
// val plateNumber = binding.etCode.text.toString()
|
|
// if (plateNumber.isEmpty()) {
|
|
// ToastUtils.showToast("请输入餐盘号")
|
|
// return@setOnClickListener
|
|
// }
|
|
// if (checkedItem == null) {
|
|
// ToastUtils.showToast("请选择要绑定的会员")
|
|
// return@setOnClickListener
|
|
// }
|
|
// if (checkedItem!!.id == null) {
|
|
// ToastUtils.showToast("选中的会员信息异常")
|
|
// return@setOnClickListener
|
|
// }
|
|
// val bindParam = BindParam(
|
|
// equipmentId = info?.equipmentId,
|
|
// equipmentCode = info?.equipmentCode,
|
|
// equipmentBoxCode = info?.equipmentBoxCode,
|
|
// faceId = checkedItem?.faceId,
|
|
// plateNumber = plateNumber
|
|
// )
|
|
// viewModel.bindPlate(bindParam) { pair ->
|
|
// updateBindState(pair)
|
|
// }
|
|
// KeyboardUtils.hideKeyboard(requireActivity())
|
|
// }
|
|
// binding.tvBack.setOnClickListener { activity?.finish() }
|
|
// }
|
|
//
|
|
// /**
|
|
// * 监听数据变化
|
|
// */
|
|
// override fun registerDataChange() {
|
|
// super.registerDataChange()
|
|
// lifecycleScope.launch {
|
|
// viewModel.searchMemberList.collect {
|
|
// adapter.updateData(it)
|
|
// }
|
|
// }
|
|
//// lifecycleScope.launch {
|
|
//// viewModel.bindStateChange.collect {
|
|
//// updateBindState(it)
|
|
//// }
|
|
//// }
|
|
// }
|
|
//
|
|
// private fun createAdapter(): GenericItemAdapter<SearchResult.Member, ItemSearchUserInfoBinding> {
|
|
// return GenericItemAdapter(
|
|
// items = emptyList(),
|
|
// bindingInflater = ItemSearchUserInfoBinding::inflate,
|
|
// bindCallback = { item, position ->
|
|
// this.tvName.text = item.name.maskName()
|
|
// this.tvPhone.text = item.phone.maskPhone()
|
|
// if (item.id != checkedItem?.id) {
|
|
// this.llRoot.setBackgroundResource(R.drawable.grid_item_bind)
|
|
// } else {
|
|
// this.llRoot.setBackgroundResource(R.drawable.grid_item_unbind)
|
|
// }
|
|
//
|
|
// this.llRoot.setOnClickListener {
|
|
// Timber.d("itemClick ${item.name}, position = $position")
|
|
// checkedItem = item
|
|
// adapter.notifyDataSetChanged()
|
|
// //TODO 调用接口查询用户是否已绑盘
|
|
// this@BindPlateFragment.equipmentBoxCode = null
|
|
// (activity as? MainActivity)?.showWaitingDialog("查询中,请稍后……")
|
|
// userViewModel.getUserInfoById(memberId = item.faceId) {
|
|
// (activity as? MainActivity)?.hideWaitingDialog()
|
|
// this@BindPlateFragment.equipmentBoxCode = it?.equipmentBoxCode
|
|
// }
|
|
// }
|
|
// }
|
|
// )
|
|
// }
|
|
//
|
|
// private var equipmentBoxCode:String? = null
|
|
//
|
|
// private fun showBindDialog() {
|
|
// UserBindRemindDialog(
|
|
// activity = requireActivity(),
|
|
// content = "您已绑定编号为${equipmentBoxCode}的柜子,请解绑后重试"
|
|
// )
|
|
// .show()
|
|
// }
|
|
//
|
|
// private fun updateBindState(pair: Pair<Boolean?, ErrorInfo>){
|
|
// if (pair.first == null) return // 解绑状态过滤
|
|
// val errorInfo = pair.second
|
|
// if (errorInfo.isSuccess()) {
|
|
// if (errorInfo.equipmentUserInfo == null) {
|
|
// ToastUtils.showToast("数据异常")
|
|
// return
|
|
// }
|
|
//// if (errorInfo.equipmentUserInfo.isOtherEquipment()) {
|
|
//// MainActivity.start(
|
|
//// requireContext(),
|
|
//// pageType = PageType.PLATE_TIP,
|
|
//// equipmentUserInfo = errorInfo.equipmentUserInfo,
|
|
//// isAdmin = true
|
|
//// )
|
|
//// } else {
|
|
// SerialApi.openPlate(
|
|
// errorInfo.equipmentUserInfo.equipmentBoxCode!!.toInt(),
|
|
// object : SerialPortManager.SendCallback {
|
|
// override fun onSuccess() {
|
|
// ToastUtils.showToast("绑定成功")
|
|
// activity?.finish()
|
|
// }
|
|
//
|
|
// override fun onFail(e: Exception?) {
|
|
// ToastUtils.showToast("绑定失败")
|
|
// }
|
|
// })
|
|
//// }
|
|
// } else {
|
|
// ToastUtils.showToast(errorInfo.msg)
|
|
// }
|
|
// }
|
|
//} |