Android硬件对接大华秤专用dialog

142 阅读1分钟

创建dialog - DaHuaDialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.recyclerview.widget.RecyclerView
import cn.huimin.cashdesk.R
import cn.huimin.cashdesk.cashierdesk.adapter.DaHuaGoodsAdapter
import cn.huimin.cashdesk.entity.Goods
import cn.huimin.cashdesk.widget.decoration.GridSpacingItemDecoration

/**
 * @ClassName: DaHuaDialog
 * @Description: 大华秤专用dialog进度
 * @Author: 有时有晌
 * @Date: 2021/12/7 17:41
 */
class DaHuaDialog(context:Context):Dialog(context, R.style.Custom_Dialog_Style) ,View.OnClickListener{

    var mContext = context
    var tv_progress:TextView?=null
    var rl_progress:RelativeLayout?=null
    var progress_bar:ProgressBar?=null
    var recycle_view:RecyclerView?=null
    var iv_close:ImageView?=null
    var ll_error_list:LinearLayout?=null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.dialg_show_progress)
        setCanceledOnTouchOutside(false)
        tv_progress = findViewById(R.id.tv_progress)
        rl_progress = findViewById(R.id.rl_progress)
        progress_bar = findViewById(R.id.progress_bar)
        recycle_view = findViewById(R.id.recycle_view)
        ll_error_list = findViewById(R.id.ll_error_list)
        iv_close = findViewById(R.id.iv_close)
        iv_close?.setOnClickListener(this)
        recycle_view?.addItemDecoration(
            GridSpacingItemDecoration(4,
                20,
                20,
                true)
        )
    }
    fun setContext(currentProgress:Int,totalProgress:Int){
        tv_progress?.text = "$currentProgress / $totalProgress "
        progress_bar?.max = totalProgress
        progress_bar?.progress = currentProgress
    }
    fun setErrorList(errorList:MutableList<Goods>){
        rl_progress?.visibility = View.GONE
        ll_error_list?.visibility = View.VISIBLE
        var daHuaGoodsAdapter = DaHuaGoodsAdapter()
        recycle_view?.adapter = DaHuaGoodsAdapter().also { it->
            daHuaGoodsAdapter = it
        }
        daHuaGoodsAdapter.setNewInstance(errorList)
    }

    override fun onClick(v: View?) {
        v?.let {
            when(v.id){
                R.id.iv_close->{
                    dismiss()
                }
            }
        }
    }
}

对应dialog使用样式

<style name="Custom_Dialog_Style" parent="@android:style/Theme.Dialog">
    <!-- 窗口背景色透明 -->
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <!-- 是否浮在界面上 -->
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowFrame">@null</item>
    <!--  是否允许背景变暗  --> <!-- 背景是否模糊显示 -->
    <item name="android:backgroundDimEnabled">true</item>
    <!-- 是否有标题 -->
    <item name="android:windowNoTitle">true</item>
    <!-- 是否半透明 -->
    <item name="android:windowIsTranslucent">true</item>
</style>

dialog 弹出是否使用动画

<style name="bottom_animation" parent="android:Animation">
    <item name="android:windowEnterAnimation">@anim/bottom_enter</item>
    <item name="android:windowExitAnimation">@anim/bottom_exit</item>
</style>

调用方式

