193 lines
6.8 KiB
Kotlin
193 lines
6.8 KiB
Kotlin
package com.sw.inbound.dialog
|
||
|
||
import android.annotation.SuppressLint
|
||
import android.content.Context
|
||
import android.view.LayoutInflater
|
||
import android.view.View
|
||
import androidx.core.widget.addTextChangedListener
|
||
import androidx.recyclerview.widget.GridLayoutManager
|
||
import androidx.recyclerview.widget.LinearLayoutManager
|
||
import com.sw.inbound.R
|
||
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.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 io.objectbox.Box
|
||
import io.objectbox.kotlin.boxFor
|
||
|
||
@SuppressLint("NotifyDataSetChanged")
|
||
class CollectSearchDialog(
|
||
context: Context
|
||
) : BaseDialog(context) {
|
||
|
||
private lateinit var inflater: LayoutInflater
|
||
private var emptyBinding: LayoutEmptySearchBinding? = null
|
||
private lateinit var binding: DialogCollectSearchBinding
|
||
override fun getRootView(): View {
|
||
inflater = LayoutInflater.from(context)
|
||
binding = DialogCollectSearchBinding.inflate(inflater)
|
||
return binding.root
|
||
}
|
||
|
||
private var activity: FoodCollectionActivity? = null
|
||
override fun initView() {
|
||
activity = getFoodCollectionActivity()
|
||
binding.ivClose.setOnClickListener { dismiss() }
|
||
binding.ivGoodsSearch.setOnClickListener {
|
||
val searchName = binding.etInputGoods.text.toString().trim()
|
||
if (searchName.isBlank()) {
|
||
activity?.toast("请输入物品名称")
|
||
return@setOnClickListener
|
||
}
|
||
getCollectGoods(searchName)
|
||
}
|
||
binding.etInputGoods.let { v ->
|
||
v.addOnActionSearchListener {
|
||
val searchName = binding.etInputGoods.text.toString().trim()
|
||
if (searchName.isBlank()) {
|
||
activity?.toast("请输入物品名称")
|
||
return@addOnActionSearchListener
|
||
}
|
||
getCollectGoods(searchName)
|
||
}
|
||
v.addTextChangedListener {
|
||
if (it.isNullOrBlank()) {
|
||
getCollectGoods()
|
||
}
|
||
}
|
||
}
|
||
binding.btnConfirm.setOnClickListener {
|
||
dismiss()
|
||
}
|
||
binding.rvSearch.let {
|
||
it.layoutManager = GridLayoutManager(context, 2, LinearLayoutManager.VERTICAL, false)
|
||
it.adapter = searchAdapter
|
||
}
|
||
binding.refreshLayout.let {
|
||
it.setEnableRefresh(true)
|
||
it.setEnableLoadMore(false)
|
||
it.setOnRefreshListener {
|
||
pageNo = 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()
|
||
//}
|
||
}
|
||
|
||
private var pageNo = 1
|
||
|
||
private fun loadEmptyView() {
|
||
binding.rvSearch.layoutManager =
|
||
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
||
if (emptyBinding == null) {
|
||
emptyBinding = LayoutEmptySearchBinding.inflate(inflater, binding.rvSearch, false)
|
||
}
|
||
emptyBinding?.tvContent?.text = "请检查物品名称,或稍后重新搜索"
|
||
emptyBinding?.root?.let { layout ->
|
||
layout.setOnClickListener { layout.hideKeyboard() }
|
||
searchAdapter.stateView = layout
|
||
}
|
||
}
|
||
|
||
private var clickIndex = -1
|
||
|
||
private val list: MutableList<SearchGoodsInfo.Record> = mutableListOf()
|
||
private val searchAdapter by lazy {
|
||
CollectSearchAdapter(list).apply {
|
||
isStateViewEnable = true
|
||
setOnItemClickListener { _, _, position ->
|
||
this@CollectSearchDialog.clickIndex = position
|
||
if (list[position].isSelected) {
|
||
return@setOnItemClickListener
|
||
}
|
||
list.forEach { item ->
|
||
item.isSelected = false
|
||
}
|
||
list[position].isSelected = true
|
||
notifyDataSetChanged()
|
||
}
|
||
addOnItemChildClickListener(R.id.ivDelete) { _, _, position ->
|
||
deleteGoods(position)
|
||
}
|
||
}
|
||
}
|
||
|
||
private fun deleteGoods(position: Int) {
|
||
WarnDialog(context = context, content = "请确认是否删除物品:${list[position].goodsName}?", confirmBlock = {
|
||
Thread {
|
||
activity?.runOnUiThread {
|
||
Loading.show(context)
|
||
}
|
||
val name = list[position].goodsName
|
||
val filterIdList = box?.all?.filter { it.name == name }?.map { it.id }
|
||
box?.removeByIds(filterIdList)
|
||
activity?.runOnUiThread {
|
||
Loading.dismiss()
|
||
//list.remove(list[position])
|
||
//searchAdapter.notifyDataSetChanged()
|
||
searchAdapter.removeAt(position)
|
||
activity?.toast("删除成功")
|
||
if (list.isEmpty()) {
|
||
loadEmptyView()
|
||
}
|
||
}
|
||
}.start()
|
||
}).show()
|
||
}
|
||
|
||
private fun finishRefresh() {
|
||
binding.refreshLayout.let {
|
||
if (pageNo == 1) {
|
||
it.finishRefresh(500)
|
||
} else {
|
||
it.finishLoadMore(500)
|
||
}
|
||
}
|
||
}
|
||
|
||
private var box: Box<Food>? = null
|
||
|
||
@SuppressLint("NotifyDataSetChanged")
|
||
private fun getCollectGoods(searchName:String?=null) {
|
||
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 }
|
||
}
|
||
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()
|
||
}
|
||
|
||
} |