优化已采集页面数据统计、更新初始化页面加载动画资源、替换新识别模型

This commit is contained in:
2026-04-28 15:29:53 +08:00
parent 13f893cd0c
commit c995daf48d
20 changed files with 597 additions and 325 deletions
@@ -5,7 +5,9 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.sw.inbound.R
@@ -13,6 +15,7 @@ import com.sw.inbound.activity.FoodCollectionActivity
import com.sw.inbound.adapter.CollectSearchAdapter
import com.sw.inbound.databinding.DialogCollectSearchBinding
import com.sw.inbound.databinding.LayoutEmptySearchBinding
import com.sw.inbound.model.response.CollectedFoodInfo
import com.sw.inbound.model.response.SearchGoodsInfo
import com.sw.inbound.objbox.ObjectBox
import com.sw.inbound.utils.ext.addOnActionSearchListener
@@ -28,6 +31,32 @@ class CollectSearchDialog(
private lateinit var inflater: LayoutInflater
private var emptyBinding: LayoutEmptySearchBinding? = null
private lateinit var binding: DialogCollectSearchBinding
/**
* 收集 searchFoodState
*/
@SuppressLint("NotifyDataSetChanged")
private fun initObserver() {
activity?.run {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
userViewModel.collectedFoodListState.collect { items ->
binding.rvSearch.layoutManager =
GridLayoutManager(activity, 2, LinearLayoutManager.VERTICAL, false)
list.clear()
list.addAll(items)
searchAdapter.notifyDataSetChanged()
finishRefresh()
binding.root.hideKeyboard()
if (items.isEmpty()) {
loadEmptyView()
}
}
}
}
}
}
override fun getRootView(): View {
inflater = LayoutInflater.from(context)
binding = DialogCollectSearchBinding.inflate(inflater)
@@ -82,23 +111,14 @@ class CollectSearchDialog(
this@CollectSearchDialog.clickIndex = -1
val searchName = binding.etInputGoods.text.toString().trim()
getCollectGoods(searchName)
}
//it.setOnLoadMoreListener {
// searchGoods()
//}
}
getCollectGoods()
//binding.root.clickWithDebounce {
// Thread {
// FoodModule.initDefFoodData(activity!!) {
// activity?.runOnUiThread {
// activity?.toast("添加完成")
// getCollectGoods()
// }
// }
// }.start()
//}
binding.root.postDelayed({
finishRefresh()
}, 1000)
}
}
initObserver()
getCollectGoods()
}
private var pageNo = 1
@@ -118,17 +138,17 @@ class CollectSearchDialog(
private var clickIndex = -1
private val list: MutableList<SearchGoodsInfo.Record> = mutableListOf()
private val list: MutableList<CollectedFoodInfo> = mutableListOf()
private val searchAdapter by lazy {
CollectSearchAdapter(list).apply {
isStateViewEnable = true
setOnItemClickListener { _, _, position ->
activity?.lifecycleScope?.launch {
val goodsName = list[position].goodsName?:""
val count = ObjectBox.countNameField(goodsName)
activity?.toast("已采集数据${count}")
}
}
// setOnItemClickListener { _, _, position ->
// activity?.lifecycleScope?.launch {
// val goodsName = list[position].goodsName?:""
// val count = ObjectBox.countNameField(goodsName)
// activity?.toast("已采集数据${count}条")
// }
// }
addOnItemChildClickListener(R.id.ivDelete) { _, _, position ->
this@CollectSearchDialog.clickIndex = position
list.forEach { item ->
@@ -144,46 +164,24 @@ class CollectSearchDialog(
}
private fun showDeleteDialog(position: Int, deleteAction: () -> Unit = {}) {
val foodName = list[position].foodName
WarnDialog(
context = context,
content = "请确认是否删除物品:${list[position].goodsName?.split("WP")?.get(0)}",
content = "请确认是否删除物品:${foodName.split("WP").first()}",
cancelBlock = {
this@CollectSearchDialog.clickIndex = -1
list[position].isSelected = false
searchAdapter.notifyItemChanged(position)
},
confirmBlock = {
// Thread {
// }.start()
activity?.lifecycleScope?.launch {
deleteGoods(position, deleteAction)
}
activity?.userViewModel?.removeCollectedFood(
foodName = foodName,
nameFilter = binding.etInputGoods.text.toString().trim()
)
deleteAction()
}).show()
}
private suspend fun deleteGoods(position: Int, deleteAction: () -> Unit = {}) {
activity?.runOnUiThread {
Loading.show(activity!!)
}
val name = list[position].goodsName ?: ""
// val filterIdList = box?.all?.filter { it.name == name }?.map { it.id }
// ObjectBox.boxStore.runInTx {
// box?.removeByIds(filterIdList)
// }
ObjectBox.remove(name)
activity?.runOnUiThread {
Loading.dismiss()
//list.remove(list[position])
//searchAdapter.notifyDataSetChanged()
searchAdapter.removeAt(position)
activity?.toast("删除成功")
if (list.isEmpty()) {
loadEmptyView()
}
deleteAction()
}
}
private fun finishRefresh() {
binding.refreshLayout.let {
if (pageNo == 1) {
@@ -197,27 +195,8 @@ class CollectSearchDialog(
// private var box: Box<Food>? = null
@SuppressLint("NotifyDataSetChanged")
private fun getCollectGoods(searchName: String? = null) = activity?.lifecycleScope?.launch {
// if (box == null) {
// box = ObjectBox.boxStore.boxFor(Food::class)
// }
list.clear()
// var queryList = box?.all?.distinctBy { it.name }
// if (searchName.isNullOrBlank().not()) {
// queryList = queryList?.filter { it.name?.contains(searchName!!) == true }
// }
val queryList = ObjectBox.filter(searchName)
queryList?.forEach {
list.add((SearchGoodsInfo.Record(goodsName = it.name)))
}
binding.rvSearch.layoutManager =
GridLayoutManager(activity, 2, LinearLayoutManager.VERTICAL, false)
searchAdapter.notifyDataSetChanged()
if (list.isEmpty()) {
loadEmptyView()
}
finishRefresh()
binding.root.hideKeyboard()
private fun getCollectGoods(name: String? = null) {
activity?.userViewModel?.loadCollectedFoodList(nameFilter = name)
}
// fun test() {