refactor(dialog): 重构 FoodSearchDialog 和 SeasoningSearchDialog 继承 BottomSheetDialog
This commit is contained in:
@@ -22,114 +22,94 @@ import com.shuwei.dish.match.utils.ext.addOnActionSearchListener
|
|||||||
import com.shuwei.dish.match.utils.ext.toast
|
import com.shuwei.dish.match.utils.ext.toast
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 食材搜索弹窗
|
* 食材搜索弹窗,继承 BottomSheetDialog 确保只初始化一次
|
||||||
|
* @param activity 宿主 Activity
|
||||||
|
* @param defGoodsName 默认填充的食材名称(可选)
|
||||||
|
* @param onItemSelected 用户点击某一食材时的回调
|
||||||
*/
|
*/
|
||||||
class FoodSearchDialog(
|
class FoodSearchDialog(
|
||||||
private var defGoodsName: String? = null
|
private val activity: BaseActivity,
|
||||||
) {
|
private val defGoodsName: String? = null,
|
||||||
companion object {
|
private val onItemSelected: (item: CookFoodGoodsEntity) -> Unit
|
||||||
const val TAG = "BottomDialog"
|
) : BottomSheetDialog(activity, R.style.BottomSheet) {
|
||||||
|
|
||||||
|
private val binding = DialogFoodSearchBinding.inflate(LayoutInflater.from(activity))
|
||||||
|
private val list = mutableListOf<CookFoodGoodsEntity>()
|
||||||
|
private val adapter = GoodsInfoSearchAdapter(list).apply {
|
||||||
|
setOnItemClickListener { _, _, position ->
|
||||||
|
list[position].isClicked = true
|
||||||
|
notifyItemChanged(position)
|
||||||
|
Handler(Looper.getMainLooper()).postDelayed({
|
||||||
|
onItemSelected(list[position])
|
||||||
|
dismiss()
|
||||||
|
}, 300)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private var list = mutableListOf<CookFoodGoodsEntity>()
|
|
||||||
|
|
||||||
private lateinit var binding: DialogFoodSearchBinding
|
|
||||||
private lateinit var searchAdapter: GoodsInfoSearchAdapter
|
|
||||||
private var goodsName: String? = null
|
private var goodsName: String? = null
|
||||||
|
private var pageNo = 1
|
||||||
|
private val pageSize = 50
|
||||||
|
|
||||||
private var activity: BaseActivity? = null
|
init {
|
||||||
|
setContentView(binding.root)
|
||||||
|
setCancelable(true)
|
||||||
|
behavior.skipCollapsed = false
|
||||||
|
setOnDismissListener { activity.hideStatusBar() }
|
||||||
|
|
||||||
fun show(activity: BaseActivity, callback: (item: CookFoodGoodsEntity) -> Unit) {
|
|
||||||
this.activity = activity
|
|
||||||
//直接使用BottomSheetDialog
|
|
||||||
var dialog: BottomSheetDialog? = null
|
|
||||||
val inflater = LayoutInflater.from(activity)
|
|
||||||
binding = DialogFoodSearchBinding.inflate(inflater)
|
|
||||||
dialog = BottomSheetDialog(activity, R.style.BottomSheet).apply {
|
|
||||||
setContentView(binding.root)
|
|
||||||
setCancelable(true)
|
|
||||||
behavior.skipCollapsed = false // 跳过折叠状态
|
|
||||||
// behavior.peekHeight = 880.dp
|
|
||||||
// window?.setLayout(-1, 880.dp)
|
|
||||||
show()
|
|
||||||
setOnDismissListener {
|
|
||||||
activity.hideStatusBar()
|
|
||||||
}
|
|
||||||
binding.etSheetInput.requestFocus()
|
|
||||||
}
|
|
||||||
binding.tvSheetName.text = "食材检索"
|
binding.tvSheetName.text = "食材检索"
|
||||||
|
|
||||||
|
// 输入框:清空时重置列表
|
||||||
binding.etSheetInput.run {
|
binding.etSheetInput.run {
|
||||||
hint = "输入食材名称或速记码"
|
hint = "输入食材名称或速记码"
|
||||||
addTextChangedListener(afterTextChanged = { it: Editable? ->
|
addTextChangedListener(
|
||||||
if (it.isNullOrBlank()) {
|
onTextChanged = { text, _, _, _ -> goodsName = text.toString() },
|
||||||
list.clear()
|
afterTextChanged = { it: Editable? ->
|
||||||
searchAdapter.notifyDataSetChanged()
|
if (it.isNullOrBlank()) {
|
||||||
|
list.clear()
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
}
|
addOnActionSearchListener { searchGoods(this) }
|
||||||
searchAdapter = GoodsInfoSearchAdapter(list).apply {
|
|
||||||
setOnItemClickListener { _, _, position ->
|
|
||||||
list[position].isClicked = true
|
|
||||||
notifyItemChanged(position)
|
|
||||||
Handler(Looper.getMainLooper()).postDelayed({
|
|
||||||
callback(list[position])
|
|
||||||
dialog?.dismiss()
|
|
||||||
}, 300)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RecyclerView 初始化及滑动冲突处理
|
||||||
binding.recyclerView.run {
|
binding.recyclerView.run {
|
||||||
layoutManager = GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false)
|
layoutManager = GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false)
|
||||||
adapter = searchAdapter
|
adapter = this@FoodSearchDialog.adapter
|
||||||
addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
||||||
override fun onScrollStateChanged(rv: RecyclerView, newState: Int) {
|
|
||||||
super.onScrollStateChanged(rv, newState);
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) {
|
override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) {
|
||||||
//解决RecyclerView和smartRefreshLayout滑动冲突问题
|
|
||||||
super.onScrolled(rv, dx, dy)
|
super.onScrolled(rv, dx, dy)
|
||||||
|
// 解决 RecyclerView 与 SmartRefreshLayout 滑动冲突
|
||||||
val topRowVerticalPosition = if (rv.isEmpty()) 0 else rv.getChildAt(0).top
|
val topRowVerticalPosition = if (rv.isEmpty()) 0 else rv.getChildAt(0).top
|
||||||
binding.refreshLayout.setNestedScrollingEnabled(topRowVerticalPosition >= 0)
|
binding.refreshLayout.setNestedScrollingEnabled(topRowVerticalPosition >= 0)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
binding.refreshLayout.setOnRefreshListener {
|
|
||||||
pageNo = 1
|
binding.refreshLayout.run {
|
||||||
getGoodsList()
|
setEnableRefresh(false)
|
||||||
|
setEnableLoadMore(false)
|
||||||
|
setOnRefreshListener { pageNo = 1; getGoodsList() }
|
||||||
|
setOnLoadMoreListener { getGoodsList() }
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.refreshLayout.setOnLoadMoreListener {
|
binding.ivSearch.setOnClickListener { searchGoods(it) }
|
||||||
getGoodsList()
|
binding.root.setOnClickListener { KeyboardUtil.hideKeyboard(it.context, it) }
|
||||||
}
|
|
||||||
binding.etSheetInput.addTextChangedListener(
|
|
||||||
onTextChanged = { text, start, before, count ->
|
|
||||||
goodsName = text.toString()
|
|
||||||
})
|
|
||||||
binding.ivSearch.setOnClickListener { v ->
|
|
||||||
searchGoods(v)
|
|
||||||
}
|
|
||||||
binding.root.setOnClickListener { v ->
|
|
||||||
KeyboardUtil.hideKeyboard(v.context, v)
|
|
||||||
}
|
|
||||||
binding.etSheetInput.let { v ->
|
|
||||||
v.addOnActionSearchListener {
|
|
||||||
searchGoods(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
binding.refreshLayout.setEnableRefresh(false)
|
|
||||||
binding.refreshLayout.setEnableLoadMore(false)
|
|
||||||
|
|
||||||
if (defGoodsName.isNullOrBlank().not()) {
|
// 若有默认食材名称,自动填充并触发搜索
|
||||||
binding.etSheetInput.let {
|
if (!defGoodsName.isNullOrBlank()) {
|
||||||
it.setText(defGoodsName)
|
binding.etSheetInput.setText(defGoodsName)
|
||||||
searchGoods(it)
|
searchGoods(binding.etSheetInput)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发搜索:校验输入、隐藏键盘、发起请求
|
||||||
|
*/
|
||||||
private fun searchGoods(v: View) {
|
private fun searchGoods(v: View) {
|
||||||
if (goodsName.isNullOrBlank()) {
|
if (goodsName.isNullOrBlank()) {
|
||||||
activity?.toast("请${binding.etSheetInput.hint}")
|
activity.toast("请${binding.etSheetInput.hint}")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pageNo = 1
|
pageNo = 1
|
||||||
@@ -137,109 +117,47 @@ class FoodSearchDialog(
|
|||||||
KeyboardUtil.hideKeyboard(v.context, v)
|
KeyboardUtil.hideKeyboard(v.context, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var pageNo = 1
|
|
||||||
private val pageSize = 50
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 请求食材列表
|
||||||
*/
|
*/
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
|
||||||
private fun getGoodsList() {
|
private fun getGoodsList() {
|
||||||
// val pathSegmentList = UrlConfig.QUERY_GOODS_LIST.split(delimiters = arrayOf("/"), limit = 1)
|
|
||||||
// val queryParams = mutableMapOf(
|
|
||||||
// "goodsType" to "0",
|
|
||||||
// "canteenId" to BaseApp.canteenId,
|
|
||||||
// "pageNo" to "$pageNo",
|
|
||||||
// "pageSize" to "$pageSize"
|
|
||||||
// )
|
|
||||||
// if (goodsName.isNullOrBlank().not()) {
|
|
||||||
// queryParams.put("name", goodsName!!)
|
|
||||||
// }
|
|
||||||
// val sb = StringBuilder().apply {
|
|
||||||
// append(UrlConfig.QUERY_GOODS_LIST)
|
|
||||||
// append("?")
|
|
||||||
// queryParams.forEach { (key, value) ->
|
|
||||||
// append("$key=$value&")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// sb.deleteCharAt(sb.length - 1)
|
|
||||||
// Log.d(TAG, "getGoodsList: url = $sb")
|
|
||||||
// HttpUtil.get(
|
|
||||||
// url = sb.toString(),
|
|
||||||
// doSuccess = {
|
|
||||||
// val json = it.toJsonString()
|
|
||||||
// Log.d(TAG, "getGoodsList: json=$json")
|
|
||||||
// val recordBean: GoodsRecordBean? = json.toObject<GoodsRecordBean>()
|
|
||||||
// if (recordBean == null || recordBean.records.isNullOrEmpty()) {
|
|
||||||
// activity?.toast("暂未搜索到食材信息")
|
|
||||||
// return@get
|
|
||||||
// }
|
|
||||||
// val records = recordBean.records
|
|
||||||
// if (pageNo == 1) {
|
|
||||||
// list.clear()
|
|
||||||
// }
|
|
||||||
// list.addAll(records!!)
|
|
||||||
// searchAdapter.notifyDataSetChanged()
|
|
||||||
// if (pageNo == 1) {
|
|
||||||
// binding.refreshLayout.finishRefresh(1200)
|
|
||||||
// } else {
|
|
||||||
// binding.refreshLayout.finishLoadMore(1200)
|
|
||||||
// }
|
|
||||||
// val isLoadMoreEnable = records.size >= pageSize
|
|
||||||
// binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
|
||||||
// if (isLoadMoreEnable) {
|
|
||||||
// pageNo++
|
|
||||||
// }
|
|
||||||
// }, doFailure = { code, msg ->
|
|
||||||
// activity?.toast(msg)
|
|
||||||
// if (pageNo == 1) {
|
|
||||||
// binding.refreshLayout.finishRefresh(1200)
|
|
||||||
// } else {
|
|
||||||
// binding.refreshLayout.finishLoadMore(1200)
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
val param = mutableMapOf<String, Any>(
|
val param = mutableMapOf<String, Any>(
|
||||||
"goodsType" to "0",
|
"goodsType" to "0",
|
||||||
"placeId" to BaseApp.canteenId,
|
"placeId" to BaseApp.canteenId,
|
||||||
"pageNum" to pageNo,
|
"pageNum" to pageNo,
|
||||||
"pageSize" to pageSize
|
"pageSize" to pageSize
|
||||||
)
|
)
|
||||||
if (goodsName.isNullOrBlank().not()) {
|
if (!goodsName.isNullOrBlank()) {
|
||||||
param.put("goodsName", goodsName!!)
|
param["goodsName"] = goodsName!!
|
||||||
}
|
}
|
||||||
activity?.queryGoodsList(param = param, onSuccess = {
|
activity.queryGoodsList(param = param, onSuccess = {
|
||||||
loadGoodsList(it)
|
loadGoodsList(it)
|
||||||
}, onFailure = { code, msg ->
|
}, onFailure = { _, msg ->
|
||||||
activity?.toast(msg)
|
activity.toast(msg)
|
||||||
finishRefresh()
|
finishRefresh()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun finishRefresh() {
|
/**
|
||||||
if (pageNo == 1) {
|
* 将返回数据填充到列表
|
||||||
binding.refreshLayout.finishRefresh()
|
*/
|
||||||
} else {
|
|
||||||
binding.refreshLayout.finishLoadMore()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
private fun loadGoodsList(records: MutableList<CookFoodGoodsEntity>?) {
|
private fun loadGoodsList(records: MutableList<CookFoodGoodsEntity>?) {
|
||||||
finishRefresh()
|
finishRefresh()
|
||||||
if (records.isNullOrEmpty()) {
|
if (records.isNullOrEmpty()) {
|
||||||
activity?.toast("暂未搜索到食材信息")
|
activity.toast("暂未搜索到食材信息")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (pageNo == 1) {
|
if (pageNo == 1) list.clear()
|
||||||
list.clear()
|
|
||||||
}
|
|
||||||
list.addAll(records)
|
list.addAll(records)
|
||||||
searchAdapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
val isLoadMoreEnable = records.size >= pageSize
|
val isLoadMoreEnable = records.size >= pageSize
|
||||||
binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
||||||
if (isLoadMoreEnable) {
|
if (isLoadMoreEnable) pageNo++
|
||||||
pageNo++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun finishRefresh() {
|
||||||
|
if (pageNo == 1) binding.refreshLayout.finishRefresh()
|
||||||
|
else binding.refreshLayout.finishLoadMore()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,7 +4,6 @@ import android.annotation.SuppressLint
|
|||||||
import android.graphics.Typeface
|
import android.graphics.Typeface
|
||||||
import android.os.Handler
|
import android.os.Handler
|
||||||
import android.os.Looper
|
import android.os.Looper
|
||||||
import android.text.Editable
|
|
||||||
import android.text.SpannableStringBuilder
|
import android.text.SpannableStringBuilder
|
||||||
import android.text.style.AbsoluteSizeSpan
|
import android.text.style.AbsoluteSizeSpan
|
||||||
import android.text.style.ForegroundColorSpan
|
import android.text.style.ForegroundColorSpan
|
||||||
@@ -38,164 +37,130 @@ import com.shuwei.dish.match.utils.ext.roundedDecimalPlace
|
|||||||
import com.shuwei.dish.match.utils.ext.toast
|
import com.shuwei.dish.match.utils.ext.toast
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 调料搜索弹窗
|
* 调料搜索弹窗,继承 BottomSheetDialog 确保只初始化一次
|
||||||
|
* @param activity 宿主 Activity
|
||||||
|
* @param weighIndex 秤的索引
|
||||||
|
* @param weighAddress 秤的地址,用于监听重量和去皮
|
||||||
|
* @param clickName 默认填充的调料名称(可选)
|
||||||
|
* @param onItemSelected 用户点击某一调料时的回调,携带当前重量
|
||||||
*/
|
*/
|
||||||
class SeasoningSearchDialog(
|
class SeasoningSearchDialog(
|
||||||
private var weighIndex: Int,
|
private val activity: BaseActivity,
|
||||||
private var weighAddress: Int,
|
private val weighIndex: Int,
|
||||||
private var clickName: String? = null
|
private val weighAddress: Int,
|
||||||
) {
|
private val clickName: String? = null,
|
||||||
|
private val onItemSelected: (item: SeasoningEntity) -> Unit
|
||||||
|
) : BottomSheetDialog(activity, R.style.BottomSheet) {
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
const val TAG = "BottomDialog2"
|
const val TAG = "BottomDialog2"
|
||||||
}
|
}
|
||||||
|
|
||||||
private var list = mutableListOf<SeasoningEntity>()
|
private val binding = DialogSeasoningSearchBinding.inflate(LayoutInflater.from(activity))
|
||||||
|
private val list = mutableListOf<SeasoningEntity>()
|
||||||
|
private val adapter = SeasoningSearchAdapter(list).apply {
|
||||||
|
setOnItemClickListener { _, _, position ->
|
||||||
|
list[position].isClicked = true
|
||||||
|
notifyItemChanged(position)
|
||||||
|
Handler(Looper.getMainLooper()).postDelayed({
|
||||||
|
onItemSelected(list[position].also { it.useWeight = currentWeight })
|
||||||
|
dismiss()
|
||||||
|
}, 300)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private lateinit var binding: DialogSeasoningSearchBinding
|
|
||||||
private lateinit var searchAdapter: SeasoningSearchAdapter
|
|
||||||
private var goodsName: String? = null
|
private var goodsName: String? = null
|
||||||
|
private var currentWeight = 0.0
|
||||||
|
private var pageNo = 1
|
||||||
|
private val pageSize = 50
|
||||||
|
|
||||||
private var activity: BaseActivity? = null
|
init {
|
||||||
|
setContentView(binding.root)
|
||||||
|
setCancelable(true)
|
||||||
|
behavior.skipCollapsed = false
|
||||||
|
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
|
||||||
|
window?.setGravity(Gravity.BOTTOM)
|
||||||
|
window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||||
|
window?.setWindowAnimations(R.style.DialogSoftInputAnimation)
|
||||||
|
window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
||||||
|
|
||||||
fun show(activity: BaseActivity, callback: (item: SeasoningEntity) -> Unit) {
|
setOnDismissListener {
|
||||||
this.activity = activity
|
// 弹窗关闭时移除重量监听,避免内存泄漏
|
||||||
//直接使用BottomSheetDialog
|
WeightUtil.removeWeightListener(TAG)
|
||||||
var dialog: BottomSheetDialog? = null
|
activity.hideStatusBar()
|
||||||
val inflater = LayoutInflater.from(activity)
|
}
|
||||||
binding = DialogSeasoningSearchBinding.inflate(inflater)
|
|
||||||
|
|
||||||
|
// 输入框:清空时重置列表
|
||||||
binding.etSheetInput.run {
|
binding.etSheetInput.run {
|
||||||
hint = "输入调料名称"
|
hint = "输入调料名称"
|
||||||
addTextChangedListener(afterTextChanged = { it: Editable? ->
|
addTextChangedListener(
|
||||||
if (it.isNullOrBlank()) {
|
onTextChanged = { text, _, _, _ -> goodsName = text.toString() },
|
||||||
list.clear()
|
afterTextChanged = {
|
||||||
searchAdapter.notifyDataSetChanged()
|
if (it.isNullOrBlank()) {
|
||||||
|
list.clear()
|
||||||
|
adapter.notifyDataSetChanged()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
)
|
||||||
}
|
addOnActionSearchListener { searchGoods(this) }
|
||||||
searchAdapter = SeasoningSearchAdapter(list).apply {
|
|
||||||
setOnItemClickListener { _, _, position ->
|
|
||||||
list[position].isClicked = true
|
|
||||||
notifyItemChanged(position)
|
|
||||||
Handler(Looper.getMainLooper()).postDelayed({
|
|
||||||
callback(list[position].also { it.useWeight = currentWeight })
|
|
||||||
dialog?.dismiss()
|
|
||||||
}, 300)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// RecyclerView 初始化及滑动冲突处理
|
||||||
binding.recyclerView.run {
|
binding.recyclerView.run {
|
||||||
layoutManager = GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false)
|
layoutManager = GridLayoutManager(activity, 2, GridLayoutManager.VERTICAL, false)
|
||||||
adapter = searchAdapter
|
adapter = this@SeasoningSearchDialog.adapter
|
||||||
addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
addOnScrollListener(object : RecyclerView.OnScrollListener() {
|
||||||
override fun onScrollStateChanged(rv: RecyclerView, newState: Int) {
|
|
||||||
super.onScrollStateChanged(rv, newState);
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) {
|
override fun onScrolled(rv: RecyclerView, dx: Int, dy: Int) {
|
||||||
//解决RecyclerView和smartRefreshLayout滑动冲突问题
|
|
||||||
super.onScrolled(rv, dx, dy)
|
super.onScrolled(rv, dx, dy)
|
||||||
|
// 解决 RecyclerView 与 SmartRefreshLayout 滑动冲突
|
||||||
val topRowVerticalPosition = if (rv.isEmpty()) 0 else rv.getChildAt(0).top
|
val topRowVerticalPosition = if (rv.isEmpty()) 0 else rv.getChildAt(0).top
|
||||||
binding.refreshLayout.setNestedScrollingEnabled(topRowVerticalPosition >= 0)
|
binding.refreshLayout.setNestedScrollingEnabled(topRowVerticalPosition >= 0)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
dialog = BottomSheetDialog(activity, R.style.BottomSheet).apply {
|
|
||||||
setContentView(binding.root)
|
binding.refreshLayout.run {
|
||||||
setCancelable(true)
|
setEnableRefresh(false)
|
||||||
behavior.skipCollapsed = false // 跳过折叠状态
|
setEnableLoadMore(false)
|
||||||
//// behavior.peekHeight = 880.dp
|
setOnRefreshListener { pageNo = 1; getGoodsList() }
|
||||||
//// window?.setLayout(-1, 880.dp)
|
setOnLoadMoreListener { getGoodsList() }
|
||||||
// window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
|
|
||||||
window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
|
|
||||||
//or WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN
|
|
||||||
//or WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING // 禁用额外平移
|
|
||||||
window?.setGravity(Gravity.BOTTOM)
|
|
||||||
window?.setLayout(
|
|
||||||
ViewGroup.LayoutParams.MATCH_PARENT,
|
|
||||||
ViewGroup.LayoutParams.WRAP_CONTENT
|
|
||||||
)
|
|
||||||
window?.setWindowAnimations(R.style.DialogSoftInputAnimation)
|
|
||||||
window?.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
|
|
||||||
show()
|
|
||||||
// val onGlobalLayoutListener = ViewTreeObserver.OnGlobalLayoutListener {
|
|
||||||
// val r = Rect()
|
|
||||||
// binding.root.getWindowVisibleDisplayFrame(r)
|
|
||||||
// val screenHeight = binding.root.rootView.height
|
|
||||||
// val keyboardHeight = screenHeight - r.bottom
|
|
||||||
//
|
|
||||||
// // 软键盘弹出(高度>200dp)
|
|
||||||
// if (keyboardHeight > 200.dp) {
|
|
||||||
// // 调整 Dialog 底部边距,避开软键盘
|
|
||||||
// val layoutParams = binding.root.layoutParams as ViewGroup.MarginLayoutParams
|
|
||||||
//// layoutParams.bottomMargin = keyboardHeight
|
|
||||||
// layoutParams.bottomMargin = 200.dp
|
|
||||||
// binding.root.layoutParams = layoutParams
|
|
||||||
// } else {
|
|
||||||
// // 软键盘收起,恢复边距
|
|
||||||
// val layoutParams = binding.root.layoutParams as ViewGroup.MarginLayoutParams
|
|
||||||
// layoutParams.bottomMargin = 0
|
|
||||||
// binding.root.layoutParams = layoutParams
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
setOnDismissListener {
|
|
||||||
activity.hideStatusBar()
|
|
||||||
// binding.root.viewTreeObserver.removeOnGlobalLayoutListener(onGlobalLayoutListener)
|
|
||||||
}
|
|
||||||
// binding.root.viewTreeObserver.addOnGlobalLayoutListener(onGlobalLayoutListener)
|
|
||||||
}
|
|
||||||
binding.refreshLayout.setOnRefreshListener {
|
|
||||||
pageNo = 1
|
|
||||||
getGoodsList()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
binding.refreshLayout.setOnLoadMoreListener {
|
binding.ivSearch.setOnClickListener { searchGoods(it) }
|
||||||
getGoodsList()
|
binding.root.setOnClickListener { KeyboardUtil.hideKeyboard(it.context, it) }
|
||||||
}
|
|
||||||
binding.etSheetInput.addTextChangedListener(
|
// 去皮按钮
|
||||||
onTextChanged = { text, start, before, count ->
|
|
||||||
goodsName = text.toString()
|
|
||||||
})
|
|
||||||
binding.ivSearch.setOnClickListener { v ->
|
|
||||||
searchGoods(v)
|
|
||||||
}
|
|
||||||
binding.root.setOnClickListener { v ->
|
|
||||||
KeyboardUtil.hideKeyboard(v.context, v)
|
|
||||||
}
|
|
||||||
binding.etSheetInput.let { v ->
|
|
||||||
v.addOnActionSearchListener {
|
|
||||||
searchGoods(v)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
binding.refreshLayout.setEnableRefresh(false)
|
|
||||||
binding.refreshLayout.setEnableLoadMore(false)
|
|
||||||
binding.tvClear.setOnClickListener {
|
binding.tvClear.setOnClickListener {
|
||||||
Log.d(TAG, "show: weighAddress=$weighAddress")
|
Log.d(TAG, "tareTwo: weighAddress=$weighAddress")
|
||||||
WeightUtil.tareTwo(weighAddress)
|
WeightUtil.tareTwo(weighAddress)
|
||||||
// repeat(14) { num->
|
|
||||||
// Log.d(TAG, "show: num=$num")
|
|
||||||
// WeightUtil.tareTwo(num+1)
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 监听对应秤的重量变化
|
||||||
WeightUtil.addWeightListener(
|
WeightUtil.addWeightListener(
|
||||||
weightKey = TAG,
|
weightKey = TAG,
|
||||||
getWeight = { address, state, weight ->
|
getWeight = { address, _, weight ->
|
||||||
if (weighAddress == address) {
|
if (weighAddress == address) {
|
||||||
this.currentWeight = weight.toDouble()
|
currentWeight = weight.toDouble()
|
||||||
binding.tvWeight.text = getTextSpan(weight)
|
binding.tvWeight.text = getTextSpan(weight)
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
)
|
||||||
binding.tvWeight.text = getTextSpan(0.0)
|
binding.tvWeight.text = getTextSpan(0.0)
|
||||||
|
|
||||||
if (clickName.isNullOrBlank().not()) {
|
// 若有默认调料名称,自动填充并触发搜索
|
||||||
binding.etSheetInput.setText(clickName!!.trim())
|
if (!clickName.isNullOrBlank()) {
|
||||||
|
binding.etSheetInput.setText(clickName.trim())
|
||||||
pageNo = 1
|
pageNo = 1
|
||||||
getGoodsList()
|
getGoodsList()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 触发搜索:校验输入、隐藏键盘、发起请求
|
||||||
|
*/
|
||||||
private fun searchGoods(v: View) {
|
private fun searchGoods(v: View) {
|
||||||
if (goodsName.isNullOrBlank()) {
|
if (goodsName.isNullOrBlank()) {
|
||||||
activity?.toast("请${binding.etSheetInput.hint}")
|
activity.toast("请${binding.etSheetInput.hint}")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pageNo = 1
|
pageNo = 1
|
||||||
@@ -203,132 +168,63 @@ class SeasoningSearchDialog(
|
|||||||
KeyboardUtil.hideKeyboard(v.context, v)
|
KeyboardUtil.hideKeyboard(v.context, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
private var currentWeight = 0.toDouble()
|
|
||||||
private var pageNo = 1
|
|
||||||
private val pageSize = 50
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* 请求调料列表
|
||||||
*/
|
*/
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
|
||||||
private fun getGoodsList() {
|
private fun getGoodsList() {
|
||||||
// val queryParams = mutableMapOf(
|
|
||||||
// "goodsType" to "1",
|
|
||||||
// "canteenId" to BaseApp.canteenId,
|
|
||||||
// "pageNo" to "$pageNo",
|
|
||||||
// "pageSize" to "$pageSize"
|
|
||||||
// )
|
|
||||||
// if (goodsName.isNullOrBlank().not()) {
|
|
||||||
// queryParams.put("name", goodsName!!)
|
|
||||||
// }
|
|
||||||
// val sb = StringBuilder().apply {
|
|
||||||
// append(UrlConfig.QUERY_GOODS_LIST)
|
|
||||||
// append("?")
|
|
||||||
// queryParams.forEach { (key, value) ->
|
|
||||||
// append("$key=$value&")
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// sb.deleteCharAt(sb.length - 1)
|
|
||||||
// Log.d(TAG, "getGoodsList: url = $sb")
|
|
||||||
// HttpUtil.get(
|
|
||||||
// url = sb.toString(),
|
|
||||||
// doSuccess = {
|
|
||||||
// val json = it.toJsonString()
|
|
||||||
// Log.d(TAG, "getGoodsList: json=$json")
|
|
||||||
// val recordBean: SeasoningRecordBean? = json.toObject<SeasoningRecordBean>()
|
|
||||||
// if (recordBean == null || recordBean.records.isNullOrEmpty()) {
|
|
||||||
// activity?.toast( "暂未搜索到调料信息")
|
|
||||||
// return@get
|
|
||||||
// }
|
|
||||||
// val records = recordBean.records
|
|
||||||
// if (pageNo == 1) {
|
|
||||||
// list.clear()
|
|
||||||
// }
|
|
||||||
// list.addAll(records!!)
|
|
||||||
// searchAdapter.notifyDataSetChanged()
|
|
||||||
// if (pageNo == 1) {
|
|
||||||
// binding.refreshLayout.finishRefresh(1200)
|
|
||||||
// } else {
|
|
||||||
// binding.refreshLayout.finishLoadMore(1200)
|
|
||||||
// }
|
|
||||||
// val isLoadMoreEnable = records.size >= pageSize
|
|
||||||
// binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
|
||||||
// if (isLoadMoreEnable) {
|
|
||||||
// pageNo++
|
|
||||||
// }
|
|
||||||
// }, doFailure = { code, msg ->
|
|
||||||
// activity?.toast(msg)
|
|
||||||
// if (pageNo == 1) {
|
|
||||||
// binding.refreshLayout.finishRefresh(1200)
|
|
||||||
// } else {
|
|
||||||
// binding.refreshLayout.finishLoadMore(1200)
|
|
||||||
// }
|
|
||||||
// })
|
|
||||||
|
|
||||||
|
|
||||||
val param = mutableMapOf<String, Any>(
|
val param = mutableMapOf<String, Any>(
|
||||||
"goodsType" to "1",
|
"goodsType" to "1",
|
||||||
"placeId" to BaseApp.canteenId,
|
"placeId" to BaseApp.canteenId,
|
||||||
"pageNum" to pageNo,
|
"pageNum" to pageNo,
|
||||||
"pageSize" to pageSize
|
"pageSize" to pageSize
|
||||||
)
|
)
|
||||||
if (goodsName.isNullOrBlank().not()) {
|
if (!goodsName.isNullOrBlank()) {
|
||||||
param.put("goodsName", goodsName!!)
|
param["goodsName"] = goodsName!!
|
||||||
}
|
}
|
||||||
activity?.querySeasoningList(
|
activity.querySeasoningList(param = param, onSuccess = {
|
||||||
param = param,
|
loadGoodsList(it)
|
||||||
onSuccess = {
|
}, onFailure = { _, msg ->
|
||||||
loadGoodsList(it)
|
activity.toast(msg)
|
||||||
},
|
finishRefresh()
|
||||||
onFailure = { code, msg ->
|
})
|
||||||
activity?.toast(msg)
|
|
||||||
finishRefresh()
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将返回数据填充到列表
|
||||||
|
*/
|
||||||
@SuppressLint("NotifyDataSetChanged")
|
@SuppressLint("NotifyDataSetChanged")
|
||||||
private fun loadGoodsList(records: MutableList<SeasoningEntity>?) {
|
private fun loadGoodsList(records: MutableList<SeasoningEntity>?) {
|
||||||
finishRefresh()
|
finishRefresh()
|
||||||
if (records.isNullOrEmpty()) {
|
if (records.isNullOrEmpty()) {
|
||||||
activity?.toast("暂未搜索到调料信息")
|
activity.toast("暂未搜索到调料信息")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if (pageNo == 1) {
|
if (pageNo == 1) list.clear()
|
||||||
list.clear()
|
|
||||||
}
|
|
||||||
list.addAll(records)
|
list.addAll(records)
|
||||||
searchAdapter.notifyDataSetChanged()
|
adapter.notifyDataSetChanged()
|
||||||
val isLoadMoreEnable = records.size >= pageSize
|
val isLoadMoreEnable = records.size >= pageSize
|
||||||
binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
binding.refreshLayout.setEnableLoadMore(isLoadMoreEnable)
|
||||||
if (isLoadMoreEnable) {
|
if (isLoadMoreEnable) pageNo++
|
||||||
pageNo++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun finishRefresh() {
|
private fun finishRefresh() {
|
||||||
if (pageNo == 1) {
|
if (pageNo == 1) binding.refreshLayout.finishRefresh()
|
||||||
binding.refreshLayout.finishRefresh()
|
else binding.refreshLayout.finishLoadMore()
|
||||||
} else {
|
|
||||||
binding.refreshLayout.finishLoadMore()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成重量显示的富文本:数值大字加粗,单位小字灰色
|
||||||
|
*/
|
||||||
fun getTextSpan(weight: Double): SpannableStringBuilder {
|
fun getTextSpan(weight: Double): SpannableStringBuilder {
|
||||||
var topWeight = "$weight"
|
val topWeight = if (weight >= 1000) "${(weight / 1000).roundedDecimalPlace(3)}" else "$weight"
|
||||||
var bottomUnit = "克"
|
val bottomUnit = if (weight >= 1000) "千克" else "克"
|
||||||
if (weight >= 1000) {
|
|
||||||
topWeight = "${(weight / 1000).roundedDecimalPlace(3)}"
|
|
||||||
bottomUnit = "千克"
|
|
||||||
}
|
|
||||||
return buildSpannableString {
|
return buildSpannableString {
|
||||||
appendText(
|
appendText(
|
||||||
topWeight,
|
topWeight,
|
||||||
ForegroundColorSpan("#000000".toColorInt()),
|
ForegroundColorSpan("#000000".toColorInt()),
|
||||||
StyleSpan(Typeface.BOLD),
|
StyleSpan(Typeface.BOLD),
|
||||||
AbsoluteSizeSpan(72, true),
|
AbsoluteSizeSpan(72, true),
|
||||||
LineHeightSpan { text, start, end, spanstartv, v, fm ->
|
LineHeightSpan { _, _, _, _, _, fm -> fm.descent += 10.dp }
|
||||||
fm.descent += 10.dp // 增加行间距
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
append("\n")
|
append("\n")
|
||||||
appendText(
|
appendText(
|
||||||
@@ -339,6 +235,3 @@ class SeasoningSearchDialog(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -251,10 +251,11 @@ class DeviceConfigActivity : BaseActivity() {
|
|||||||
val currentAddress = AddressUtil.getWeighAddressArray().get(sort)
|
val currentAddress = AddressUtil.getWeighAddressArray().get(sort)
|
||||||
// val seasoningEntity = list.first{it.sort == sort}
|
// val seasoningEntity = list.first{it.sort == sort}
|
||||||
SeasoningSearchDialog(
|
SeasoningSearchDialog(
|
||||||
|
activity = this,
|
||||||
weighIndex = sort,
|
weighIndex = sort,
|
||||||
weighAddress = currentAddress,
|
weighAddress = currentAddress,
|
||||||
clickName = entity.goodsName
|
clickName = entity.goodsName
|
||||||
).show(this) { item ->
|
) { item ->
|
||||||
syncItem(entity, item)
|
syncItem(entity, item)
|
||||||
//3-调料
|
//3-调料
|
||||||
entity.materialType = 3
|
entity.materialType = 3
|
||||||
@@ -265,7 +266,7 @@ class DeviceConfigActivity : BaseActivity() {
|
|||||||
val frameLayout = gridLayout.findViewWithTag<FrameLayout>(tag)
|
val frameLayout = gridLayout.findViewWithTag<FrameLayout>(tag)
|
||||||
TextCellAdapter.loadLayout(frameLayout, entity)
|
TextCellAdapter.loadLayout(frameLayout, entity)
|
||||||
}
|
}
|
||||||
}
|
}.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun syncItem(oldItem: SeasoningEntity, newItem: SeasoningEntity) {
|
private fun syncItem(oldItem: SeasoningEntity, newItem: SeasoningEntity) {
|
||||||
|
|||||||
@@ -276,11 +276,14 @@ class DishSamplingActivity : BaseActivity() {
|
|||||||
|
|
||||||
private fun searchDishType() {
|
private fun searchDishType() {
|
||||||
val inputText = binding.etInputDishType.text.toString().trim()
|
val inputText = binding.etInputDishType.text.toString().trim()
|
||||||
FoodSearchDialog(defGoodsName = inputText).show(this@DishSamplingActivity) { item ->
|
FoodSearchDialog(
|
||||||
|
activity = this@DishSamplingActivity,
|
||||||
|
defGoodsName = inputText
|
||||||
|
) { item ->
|
||||||
val filterResult = list.firstOrNull { it.goodsName == item.goodsName }
|
val filterResult = list.firstOrNull { it.goodsName == item.goodsName }
|
||||||
if (filterResult != null) {
|
if (filterResult != null) {
|
||||||
toast("不允许重复添加同一食材")
|
toast("不允许重复添加同一食材")
|
||||||
return@show
|
return@FoodSearchDialog
|
||||||
}
|
}
|
||||||
binding.etInputDishType.run {
|
binding.etInputDishType.run {
|
||||||
setText(item.goodsName)
|
setText(item.goodsName)
|
||||||
@@ -297,7 +300,7 @@ class DishSamplingActivity : BaseActivity() {
|
|||||||
// TODO: 待定
|
// TODO: 待定
|
||||||
//allEdible = item.allEdible
|
//allEdible = item.allEdible
|
||||||
}
|
}
|
||||||
}
|
}.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -201,7 +201,7 @@ class PrepareCookActivity : BaseActivity() {
|
|||||||
* 添加食材
|
* 添加食材
|
||||||
*/
|
*/
|
||||||
private fun addFood() {
|
private fun addFood() {
|
||||||
FoodSearchDialog().show(activity = this, callback = foodSelectCallback)
|
FoodSearchDialog(activity = this, onItemSelected = foodSelectCallback).show()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun openSubmitPage() {
|
private fun openSubmitPage() {
|
||||||
|
|||||||
@@ -242,10 +242,11 @@ class DeviceConfigFragment : BaseFragment<FragmentDeviceConfigBinding>() {
|
|||||||
private fun clickGridItem(sort: Int, entity: SeasoningEntity) {
|
private fun clickGridItem(sort: Int, entity: SeasoningEntity) {
|
||||||
val currentAddress = AddressUtil.getWeighAddressArray().get(sort)
|
val currentAddress = AddressUtil.getWeighAddressArray().get(sort)
|
||||||
SeasoningSearchDialog(
|
SeasoningSearchDialog(
|
||||||
|
activity = requireActivity() as BaseActivity,
|
||||||
weighIndex = sort,
|
weighIndex = sort,
|
||||||
weighAddress = currentAddress,
|
weighAddress = currentAddress,
|
||||||
clickName = entity.goodsName
|
clickName = entity.goodsName
|
||||||
).show(requireActivity() as BaseActivity) { item ->
|
) { item ->
|
||||||
syncItem(entity, item)
|
syncItem(entity, item)
|
||||||
// 3-调料
|
// 3-调料
|
||||||
entity.materialType = 3
|
entity.materialType = 3
|
||||||
@@ -256,7 +257,7 @@ class DeviceConfigFragment : BaseFragment<FragmentDeviceConfigBinding>() {
|
|||||||
val frameLayout = gridLayout.findViewWithTag<FrameLayout>(tag)
|
val frameLayout = gridLayout.findViewWithTag<FrameLayout>(tag)
|
||||||
TextCellAdapter.loadLayout(frameLayout, entity)
|
TextCellAdapter.loadLayout(frameLayout, entity)
|
||||||
}
|
}
|
||||||
}
|
}.show()
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Reference in New Issue
Block a user