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