222 lines
7.9 KiB
Kotlin
222 lines
7.9 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.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
|
||
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
|
||
import com.sw.inbound.utils.ext.hideKeyboard
|
||
import com.sw.inbound.utils.ext.toast
|
||
import kotlinx.coroutines.launch
|
||
|
||
@SuppressLint("NotifyDataSetChanged")
|
||
class CollectSearchDialog(
|
||
context: Context
|
||
) : BaseDialog(context) {
|
||
|
||
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)
|
||
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 {
|
||
if (clickIndex == -1) {
|
||
dismiss()
|
||
return@setOnClickListener
|
||
}
|
||
showDeleteDialog(clickIndex) {
|
||
it.postDelayed({ dismiss() }, 500)
|
||
}
|
||
}
|
||
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
|
||
this@CollectSearchDialog.clickIndex = -1
|
||
val searchName = binding.etInputGoods.text.toString().trim()
|
||
getCollectGoods(searchName)
|
||
|
||
binding.root.postDelayed({
|
||
finishRefresh()
|
||
}, 1000)
|
||
}
|
||
}
|
||
initObserver()
|
||
getCollectGoods()
|
||
}
|
||
|
||
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<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}条")
|
||
// }
|
||
// }
|
||
addOnItemChildClickListener(R.id.ivDelete) { _, _, position ->
|
||
this@CollectSearchDialog.clickIndex = position
|
||
list.forEach { item ->
|
||
item.isSelected = false
|
||
}
|
||
list[position].isSelected = true
|
||
notifyDataSetChanged()
|
||
showDeleteDialog(position) {
|
||
this@CollectSearchDialog.clickIndex = -1
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
private fun showDeleteDialog(position: Int, deleteAction: () -> Unit = {}) {
|
||
val foodName = list[position].foodName
|
||
WarnDialog(
|
||
context = context,
|
||
content = "请确认是否删除物品:${foodName.split("WP").first()}?",
|
||
cancelBlock = {
|
||
this@CollectSearchDialog.clickIndex = -1
|
||
list[position].isSelected = false
|
||
searchAdapter.notifyItemChanged(position)
|
||
},
|
||
confirmBlock = {
|
||
activity?.userViewModel?.removeCollectedFood(
|
||
foodName = foodName,
|
||
nameFilter = binding.etInputGoods.text.toString().trim()
|
||
)
|
||
deleteAction()
|
||
}).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(name: String? = null) {
|
||
activity?.userViewModel?.loadCollectedFoodList(nameFilter = name)
|
||
}
|
||
|
||
// fun test() {
|
||
// val goodsName = "多宝鱼,黑鱼(整条鱼),鸡腿肉"
|
||
// activity?.searchGoodsInfoList(goodsName = goodsName) { items->
|
||
// items.forEach { goods ->
|
||
// box?.all?.filter {
|
||
// it.name?.contains(goods.goodsName?:"-") == true
|
||
// }?.forEach {
|
||
// activity?.runOnUiThread {
|
||
// activity?.toast(goods.goodsName)
|
||
// }
|
||
// it.name = goods.goodsName + goods.goodsCode
|
||
// box?.put(it)
|
||
// }
|
||
// }
|
||
//// activity?.runOnUiThread {
|
||
//// activity?.toast("已保存")
|
||
//// }
|
||
// }
|
||
// }
|
||
|
||
} |