改造objectbox

This commit is contained in:
2026-01-31 17:49:44 +08:00
parent 4322c9495b
commit af120eabd0
7 changed files with 120 additions and 74 deletions
@@ -4,8 +4,8 @@ import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.activity.viewModels
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.sw.inbound.R
@@ -14,14 +14,11 @@ import com.sw.inbound.adapter.CollectSearchAdapter
import com.sw.inbound.databinding.DialogCollectSearchBinding
import com.sw.inbound.databinding.LayoutEmptySearchBinding
import com.sw.inbound.model.response.SearchGoodsInfo
import com.sw.inbound.objbox.Food
import com.sw.inbound.objbox.ObjectBox
import com.sw.inbound.utils.ext.addOnActionSearchListener
import com.sw.inbound.utils.ext.hideKeyboard
import com.sw.inbound.utils.ext.toast
import com.sw.inbound.viewmodel.ReceiptViewModel
import io.objectbox.Box
import io.objectbox.kotlin.boxFor
import kotlinx.coroutines.launch
@SuppressLint("NotifyDataSetChanged")
class CollectSearchDialog(
@@ -69,8 +66,8 @@ class CollectSearchDialog(
dismiss()
return@setOnClickListener
}
deleteGoods(clickIndex) {
it.postDelayed({dismiss()}, 500)
showDeleteDialog(clickIndex) {
it.postDelayed({ dismiss() }, 500)
}
}
binding.rvSearch.let {
@@ -142,40 +139,47 @@ class CollectSearchDialog(
}
list[position].isSelected = true
notifyDataSetChanged()
deleteGoods(position)
showDeleteDialog(position)
}
}
}
private fun deleteGoods(position: Int, deleteAction:()-> Unit = {}) {
private fun showDeleteDialog(position: Int, deleteAction: () -> Unit = {}) {
WarnDialog(
context = context,
content = "请确认是否删除物品:${list[position].goodsName}",
confirmBlock = {
Thread {
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)
}
activity?.runOnUiThread {
Loading.dismiss()
//list.remove(list[position])
//searchAdapter.notifyDataSetChanged()
searchAdapter.removeAt(position)
activity?.toast("删除成功")
if (list.isEmpty()) {
loadEmptyView()
}
deleteAction()
}
}.start()
// Thread {
// }.start()
activity?.lifecycleScope?.launch {
deleteGoods(position, 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) {
@@ -186,18 +190,19 @@ class CollectSearchDialog(
}
}
private var box: Box<Food>? = null
// private var box: Box<Food>? = null
@SuppressLint("NotifyDataSetChanged")
private fun getCollectGoods(searchName: String? = null) {
if (box == null) {
box = ObjectBox.boxStore.boxFor(Food::class)
}
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 }
}
// 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)))
}