feat(activity): 搜索菜品直接刷新列表,切换餐次自动清空搜索框

- jumpSearch 改为调用 FoodListFragment.getDishList(foodName),不再跳转 FoodSearchActivity
- 切换餐次时清空输入框,用 isClearingByTabSwitch 标志避免重复请求
- 手动清空输入框时自动触发查全部数据
- FoodListFragment.getDishList 新增 foodName 参数,支持按名称过滤

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-13 16:22:46 +08:00
co-authored by Claude Sonnet 4.6
parent ce7d2da6d8
commit 67d64848de
2 changed files with 25 additions and 7 deletions
@@ -3,6 +3,7 @@ package com.shuwei.dish.match.ui
import android.content.Intent
import android.os.Bundle
import androidx.activity.addCallback
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import com.shuwei.dish.match.R
@@ -92,6 +93,9 @@ class CookingModeActivity : BaseActivity() {
.show()
}
/** 切换餐次时清空输入框,用此标志跳过 afterTextChanged 的查询触发 */
private var isClearingByTabSwitch = false
fun addViewListener() {
binding.dishRadioGroup.setOnCheckedChangeListener { _, checkedId ->
dinnerType = when (checkedId) {
@@ -100,6 +104,10 @@ class CookingModeActivity : BaseActivity() {
R.id.rbDinner -> "3"
else -> "1"
}
// 切换餐次时清空搜索框,避免用户误以为列表是被过滤的状态
isClearingByTabSwitch = true
binding.etInputDish.text?.clear()
isClearingByTabSwitch = false
loadFragment()
}
binding.etInputDish.addOnActionSearchListener {
@@ -108,6 +116,17 @@ class CookingModeActivity : BaseActivity() {
binding.ivDishSearch.setOnClickListener {
jumpSearch()
}
// 输入框清空时自动查询全部数据(切换餐次触发的清空除外,由 loadFragment 负责刷新)
binding.etInputDish.addTextChangedListener(afterTextChanged = { text ->
if (!isClearingByTabSwitch && text.isNullOrBlank()) {
currentFragment()?.getDishList(pageNo = 1, dinnerType = dinnerType)
}
})
}
/** 获取当前显示的 FoodListFragment */
private fun currentFragment(): FoodListFragment? {
return runCatching { fragmentList[dinnerType.toInt() - 1] }.getOrNull()
}
private fun jumpSearch() {
@@ -116,10 +135,7 @@ class CookingModeActivity : BaseActivity() {
toast(binding.etInputDish.hint.toString())
return
}
startActivity<FoodSearchActivity> {
putExtra(FoodSearchActivity.DINNER_TYPE, dinnerType)
putExtra(FoodSearchActivity.FOOD_NAME, searchText)
}
currentFragment()?.getDishList(pageNo = 1, dinnerType = dinnerType, foodName = searchText)
hideKeyboard()
}
@@ -199,10 +199,10 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
}
}
fun getDishList(pageNo: Int, dinnerType: String) {
fun getDishList(pageNo: Int, dinnerType: String, foodName: String? = null) {
this.pageNo = pageNo
this.dinnerType = dinnerType
getDishList()
getDishList(foodName)
}
fun getDinnerTypeText(): String {
@@ -216,13 +216,15 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
@SuppressLint("NotifyDataSetChanged")
@Suppress("unchecked_cast")
fun getDishList() {
fun getDishList(foodName: String? = null) {
val param = mutableMapOf<String, Any>(
"pageNum" to pageNo,
"pageSize" to pageSize,
"placeId" to BaseApp.canteenId,
"dinnerType" to getDinnerTypeText()
)
// foodName 不为空时按名称过滤,为 null 时查全部
foodName?.let { param["foodName"] = it }
activity.getFoodList(param = param)
if (pageNo == 1) activity.getCookFoodList()
}