refactor(viewmodel): 将回调方式统一改为 StateFlow + UiState,消除竞态问题
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -142,19 +142,18 @@ class HomeActivity : BaseActivity() {
|
||||
finish()
|
||||
return@launch
|
||||
}
|
||||
appViewModel.countCookFood(cookMode = 1) { count ->
|
||||
if (count > 0) {
|
||||
// 有烹饪中数据,打开采样历史列表页面
|
||||
startActivity<SamplingListActivity>()
|
||||
finish()
|
||||
return@countCookFood
|
||||
}
|
||||
// 无烹饪数据,打开新增采样页面
|
||||
startActivity<PrepareCookActivity> {
|
||||
putExtra(PrepareCookActivity.PAGE_FROM, PrepareCookActivity.HOME)
|
||||
}
|
||||
val count = appViewModel.countCookFood(cookMode = 1)
|
||||
if (count > 0) {
|
||||
// 有烹饪中数据,打开采样历史列表页面
|
||||
startActivity<SamplingListActivity>()
|
||||
finish()
|
||||
return@launch
|
||||
}
|
||||
// 无烹饪数据,打开新增采样页面
|
||||
startActivity<PrepareCookActivity> {
|
||||
putExtra(PrepareCookActivity.PAGE_FROM, PrepareCookActivity.HOME)
|
||||
}
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -95,16 +95,15 @@ class InitActivity : BaseActivity() {
|
||||
startActivity<SamplingListActivity>()
|
||||
return@launch
|
||||
}
|
||||
appViewModel.countCookFood(cookMode = 1) { count ->
|
||||
if (count > 0) {
|
||||
// 有烹饪中数据,打开采样历史列表页面
|
||||
startActivity<SamplingListActivity>()
|
||||
return@countCookFood
|
||||
}
|
||||
// 无烹饪数据,打开新增采样页面
|
||||
startActivity<PrepareCookActivity> {
|
||||
putExtra(PrepareCookActivity.PAGE_FROM, PrepareCookActivity.HOME)
|
||||
}
|
||||
val count = appViewModel.countCookFood(cookMode = 1)
|
||||
if (count > 0) {
|
||||
// 有烹饪中数据,打开采样历史列表页面
|
||||
startActivity<SamplingListActivity>()
|
||||
return@launch
|
||||
}
|
||||
// 无烹饪数据,打开新增采样页面
|
||||
startActivity<PrepareCookActivity> {
|
||||
putExtra(PrepareCookActivity.PAGE_FROM, PrepareCookActivity.HOME)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ import android.util.Log
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import androidx.activity.addCallback
|
||||
import androidx.lifecycle.Lifecycle
|
||||
import androidx.lifecycle.lifecycleScope
|
||||
import androidx.lifecycle.repeatOnLifecycle
|
||||
import androidx.recyclerview.widget.ItemTouchHelper
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import com.shuwei.dish.match.R
|
||||
@@ -20,6 +22,7 @@ import com.shuwei.dish.match.dialog.CommonDialog
|
||||
import com.shuwei.dish.match.entity.CookFoodEntity
|
||||
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
|
||||
import com.shuwei.dish.match.entity.FoodRecord
|
||||
import com.shuwei.dish.match.net.UiState
|
||||
import com.shuwei.dish.match.objbox.FoodModule
|
||||
import com.shuwei.dish.match.utils.AddressUtil
|
||||
import com.shuwei.dish.match.utils.CameraUtils
|
||||
@@ -36,6 +39,7 @@ import com.shuwei.dish.match.utils.ext.toJsonString
|
||||
import com.shuwei.dish.match.utils.ext.toast
|
||||
import com.shuwei.dish.match.utils.ext.visible
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.flow.collect
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.withContext
|
||||
import java.io.Serializable
|
||||
@@ -110,6 +114,7 @@ class PrepareCookActivity : BaseActivity() {
|
||||
addBackKeyListener()
|
||||
initCamera(binding.flCameraContainer)
|
||||
initRecyclerView()
|
||||
initObserver()
|
||||
|
||||
if (!isSamplingMode) {
|
||||
getDishDetail()
|
||||
@@ -267,6 +272,28 @@ class PrepareCookActivity : BaseActivity() {
|
||||
}
|
||||
|
||||
|
||||
private fun initObserver() {
|
||||
lifecycleScope.launch {
|
||||
repeatOnLifecycle(Lifecycle.State.STARTED) {
|
||||
netViewModel.foodDetailState.collect { state ->
|
||||
when (state) {
|
||||
is UiState.Success -> {
|
||||
val detail = state.data
|
||||
if (detail == null) {
|
||||
toast("查询菜品信息为空")
|
||||
return@collect
|
||||
}
|
||||
loadDishDetail(detail)
|
||||
}
|
||||
|
||||
is UiState.Error -> toast(state.msg)
|
||||
else -> {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun getDishDetail() {
|
||||
// //649
|
||||
// val url = "${UrlConfig.DISH_DETAIL}?foodId=${food?.foodId}"
|
||||
@@ -285,17 +312,7 @@ class PrepareCookActivity : BaseActivity() {
|
||||
// toast(msg)
|
||||
// }
|
||||
|
||||
netViewModel.getFoodDetail(
|
||||
food?.foodId ?: "",
|
||||
onSuccess = { detail ->
|
||||
if (detail == null) {
|
||||
toast("查询菜品信息为空")
|
||||
return@getFoodDetail
|
||||
}
|
||||
loadDishDetail(detail)
|
||||
}, onFailure = { code, msg ->
|
||||
toast(msg)
|
||||
})
|
||||
netViewModel.getFoodDetail(food?.foodId ?: "")
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user