改造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)))
}
@@ -15,10 +15,9 @@ import androidx.camera.view.PreviewView
import androidx.core.graphics.toColorInt
import androidx.core.view.updateLayoutParams
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import coil.load
import coil.transform.RoundedCornersTransformation
import com.sw.inbound.utils.ext.appendText
import com.sw.inbound.utils.ext.buildSpannableString
import com.sw.inbound.R
import com.sw.inbound.activity.GoodsListActivity
import com.sw.inbound.databinding.DialogGoodsStoreBinding
@@ -35,6 +34,8 @@ import com.sw.inbound.utils.BitmapCropper
import com.sw.inbound.utils.BitmapSaver
import com.sw.inbound.utils.CameraUtils
import com.sw.inbound.utils.ImageUtil
import com.sw.inbound.utils.ext.appendText
import com.sw.inbound.utils.ext.buildSpannableString
import com.sw.inbound.utils.ext.dp
import com.sw.inbound.utils.ext.gone
import com.sw.inbound.utils.ext.hideKeyboard
@@ -43,11 +44,9 @@ import com.sw.inbound.utils.ext.roundedDecimalPlace
import com.sw.inbound.utils.ext.toast
import com.sw.inbound.utils.ext.visible
import com.sw.inbound.utils.removeTrailingZeros
import io.objectbox.Box
import io.objectbox.kotlin.boxFor
import kotlinx.coroutines.launch
import timber.log.Timber
import java.io.File
import kotlin.Int
import kotlin.math.abs
import kotlin.math.min
@@ -441,8 +440,10 @@ class GoodsStoreDialog(
}
}
}
private var imageFile: File?=null
private var box: Box<Food>? = null
private var imageFile: File? = null
// private var box: Box<Food>? = null
private fun collectGoodsVector() {
try {
ImageUtil.uriToBitmap(context, imageUri!!)?.let { bitmap ->
@@ -458,21 +459,36 @@ class GoodsStoreDialog(
)
imageFile = file
Timber.d("${this.javaClass.simpleName}-collectGoodsVector-裁剪bitmap保存文件路径:${file?.absolutePath}")
if (box == null) {
box = ObjectBox.boxStore.boxFor(Food::class)
}
Thread {
// if (box == null) {
// box = ObjectBox.boxStore.boxFor(Food::class)
// }
activity?.lifecycleScope?.launch {
try {
val imageVector = FoodModule.bitmap2FloatArray(bitmap, true)
if (imageVector != null) {
ObjectBox.boxStore.runInTx {
box?.put(Food(name = goods.goodsName + goods.goodsCode, foodIdx = 0, foodVector = imageVector))
}
val food = Food(
name = goods.goodsName + goods.goodsCode,
foodIdx = 0,
foodVector = imageVector
)
ObjectBox.put(food)
}
} catch (e: Exception) {
e.printStackTrace()
}
}.start()
}
// Thread {
// try {
// val imageVector = FoodModule.bitmap2FloatArray(bitmap, true)
// if (imageVector != null) {
// ObjectBox.boxStore.runInTx {
// box?.put(Food(name = goods.goodsName + goods.goodsCode, foodIdx = 0, foodVector = imageVector))
// }
// }
// } catch (e: Exception) {
// e.printStackTrace()
// }
// }.start()
}
} catch (e: Exception) {
e.printStackTrace()