188 lines
6.2 KiB
Kotlin
188 lines
6.2 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.recyclerview.widget.GridLayoutManager
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import com.sw.inbound.R
|
|
import com.sw.inbound.activity.GoodsListActivity
|
|
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: GoodsListActivity? = null
|
|
override fun initView() {
|
|
activity = getReceiptActivity()
|
|
binding.ivClose.setOnClickListener { dismiss() }
|
|
binding.ivGoodsSearch.setOnClickListener { searchGoods() }
|
|
binding.etInputGoods.let { v ->
|
|
v.addOnActionSearchListener {
|
|
searchGoods()
|
|
}
|
|
}
|
|
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
|
|
searchGoods()
|
|
}
|
|
it.setOnLoadMoreListener {
|
|
searchGoods()
|
|
}
|
|
}
|
|
getCollectGoods()
|
|
}
|
|
|
|
private var pageNo = 1
|
|
|
|
companion object {
|
|
private const val PAGE_SIZE = 10
|
|
}
|
|
|
|
@SuppressLint("NotifyDataSetChanged")
|
|
private fun searchGoods() {
|
|
val searchName = binding.etInputGoods.text.toString().trim()
|
|
if (searchName.isBlank()) {
|
|
activity?.toast("请输入物品名称")
|
|
finishRefresh()
|
|
return
|
|
}
|
|
activity?.searchGoods(
|
|
searchName = searchName,
|
|
pageNo = pageNo,
|
|
pageSize = PAGE_SIZE
|
|
) { records ->
|
|
finishRefresh()
|
|
binding.etInputGoods.hideKeyboard()
|
|
if (pageNo == 1) {
|
|
list.clear()
|
|
}
|
|
list.addAll(records)
|
|
if (list.isEmpty()) {
|
|
loadEmptyView()
|
|
return@searchGoods
|
|
}
|
|
val enableLoadMore = records.size >= PAGE_SIZE
|
|
binding.refreshLayout.setEnableLoadMore(enableLoadMore)
|
|
if (enableLoadMore) {
|
|
pageNo++
|
|
}
|
|
binding.rvSearch.layoutManager =
|
|
GridLayoutManager(context, 2, GridLayoutManager.VERTICAL, false)
|
|
searchAdapter.notifyDataSetChanged()
|
|
}
|
|
}
|
|
|
|
private fun loadEmptyView() {
|
|
binding.rvSearch.layoutManager =
|
|
LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
|
|
if (emptyBinding == null) {
|
|
emptyBinding = LayoutEmptySearchBinding.inflate(inflater, binding.rvSearch, false)
|
|
}
|
|
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 = "请确认是否删除该物品?", 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.removeAt(position)
|
|
searchAdapter.notifyDataSetChanged()
|
|
activity?.toast("删除成功")
|
|
}
|
|
}.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() {
|
|
if (box == null) {
|
|
box = ObjectBox.boxStore.boxFor(Food::class)
|
|
}
|
|
box?.all?.distinctBy { it.name }?.forEach {
|
|
list.add((SearchGoodsInfo.Record(goodsName = it.name)))
|
|
}
|
|
binding.rvSearch.layoutManager = GridLayoutManager(activity, 2, LinearLayoutManager.VERTICAL, false)
|
|
searchAdapter.notifyDataSetChanged()
|
|
if (list.isEmpty()) {
|
|
loadEmptyView()
|
|
}
|
|
}
|
|
|
|
} |