Files
smart-speaker-android-main/app/src/main/java/com/qidian/zhongkesmart/dialog/FindNewEquipmentDialog.kt
2025-07-11 21:19:23 +08:00

459 lines
18 KiB
Kotlin

package com.qidian.zhongkesmart.dialog
import android.content.Context
import android.content.Intent
import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.flyco.dialog.utils.CornerUtils
import com.flyco.dialog.widget.base.BaseDialog
import com.qidian.zhongkesmart.R
import com.qidian.zhongkesmart.activity.HomeActivity
import com.qidian.zhongkesmart.activity.SettingModeActivity
import com.qidian.zhongkesmart.model.CommonMLocation
import com.qidian.zhongkesmart.model.CommonMUse
import com.qidian.zhongkesmart.utils.*
import com.scwang.smartrefresh.layout.util.DensityUtil.dp2px
import kotlinx.android.synthetic.main.layout_list.*
import net.idik.lib.slimadapter.SlimAdapter
import net.idik.lib.slimadapter.SlimInjector
import net.idik.lib.slimadapter.viewinjector.IViewInjector
import org.w3c.dom.Text
import java.util.*
import kotlin.collections.ArrayList
/*发现新设备弹窗*/
class FindNewEquipmentDialog {
private var callBack: PopupYearWindowCallBack? = null
private var activity: Context? = null
var dialog: CustomCommonDialog? = null
var mChooseType = 0//默认显示设备信息
var mMac = ""
var ll_setting_mode: LinearLayout? = null
var tv_pop_sure: TextView? = null
var tv_pop_relievebind: TextView? = null
var tv_pop_cancel: TextView? = null
var tv_connect_status:TextView? = null
fun getShareDialog(context: Context?, mac: String, callBack: PopupYearWindowCallBack?) {
activity = context
this.callBack = callBack
this.mMac = mac
dialog = CustomCommonDialog(activity)
dialog!!.widthScale(0.8f)
dialog!!.show()
dialog!!.setCanceledOnTouchOutside(false)
}
fun setDismiss() {
if (dialog != null && dialog!!.isShowing) {
dialog!!.dismiss()
callBack!!.doDismiss()
}
}
fun bindSuccess(){
if (dialog != null && dialog!!.isShowing) {
// ll_setting_mode!!.visibility = View.VISIBLE
tv_pop_relievebind!!.visibility = View.VISIBLE
tv_pop_cancel!!.visibility = View.GONE
tv_pop_sure!!.text = "下一步"
}
}
fun connectSuccess(){
if (dialog != null && dialog!!.isShowing) {
tv_connect_status!!.visibility = View.GONE
}
}
fun connectFail(){
if (dialog != null && dialog!!.isShowing) {
tv_connect_status!!.visibility = View.VISIBLE
}
}
fun goSettingMode(){
setDismiss()
activity!!.startActivity(Intent(activity, SettingModeActivity::class.java))
}
interface PopupYearWindowCallBack {
fun doWork(roomName: String?, type: String?)
fun doChangeWork(roomName: String?, type: String?)
fun doUnBindWork()
fun doConnectTj()
fun doDismiss()
}
inner class CustomCommonDialog(context: Context?) : BaseDialog<CustomCommonDialog?>(context) {
var li_close: LinearLayout? = null
var li_new_place: LinearLayout? = null
var li_new_use: LinearLayout? = null
var li_equipment: LinearLayout? = null
var tv_pop_title: TextView? = null
var tv_new_name: TextView? = null
var tv_place_custom: TextView? = null
var tv_place_worm: TextView? = null
var tv_use_custom: TextView? = null
var tv_use_worm: TextView? = null
var imv_pic: ImageView? = null
var tv_new_place: EditText? = null
var tv_new_use: EditText? = null
var recycle_options: RecyclerView? = null
var mAdapter: SlimAdapter? = null
var mData = ArrayList<Any>()
override fun onCreateView(): View {
val inflate = View.inflate(mContext, R.layout.pop_findnew, null)
inflate.setBackgroundDrawable(
CornerUtils.cornerDrawable(Color.parseColor("#ffffff"), dp2px(10f).toFloat())
)
li_close = inflate.findViewById<LinearLayout>(R.id.li_close)
li_new_place = inflate.findViewById<LinearLayout>(R.id.li_new_place)
li_new_use = inflate.findViewById<LinearLayout>(R.id.li_new_use)
tv_pop_title = inflate.findViewById<TextView>(R.id.tv_pop_title)
tv_new_name = inflate.findViewById<TextView>(R.id.tv_new_name)
imv_pic = inflate.findViewById<ImageView>(R.id.imv_pic)
tv_new_place = inflate.findViewById<EditText>(R.id.tv_new_place)
tv_place_custom = inflate.findViewById<TextView>(R.id.tv_place_custom)
tv_place_worm = inflate.findViewById<TextView>(R.id.tv_place_worm)
tv_new_use = inflate.findViewById<EditText>(R.id.tv_new_use)
tv_use_custom = inflate.findViewById<TextView>(R.id.tv_use_custom)
tv_use_worm = inflate.findViewById<TextView>(R.id.tv_use_worm)
recycle_options = inflate.findViewById<RecyclerView>(R.id.recycle_options)
li_equipment = inflate.findViewById<LinearLayout>(R.id.li_equipment)
//按钮
tv_pop_cancel = inflate.findViewById<TextView>(R.id.tv_pop_cancel)
tv_pop_relievebind = inflate.findViewById<TextView>(R.id.tv_pop_relievebind)
tv_pop_sure = inflate.findViewById<TextView>(R.id.tv_pop_sure)
ll_setting_mode = inflate.findViewById<LinearLayout>(R.id.ll_setting_mode)
tv_connect_status = inflate.findViewById<TextView>(R.id.tv_connect_status)
//固定内容
tv_new_name!!.text = "低功耗节点Low Power"
/*进入默认不能输入*/
setEdit(false, tv_new_place!!)
setEdit(false, tv_new_use!!)
aboutList()
if (ToolUtil.isBind(activity!!, DataServer.mBindHistory, mMac)) {
var model = ToolUtil.getBindBlueToothData(activity!!, DataServer.mBindHistory, mMac)
tv_new_place!!.setText(model.name)
tv_new_use!!.setText(model.type)
}
isBind()
return inflate
}
/**
* 是否绑定
*/
fun isBind() {
if (ToolUtil.isBind(activity!!, DataServer.mBindHistory, mMac)) {
tv_pop_relievebind!!.visibility = View.VISIBLE
tv_pop_cancel!!.visibility = View.GONE
tv_pop_sure!!.text = "确定"
tv_pop_title!!.text = "已绑定设备"
} else {
tv_pop_relievebind!!.visibility = View.GONE
tv_pop_cancel!!.visibility = View.VISIBLE
tv_pop_sure!!.text = "立即绑定"
tv_pop_title!!.text = "发现新设备"
}
}
/*设置是否可编辑*/
fun setEdit(isCan: Boolean, edittext: EditText) {
edittext.isCursorVisible = isCan
edittext.isFocusable = isCan
edittext.setTextIsSelectable(isCan)
}
/*点击事件处理*/
override fun setUiBeforShow() {
//选择位置
tv_new_place!!.setOnClickListener {
mChooseType = 1
getTypeList()
showChooseList(true)
}
tv_new_place!!.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
if (s!!.isNotEmpty()) {
InputLimitUtil.ShowEdittextStyle3(
tv_new_place!!,
li_new_place!!,
tv_place_worm!!,
""
)
}
}
})
//自定义
tv_place_custom!!.setOnClickListener {
if (tv_place_custom!!.text.toString() == "自定义") {
tv_place_custom!!.text = "选择"
tv_new_place!!.hint = "请输入放置位置"
setEdit(true, tv_new_place!!)
} else {
tv_place_custom!!.text = "自定义"
setEdit(false, tv_new_place!!)
tv_new_place!!.hint = "请选择放置位置"
}
}
//选择用途
tv_new_use!!.setOnClickListener {
mChooseType = 2
showChooseList(true)
getLocationData(tv_new_place!!.text.toString())
}
tv_new_use!!.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(
s: CharSequence?,
start: Int,
count: Int,
after: Int
) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
if (s!!.isNotEmpty()) {
InputLimitUtil.ShowEdittextStyle3(
tv_new_use!!,
li_new_use!!,
tv_use_worm!!,
""
)
}
}
})
//自定义
tv_use_custom!!.setOnClickListener {
if (tv_use_custom!!.text.toString() == "自定义") {
tv_use_custom!!.text = "选择"
setEdit(true, tv_new_use!!)
tv_new_use!!.hint = "请输入设备用途"
} else {
tv_use_custom!!.text = "自定义"
tv_new_use!!.isEnabled = false
setEdit(false, tv_new_use!!)
tv_new_use!!.hint = "请选择设备用途"
}
}
//立即绑定+确定
tv_pop_sure!!.setOnClickListener {
if (tv_pop_sure!!.text == "立即绑定") {
if (tv_new_place!!.text!!.isEmpty() || tv_new_use!!.text!!.isEmpty()) {
if (tv_new_place!!.text!!.isEmpty()) {
InputLimitUtil.ShowEdittextStyle3(
tv_new_place!!,
li_new_place!!,
tv_place_worm!!,
"请选择放置位置"
)
}
if (tv_new_use!!.text!!.isEmpty()) {
InputLimitUtil.ShowEdittextStyle3(
tv_new_use!!,
li_new_use!!,
tv_use_worm!!,
"请选择设备用途"
)
}
return@setOnClickListener
}
callBack!!.doWork(tv_new_place!!.text.toString(), tv_new_use!!.text.toString())
}else if(tv_pop_sure!!.text == "返回"){
callBack!!.doDismiss()
dismiss()
}else if(tv_pop_sure!!.text == "下一步"){
callBack!!.doConnectTj()
} else {//确定 修改
if (tv_new_place!!.text!!.isEmpty() || tv_new_use!!.text!!.isEmpty()) {
if (tv_new_place!!.text!!.isEmpty()) {
InputLimitUtil.ShowEdittextStyle3(
tv_new_place!!,
li_new_place!!,
tv_place_worm!!,
"请选择放置位置"
)
}
if (tv_new_use!!.text!!.isEmpty()) {
InputLimitUtil.ShowEdittextStyle3(
tv_new_use!!,
li_new_use!!,
tv_use_worm!!,
"请选择设备用途"
)
}
return@setOnClickListener
}
callBack!!.doChangeWork(tv_new_place!!.text.toString(), tv_new_use!!.text.toString())
// dismiss()
}
}
li_close!!.setOnClickListener {
if (mChooseType != 0) {
showChooseList(false)
} else {
callBack!!.doDismiss()
dismiss()
}
}
//取消
tv_pop_cancel!!.setOnClickListener {
if (mChooseType != 0) {
showChooseList(false)
} else {
callBack!!.doDismiss()
dismiss()
}
}
//解除绑定
tv_pop_relievebind!!.setOnClickListener {
callBack!!.doUnBindWork()
}
//配置模式
ll_setting_mode!!.setOnClickListener{
callBack!!.doConnectTj()
}
}
/*选择位置+用途*/
fun aboutList() {
LoadUtils.setLinearLayoutManager(activity, recycle_options, false)
mAdapter =
SlimAdapter.create()
//位置
.register<CommonMLocation>(
R.layout.item_text,
object : SlimInjector<CommonMLocation> {
override fun onInject(
data: CommonMLocation,
injector: IViewInjector<out IViewInjector<*>>
) {
injector.text(R.id.tv_text, data.name)
injector.with<TextView>(R.id.tv_text) {
//选中变色效果暂未实现
if (data.check == 1) {
it.setBackgroundColor(activity!!.resources.getColor(R.color.color_516AFC))
it.setTextColor(activity!!.resources.getColor(R.color.white))
} else {
it.setBackgroundColor(activity!!.resources.getColor(R.color.white))
it.setTextColor(activity!!.resources.getColor(R.color.color_999999))
}
}
injector.clicked(R.id.li_text) {
if (mChooseType == 1) {
tv_new_place!!.setText(data.name)
// tv_new_use!!.setText("")
} else if (mChooseType == 2) {
tv_new_use!!.setText(data.name)
}
showChooseList(false)
}
}
})
.attachTo(recycle_options)
}
//位置数据
fun getTypeList() {
mData.clear()
DataServer.TJLocation.forEach {
var bean = CommonMLocation(it, 0)
mData.add(bean)
}
mAdapter!!.updateData(mData).notifyDataSetChanged()
}
//用途数据
fun getLocationData(type: String) {
mData.clear()
DataServer.TJUse.forEach {
var bean = CommonMLocation(it, 0)
mData.add(bean)
}
mAdapter!!.updateData(mData).notifyDataSetChanged()
}
fun getData(list: Array<String>) {
list.forEach {
var bean = CommonMLocation(it, 0)
mData.add(bean)
}
}
/*显示界面按断*/
fun showChooseList(isShow: Boolean) {
if (isShow) {
//按钮
tv_pop_cancel!!.visibility = View.VISIBLE
tv_pop_sure!!.visibility = View.VISIBLE
tv_pop_sure!!.text = "确定"
tv_pop_relievebind!!.visibility = View.GONE
//布局样式
recycle_options!!.visibility = View.VISIBLE
li_equipment!!.visibility = View.GONE
} else {
recycle_options!!.visibility = View.GONE
li_equipment!!.visibility = View.VISIBLE
//按钮--现在只考虑未绑定状态
isBind()
/* tv_pop_cancel!!.visibility = View.GONE
tv_pop_sure!!.visibility = View.VISIBLE
tv_pop_sure!!.text = "立即绑定"
tv_pop_relievebind!!.visibility = View.GONE*/
mChooseType = 0
}
when (mChooseType) {
0 -> {
tv_pop_title!!.text = "发现新设备"
}
1 -> {
tv_pop_title!!.text = "选择位置"
}
2 -> {
tv_pop_title!!.text = "选择用途"
}
}
}
}
companion object {
private var popupWindowPrivinceListUtils: FindNewEquipmentDialog? = null
@get:Synchronized
val instance: FindNewEquipmentDialog?
get() {
if (popupWindowPrivinceListUtils == null) {
popupWindowPrivinceListUtils = FindNewEquipmentDialog()
}
return popupWindowPrivinceListUtils
}
}
}