refactor(activity): 统一各入口传 FoodRecord 跳转 PrepareFoodActivity,重构菜品详情加载逻辑
This commit is contained in:
@@ -51,6 +51,10 @@ class NetViewModel(
|
||||
}
|
||||
}
|
||||
|
||||
fun resetFoodDetailState() {
|
||||
_foodDetailState.value = UiState.Idle
|
||||
}
|
||||
|
||||
/**
|
||||
* 搜索菜品列表的 UI 状态流,UI 层通过 collect 监听
|
||||
*/
|
||||
|
||||
@@ -117,7 +117,6 @@ class CookingModeActivity : BaseActivity() {
|
||||
return
|
||||
}
|
||||
startActivity<FoodSearchActivity> {
|
||||
putExtra(FoodSearchActivity.PAGE_TYPE, 1)
|
||||
putExtra(FoodSearchActivity.DINNER_TYPE, dinnerType)
|
||||
putExtra(FoodSearchActivity.FOOD_NAME, searchText)
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ import com.shuwei.dish.match.databinding.LayoutEmptyViewBinding
|
||||
import com.shuwei.dish.match.entity.FoodRecord
|
||||
import com.shuwei.dish.match.net.UiState
|
||||
import com.shuwei.dish.match.utils.KeyboardUtil
|
||||
import com.shuwei.dish.match.utils.SpTool
|
||||
import com.shuwei.dish.match.utils.ext.addOnActionSearchListener
|
||||
import com.shuwei.dish.match.utils.ext.gone
|
||||
import com.shuwei.dish.match.utils.ext.startActivity
|
||||
@@ -32,7 +33,7 @@ class FoodSearchActivity : BaseActivity() {
|
||||
|
||||
companion object {
|
||||
private const val TAG = "FoodSearchActivity"
|
||||
const val PAGE_TYPE = "pageType"
|
||||
const val FOOD_ITEM = "foodItem"
|
||||
const val FOOD_ID = "foodId"
|
||||
const val FOOD_NAME = "foodName"
|
||||
const val DINNER_TYPE = "dinnerType"
|
||||
@@ -41,13 +42,12 @@ class FoodSearchActivity : BaseActivity() {
|
||||
private lateinit var binding: ActivityFoodSearchBinding
|
||||
|
||||
private var list: MutableList<FoodRecord> = mutableListOf()
|
||||
private var pageType = 0
|
||||
private var dinnerType = "1"
|
||||
private val recordAdapter by lazy {
|
||||
Food2Adapter(list).apply {
|
||||
isStateViewEnable = true
|
||||
setOnItemClickListener { adapter, view, position ->
|
||||
if (pageType == 1) {
|
||||
if (SpTool.cookMode == 0) {
|
||||
val jumpItem = list[position].also { it.dinnerType = dinnerType }
|
||||
startActivity<PrepareFoodActivity> {
|
||||
putExtra(PrepareFoodActivity.FOOD_ITEM, jumpItem as Serializable)
|
||||
@@ -55,8 +55,7 @@ class FoodSearchActivity : BaseActivity() {
|
||||
return@setOnItemClickListener
|
||||
}
|
||||
setResult(RESULT_OK, Intent().apply {
|
||||
putExtra(FOOD_ID, list[position].foodId)
|
||||
putExtra(FOOD_NAME, list[position].foodName)
|
||||
putExtra(FOOD_ITEM, list[position] as Serializable)
|
||||
})
|
||||
finish()
|
||||
}
|
||||
@@ -69,7 +68,6 @@ class FoodSearchActivity : BaseActivity() {
|
||||
super.onCreate(savedInstanceState)
|
||||
binding = ActivityFoodSearchBinding.inflate(layoutInflater)
|
||||
setContentView(binding.root)
|
||||
pageType = intent.getIntExtra(PAGE_TYPE, 0)
|
||||
dinnerType = intent.getStringExtra(DINNER_TYPE) ?: "0"
|
||||
setHeaderBackground()
|
||||
foodName = intent.getStringExtra(FOOD_NAME)
|
||||
|
||||
@@ -10,6 +10,7 @@ import android.view.MotionEvent
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.addCallback
|
||||
import androidx.core.content.IntentCompat
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
@@ -57,7 +58,6 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
companion object {
|
||||
const val TAG = "CookActivity"
|
||||
const val FOOD_ITEM = "foodItem"
|
||||
const val FOOD_NAME = "foodName"
|
||||
const val PAGE_FROM = "pageFrom"
|
||||
const val HOME = "home"
|
||||
const val WEIGHT_CHANGE_VALUE = 15
|
||||
@@ -73,9 +73,6 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
|
||||
private var food: FoodRecord? = null
|
||||
|
||||
/** 采集模式下通过搜索页回填的 foodId */
|
||||
private var foodId: String? = null
|
||||
|
||||
/** 来源页面标识,HOME 时返回跳转 SamplingListActivity */
|
||||
private var pageFrom: String? = null
|
||||
|
||||
@@ -85,8 +82,8 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
setContentView(binding.root)
|
||||
setHeaderBackground()
|
||||
pageFrom = intent.getStringExtra(PAGE_FROM)
|
||||
food = intent.extras?.getSerializable(FOOD_ITEM) as FoodRecord?
|
||||
|
||||
//food = intent.extras?.getSerializable(FOOD_ITEM) as FoodRecord?
|
||||
food = IntentCompat.getSerializableExtra(intent, FOOD_ITEM, FoodRecord::class.java)
|
||||
val isSamplingMode = SpTool.cookMode == 1
|
||||
val titleText = if (isSamplingMode) "菜品采集" else "菜品制作"
|
||||
setTitleBar(titleBarAction = {
|
||||
@@ -100,16 +97,21 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
onBackPressedDispatcher.onBackPressed()
|
||||
}
|
||||
})
|
||||
|
||||
food?.let {
|
||||
binding.etInputDish.run {
|
||||
setText(it.foodName)
|
||||
tag = it.foodName
|
||||
}
|
||||
}
|
||||
// 根据 cookMode 设置搜索栏可编辑性
|
||||
if (isSamplingMode) {
|
||||
// 采集模式:可编辑,支持搜索
|
||||
val foodName = intent.getStringExtra(FOOD_NAME)
|
||||
binding.etInputDish.setText(foodName ?: food?.foodName ?: "")
|
||||
binding.ivDishSearch.visible()
|
||||
binding.etInputDish.let {
|
||||
it.setSelection(it.length())
|
||||
}
|
||||
} else {
|
||||
// 制作模式:不可编辑,仅展示菜名
|
||||
binding.etInputDish.setText(food?.foodName ?: "")
|
||||
binding.etInputDish.isFocusable = false
|
||||
binding.etInputDish.isFocusableInTouchMode = false
|
||||
binding.ivDishSearch.gone()
|
||||
@@ -121,8 +123,8 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
initRecyclerView()
|
||||
initObserver()
|
||||
|
||||
if (!isSamplingMode) {
|
||||
getDishDetail()
|
||||
if (food?.foodId.isNullOrBlank().not()) {
|
||||
getDishDetail(food!!.foodId!!)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,10 +169,11 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
toast("菜品构成信息未设置")
|
||||
return@clickWithDebounce
|
||||
}
|
||||
val foodRecord = FoodRecord()
|
||||
val foodRecord = food ?: FoodRecord()
|
||||
binding.etInputDish.let {
|
||||
if (it.tag != null && it.tag.toString() == it.text.toString()) {
|
||||
foodRecord.foodId = foodId
|
||||
if (it.tag == null || it.tag.toString() != it.text.toString()) {
|
||||
// 输入框内容与回填名称不一致,说明用户手动修改了菜名,清除 foodId
|
||||
foodRecord.foodId = null
|
||||
}
|
||||
}
|
||||
foodRecord.foodName = showFoodName
|
||||
@@ -329,31 +332,25 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
|
||||
private fun initObserver() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
netViewModel.foodDetailState.collect { state ->
|
||||
when (state) {
|
||||
is UiState.Success -> {
|
||||
// 防止 StateFlow 重放导致 list 被重复清空
|
||||
if (isDetailLoaded) return@collect
|
||||
val detail = state.data
|
||||
if (detail == null) {
|
||||
toast("查询菜品信息为空")
|
||||
return@collect
|
||||
}
|
||||
isDetailLoaded = true
|
||||
loadDishDetail(detail)
|
||||
netViewModel.foodDetailState.collect { state ->
|
||||
when (state) {
|
||||
is UiState.Success -> {
|
||||
val detail = state.data
|
||||
if (detail == null) {
|
||||
toast("查询菜品信息为空")
|
||||
return@collect
|
||||
}
|
||||
|
||||
is UiState.Error -> toast(state.msg)
|
||||
else -> {}
|
||||
loadDishDetail(detail)
|
||||
}
|
||||
is UiState.Error -> toast(state.msg)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDishDetail() {
|
||||
netViewModel.getFoodDetail(food?.foodId ?: "")
|
||||
private fun getDishDetail(foodId: String) {
|
||||
netViewModel.getFoodDetail(foodId)
|
||||
}
|
||||
|
||||
|
||||
@@ -366,14 +363,16 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
// 筛选出主材和辅材,转换为 GoodsItem(useWeight 置零)
|
||||
val tempData = voList.filter { it.materialType == 1 || it.materialType == 2 }
|
||||
.map { it.toGoodsItem().also { item -> item.useWeight = 0.0 } }
|
||||
// 直接替换 adapter.items,确保 adapter 数据源被完全替换
|
||||
// 同时同步外部 list,保证后续 updateMaterialTypes/removeFood 等操作正确
|
||||
list.clear()
|
||||
firstReqSize = list.size
|
||||
list.addAll(tempData)
|
||||
materialAdapter.items = list
|
||||
materialAdapter.notifyDataSetChanged()
|
||||
firstReqSize = list.size
|
||||
}
|
||||
|
||||
private var firstReqSize = 0
|
||||
private var isDetailLoaded = false
|
||||
private val list = mutableListOf<GoodsItem>()
|
||||
private val materialAdapter by lazy {
|
||||
FoodMaterialAdapter(list).apply {
|
||||
@@ -627,19 +626,25 @@ class PrepareFoodActivity : BaseActivity() {
|
||||
toast(binding.etInputDish.hint.toString())
|
||||
return
|
||||
}
|
||||
val intent = Intent(this, FoodSearchActivity::class.java).apply {
|
||||
val launchIntent = Intent(this, FoodSearchActivity::class.java).apply {
|
||||
putExtra(FoodSearchActivity.FOOD_NAME, searchContent)
|
||||
}
|
||||
startActivity(intent) {
|
||||
foodId = it?.getStringExtra(FoodSearchActivity.FOOD_ID)
|
||||
val name = it?.getStringExtra(FoodSearchActivity.FOOD_NAME)
|
||||
Log.d(TAG, "jumpSearch: foodId=$foodId, name=$name")
|
||||
if (name.isNullOrBlank()) return@startActivity
|
||||
startActivity(launchIntent) { resultIntent ->
|
||||
if (resultIntent == null) return@startActivity
|
||||
food = IntentCompat.getSerializableExtra(resultIntent, FOOD_ITEM, FoodRecord::class.java)
|
||||
Log.d(TAG, "jumpSearch: record=$food")
|
||||
if (food == null) return@startActivity
|
||||
// 将搜索结果写入 food,保留 cookMode
|
||||
food!!.cookMode = 1
|
||||
binding.etInputDish.run {
|
||||
setText(name)
|
||||
tag = name
|
||||
setText(food!!.foodName)
|
||||
tag = food!!.foodName
|
||||
setSelection(text.length)
|
||||
}
|
||||
// 有 foodId 时请求菜品构成
|
||||
if (!food!!.foodId.isNullOrBlank()) {
|
||||
getDishDetail(food!!.foodId!!)
|
||||
}
|
||||
}
|
||||
hideKeyboard()
|
||||
}
|
||||
|
||||
@@ -186,7 +186,7 @@ class SamplingModeActivity : BaseActivity() {
|
||||
return
|
||||
}
|
||||
startActivity<PrepareFoodActivity> {
|
||||
putExtra(PrepareFoodActivity.FOOD_NAME, item.foodName)
|
||||
putExtra(PrepareFoodActivity.FOOD_ITEM, item as Serializable)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ class FoodListFragment : BaseFragment<FragmentFoodListBinding>() {
|
||||
}
|
||||
item.dinnerType = dinnerType
|
||||
activity.startActivity<PrepareFoodActivity> {
|
||||
putExtra(SubmitFoodActivity.FOOD_ITEM, item as Serializable)
|
||||
putExtra(PrepareFoodActivity.FOOD_ITEM, item as Serializable)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user