fun allPush(mContext:Activity,ipAddress: String) {
    var daHuaDialog = DaHuaDialog(mContext)
    var addressArray = ipAddress.split(":")
    //开个线程执行写入
    Thread{
       
        if (goodLists.size<=0){
            mContext.runOnUiThread {
                ToastUtil.show(mContext,"商品不存在或商品pluCode不存在,请到秤编码设置下发")
                daHuaDialog.dismiss()
            }
            return@Thread
        }else{
           mContext.runOnUiThread {
               daHuaDialog.show()
               daHuaDialog.setContext(0,goodLists.size)
           }
        }
        var socketUtils = SocketUtils().socketUtils
        var isConnect = socketUtils.connect(addressArray[0],addressArray[1].toInt()) // 批量下发与清除秤数据合并
        mContext.runOnUiThread {
            try {
                DahuaSettingFragment.dahuaSettingFragment?.setConnectStatus(ipAddress,isConnect)
            } catch (e: Exception) {
                addressList.forEach {
                    if (it.ipAddress == ipAddress){
                        it.isConnect = isConnect
                    }
                }
            }
        }
        if (!isConnect){
            LogUtil.e("TAG","==-->socket连接失败")
            mContext.runOnUiThread {
                ToastUtil.showShort(mContext, "${ipAddress.replace(":4001","")}连接失败,请稍后重试或检查设备")
                daHuaDialog.dismiss()
            }
            return@Thread
        }
        LogUtil.e("TAG","==-->发送商品信息总数:${goodLists.size}")
        goodLists.forEach {
            var beanIndex = goodLists.indexOf(it)+1
            LogUtil.e("TAG","==-->发送商品信息每条数据:$beanIndex : $it")
            var barCodeScaleGoods = BarCodeScaleGoods()
            barCodeScaleGoods.pluCode = if (it.pluCode<=0) "$beanIndex" else "${it.pluCode}"
            barCodeScaleGoods.code = if (it.pluCode<=0) "$beanIndex" else "${it.pluCode}"
            barCodeScaleGoods.price = it.realSalePrice
            barCodeScaleGoods.goodsName =it.goodsName.replace(" ","")
            barCodeScaleGoods.unit = "0"// 0 称重 1 计件 2 定重
            barCodeScaleGoods.barCodeHeader = "62"//大华秤固定前缀
            LogUtil.e("TAG","==-->发送商品信息pluCode:=${barCodeScaleGoods.pluCode} skuId ${barCodeScaleGoods.code}  price ${barCodeScaleGoods.price} goodsName ${barCodeScaleGoods.goodsName} unit ${barCodeScaleGoods.unit} barHeader ${barCodeScaleGoods.barCodeHeader}")
            var isSendSuccess = socketUtils.setGoods(barCodeScaleGoods)
            if (isSendSuccess){
                LogUtil.e("TAG","==-->成功商品信息pluCode:=${barCodeScaleGoods.pluCode} skuId ${barCodeScaleGoods.code}  price ${barCodeScaleGoods.price} goodsName ${barCodeScaleGoods.goodsName} unit ${barCodeScaleGoods.unit} barHeader ${barCodeScaleGoods.barCodeHeader}")
            }else{
                LogUtil.e("TAG","==-->失败商品信息pluCode:=${barCodeScaleGoods.pluCode} skuId ${barCodeScaleGoods.code}  price ${barCodeScaleGoods.price} goodsName ${barCodeScaleGoods.goodsName} unit ${barCodeScaleGoods.unit} barHeader ${barCodeScaleGoods.barCodeHeader}")
                var secendSuccess = socketUtils.setGoods(barCodeScaleGoods)
                if (secendSuccess){
                    LogUtil.e("TAG","==-->失败商品二次写入成功信息pluCode:=${barCodeScaleGoods.pluCode} skuId ${barCodeScaleGoods.code}  price ${barCodeScaleGoods.price} goodsName ${barCodeScaleGoods.goodsName} unit ${barCodeScaleGoods.unit} barHeader ${barCodeScaleGoods.barCodeHeader}")
                }else{
                    LogUtil.e("TAG","==-->失败商品二次写入失败信息pluCode:=${barCodeScaleGoods.pluCode} skuId ${barCodeScaleGoods.code}  price ${barCodeScaleGoods.price} goodsName ${barCodeScaleGoods.goodsName} unit ${barCodeScaleGoods.unit} barHeader ${barCodeScaleGoods.barCodeHeader}")
                    errorList.add(it)
                }
            }

            mContext.runOnUiThread {
                //可以通过当前方式直接刷新对应的UI进度
                daHuaDialog.setContext(beanIndex+1,goodLists.size)
                if (beanIndex==goodLists.size){
                    LogUtil.e("TAG","==-->写入完成")
                    mContext.runOnUiThread {
                        ToastUtil.show(mContext,"${ipAddress.replace(":4001","")}数据同步完成")
                        if (errorList.size<=0){
                            daHuaDialog.dismiss()
                        }
                    }
                }
            }
            if (beanIndex==goodLists.size){
                if (errorList.size>0){
                    mContext.runOnUiThread {
                        daHuaDialog.setErrorList(errorList = errorList)
                    }
                }
                socketUtils.closeConnect()
            }
        }
    }.start()
}