refactor(HomeActivity): 修复内存泄漏并优化错误处理
1. 修复内存泄漏问题 - 在 onDestroy 中清理所有 Handler 回调 - 在 onDestroy 中释放 Dialog 引用 2. 增强类型安全 - updateUI 中增加类型检查和日志 - updateOverdueState 中增加完整的类型检查 3. 优化串口异常处理 - 串口数据处理异常时向用户提示 4. 消除代码重复 - 删除冗余的 saveShelfGoodsList 方法 - 定时任务直接调用 syncShelfGoodsToServer Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
04fe3ec2c1
commit
3e5c7c6caa
@@ -132,7 +132,11 @@ class HomeActivity : BaseActivity() {
|
||||
// 切换到主线程协程处理串口数据
|
||||
lifecycleScope.launch {
|
||||
runCatching { receiveSerialPortData(data) }
|
||||
.onFailure { it.printStackTrace() }
|
||||
.onFailure {
|
||||
it.printStackTrace()
|
||||
log("串口数据处理异常: ${it.message}")
|
||||
toast("串口数据处理异常,请检查设备连接")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -164,8 +168,21 @@ class HomeActivity : BaseActivity() {
|
||||
launch {
|
||||
viewModel.updateOverdueStateUiState.collect { state ->
|
||||
if (state is UiState.Success<*>) {
|
||||
val respData = state.data as? com.shuwei.intelligent.shelves.net.RespData<*> ?: return@collect
|
||||
val items = (respData.data as? ShelfResult)?.containerGoodsList ?: return@collect
|
||||
val respData = state.data as? com.shuwei.intelligent.shelves.net.RespData<*>
|
||||
if (respData == null) {
|
||||
log("updateOverdueState: 数据类型错误,期望 RespData")
|
||||
return@collect
|
||||
}
|
||||
val shelfResult = respData.data as? ShelfResult
|
||||
if (shelfResult == null) {
|
||||
log("updateOverdueState: 数据类型错误,期望 ShelfResult")
|
||||
return@collect
|
||||
}
|
||||
val items = shelfResult.containerGoodsList
|
||||
if (items.isNullOrEmpty()) {
|
||||
log("updateOverdueState: 货架列表为空")
|
||||
return@collect
|
||||
}
|
||||
applyOverdueState(items)
|
||||
}
|
||||
}
|
||||
@@ -200,15 +217,17 @@ class HomeActivity : BaseActivity() {
|
||||
private fun updateUI(data: RespData<*>) {
|
||||
binding.include?.root?.gone()
|
||||
window?.decorView?.postDelayed({ Loading.dismiss() }, 500)
|
||||
if (data.data !is ShelfResult) {
|
||||
val shelfResult = data.data as? ShelfResult
|
||||
if (shelfResult == null) {
|
||||
log("updateUI: 数据类型错误,期望 ShelfResult,实际类型:${data.data?.javaClass?.simpleName}")
|
||||
loadEmptyView()
|
||||
return
|
||||
}
|
||||
deviceName = data.data.deviceName
|
||||
deviceName = shelfResult.deviceName
|
||||
updateLeftStatus(deviceName)
|
||||
|
||||
App.canteenId = data.data.placeId
|
||||
val tempList = data.data.containerGoodsList
|
||||
App.canteenId = shelfResult.placeId
|
||||
val tempList = shelfResult.containerGoodsList
|
||||
if (tempList.isNullOrEmpty()) {
|
||||
loadEmptyView()
|
||||
return
|
||||
@@ -276,6 +295,15 @@ class HomeActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
override fun onDestroy() {
|
||||
// 清理 Handler 所有回调,防止内存泄漏
|
||||
handler.removeCallbacksAndMessages(null)
|
||||
|
||||
// 关闭并释放 Dialog 引用,防止内存泄漏
|
||||
tipDialog?.dismiss()
|
||||
tipDialog = null
|
||||
noDataWarningDialog?.dismiss()
|
||||
noDataWarningDialog = null
|
||||
|
||||
SerialPortManager.close()
|
||||
saveTaskJob?.cancel()
|
||||
overdueTaskJob?.cancel()
|
||||
@@ -411,7 +439,7 @@ class HomeActivity : BaseActivity() {
|
||||
*/
|
||||
private fun saveGoodsTask() {
|
||||
saveTaskJob = taskExecutor.startIntervalTaskWithInitialDelay(1 * 60 * 1000L, 5 * 60 * 1000L) {
|
||||
saveShelfGoodsList()
|
||||
syncShelfGoodsToServer()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -449,6 +477,7 @@ class HomeActivity : BaseActivity() {
|
||||
* 构建 ShelfBody 并提交到服务端(被定时任务与 ShelfActivity 返回共用)
|
||||
*/
|
||||
private fun syncShelfGoodsToServer() {
|
||||
log("performSync: list:${list.toJsonString()}")
|
||||
val submitList = list.sortedBy { it.deviceNo }
|
||||
submitList.forEach {
|
||||
if (it.goodsId.isNullOrBlank()) {
|
||||
@@ -465,9 +494,4 @@ class HomeActivity : BaseActivity() {
|
||||
log("performSync: body:${body.toJsonString()}")
|
||||
viewModel.saveShelfGoodsList(body)
|
||||
}
|
||||
|
||||
private fun saveShelfGoodsList() {
|
||||
log("performSync: list:${list.toJsonString()}")
|
||||
syncShelfGoodsToServer()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user