feat(ui): 在已采集食材页添加清除全部数据功能

- ObjectBox 新增 removeAll() 方法清除所有向量数据
- DbViewModel 新增 clearAllCollectedFood() 方法并刷新列表
- CollectedFoodActivity 右上角添加垃圾桶图标,点击通过 CommonDialog 二次确认后清除

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 14:38:40 +08:00
co-authored by Claude Sonnet 4.6
parent e38f19ac1b
commit 25167829cc
3 changed files with 38 additions and 1 deletions
@@ -537,5 +537,17 @@ class DbViewModel : ViewModel() {
}
}
/**
* 清除所有向量采集数据,完成后刷新列表
* @param onDone 清除完成后在主线程执行的回调
*/
fun clearAllCollectedFood(onDone: (() -> Unit)? = null) {
viewModelScope.launch {
ObjectBox.removeAll()
loadCollectedFoodList()
onDone?.invoke()
}
}
//--------------------------------------------------------------------------------------------------------
}
@@ -213,6 +213,11 @@ object ObjectBox {
getBox<Food>()?.query(Food_.name.equal(name))?.build()?.remove()
}
// 清除所有向量采集数据
suspend fun removeAll() = safeDbOp {
getBox<Food>()?.removeAll()
}
/**
* 按名称分组统计采集数量
* 使用属性查询只加载 name 字段,不加载 foodVector,节省大量内存
@@ -55,6 +55,22 @@ class CollectedFoodActivity : BaseActivity() {
}
}
/**
* 清除所有采集数据确认弹窗
*/
private fun showClearAllDialog() {
CommonDialog(this)
.setTitle("清除提示")
.setContent("清除后无法恢复,确认要清除所有采集数据吗?")
.setNegativeButton("取消")
.setPositiveButton("确认") {
dbViewModel.clearAllCollectedFood {
toast("已清除所有采集数据")
}
}
.show()
}
/**
* 删除弹窗
*/
@@ -96,7 +112,11 @@ class CollectedFoodActivity : BaseActivity() {
}, titleAction = {
it.text = "已采集食材"
}, rightIconAction = {
it.gone()
it.visible()
it.setImageResource(R.drawable.ic_trash_white)
it.setOnClickListener {
showClearAllDialog()
}
}, backAction = {
it.visible()
it.setOnClickListener {