package com.sw.platecabinet.activity import androidx.lifecycle.lifecycleScope import com.sw.plate.utils.ToastUtils import com.sw.platecabinet.MyApp import com.sw.platecabinet.databinding.ActivityFunBinding import com.sw.platecabinet.databinding.ItemTitleTimeBinding import com.sw.platecabinet.dialog.CustomDialog import com.sw.platecabinet.ext.clickWithDebounce import com.sw.platecabinet.utils.PlateUtils import com.sw.platecabinet.utils.SpTool import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class FunActivity : BaseActivity() { override fun inflateTitleBinding(): ItemTitleTimeBinding? { return binding.includeHeader } override fun inflateViewBinding() = ActivityFunBinding.inflate(layoutInflater) override fun initialize() { binding.btnClearFace.clickWithDebounce { clearFace() } binding.btnClearAndGetAll.clickWithDebounce { clearFaceAndGetAll() } binding.btnOpenAllPlate.clickWithDebounce { lifecycleScope.launch { openAllPlate() } } binding.btnPutPlate.clickWithDebounce { putPlate() } } private fun clearFace() { loadClearDialog("请确认是否清除本地所有数据?") { showWaitingDialog("加载中……") clearAllFace { binding.root.postDelayed( { hideWaitingDialog() ToastUtils.showToast("已清除") }, 500 ) } } } private fun clearFaceAndGetAll() { loadClearDialog("请确认是否清除本地所有数据,并重新拉取全量人脸数据?") { showWaitingDialog("加载中……") clearAllFace { userViewModel.getUserFaceCache { status, msg -> hideWaitingDialog() ToastUtils.showToast( if (status) "请求成功,$msg" else "请求失败,$msg" ) } } } } private suspend fun openAllPlate() { MyApp.plateList?.let { plateList -> val boxCodeList = plateList.filter { it.equipmentBoxCode.isNullOrBlank().not() } .map { it.equipmentBoxCode } boxCodeList.forEach { boxCode -> withContext(Dispatchers.Default) { PlateUtils.openDirect(boxCode) delay(500) } } } } private fun putPlate() { showWaitingDialog("加载中……") MyApp.plateList?.forEachIndexed { index, item -> val num = index + 1 val realNum = if (num < 10) "0${num}" else "${num}" item.plateNumber = realNum + realNum + realNum item.faceId = null item.updateTime = null } SpTool.savePlateData(MyApp.plateList!!) binding.root.postDelayed({ hideWaitingDialog() ToastUtils.showToast("已完成") }, 1000) } /** * 加载清除数据弹窗 */ private fun loadClearDialog(msg: String, callback: () -> Unit) { CustomDialog( activity = this, content = msg, onConfirm = { callback() }).show() } override fun onLeftDoubleClick() { finish() } override fun onRightDoubleClick() { } }