176 lines
5.7 KiB
Kotlin
176 lines
5.7 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.activity.GoodsListActivity
|
|
import com.sw.inbound.adapter.GoodsSearchAdapter
|
|
import com.sw.inbound.databinding.DialogGoodsSearchBinding
|
|
import com.sw.inbound.databinding.LayoutEmptySearchBinding
|
|
import com.sw.inbound.model.response.SearchGoodsInfo
|
|
import com.sw.inbound.utils.ext.addOnActionSearchListener
|
|
import com.sw.inbound.utils.ext.hideKeyboard
|
|
import com.sw.inbound.utils.ext.toast
|
|
|
|
class GoodsSearchDialog(
|
|
context: Context
|
|
) : BaseDialog(context) {
|
|
|
|
private lateinit var inflater: LayoutInflater
|
|
private var emptyBinding: LayoutEmptySearchBinding? = null
|
|
private lateinit var binding: DialogGoodsSearchBinding
|
|
override fun getRootView(): View {
|
|
inflater = LayoutInflater.from(context)
|
|
binding = DialogGoodsSearchBinding.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.btnAddGoods.setOnClickListener {
|
|
AddGoodsDialog(
|
|
context = context,
|
|
cancelBlock = {},
|
|
confirmBlock = { goodsName->
|
|
pageNo = 1
|
|
binding.etInputGoods.setText(goodsName)
|
|
searchGoods()
|
|
}
|
|
).apply {
|
|
setOnDismissListener {
|
|
//activity?.bindCamera()
|
|
}
|
|
show()
|
|
}
|
|
}
|
|
binding.btnConfirm.setOnClickListener {
|
|
if (list.isEmpty()) {
|
|
context.toast("请搜索物品")
|
|
return@setOnClickListener
|
|
}
|
|
if (clickIndex == -1) {
|
|
context.toast("请选择物品")
|
|
return@setOnClickListener
|
|
}
|
|
GoodsStoreDialog(
|
|
context = context,
|
|
funcType = 1,
|
|
goods = list[clickIndex]
|
|
) {
|
|
activity?.addGoods(it)
|
|
//activity?.startTime = System.currentTimeMillis()
|
|
activity?.goodsRecognizeDialog?.dismiss()
|
|
dismiss()
|
|
}.show()
|
|
}
|
|
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()
|
|
}
|
|
}
|
|
loadEmptyView()
|
|
}
|
|
|
|
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 {
|
|
GoodsSearchAdapter(list).apply {
|
|
isStateViewEnable = true
|
|
setOnItemClickListener { adapter, view, position ->
|
|
this@GoodsSearchDialog.clickIndex = position
|
|
if (list[position].isSelected) {
|
|
return@setOnItemClickListener
|
|
}
|
|
list.forEach { item ->
|
|
item.isSelected = false
|
|
}
|
|
list[position].isSelected = true
|
|
notifyDataSetChanged()
|
|
}
|
|
}
|
|
}
|
|
|
|
private fun finishRefresh() {
|
|
binding.refreshLayout.let {
|
|
if (pageNo == 1) {
|
|
it.finishRefresh(500)
|
|
} else {
|
|
it.finishLoadMore(500)
|
|
}
|
|
}
|
|
}
|
|
} |