refactor(viewmodel): 修复 loadSeasoning 防重入缺陷并移除 BaseActivity 网络包装方法
- AppViewModel.loadSeasoning 用 Mutex 替换 isProcessing 标志位,避免异常时状态永久锁死 - 删除 BaseActivity 中 queryGoodsList / querySeasoningList 包装方法,减少隐式依赖 - FoodSearchDialog / SeasoningSearchDialog 改为直接调用 activity.netViewModel.xxx() Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -21,8 +21,6 @@ import androidx.core.view.WindowInsetsControllerCompat
|
||||
import com.shuwei.dish.match.R
|
||||
import com.shuwei.dish.match.databinding.ActivityBaseBinding
|
||||
import com.shuwei.dish.match.dialog.Loading
|
||||
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
|
||||
import com.shuwei.dish.match.entity.SeasoningEntity
|
||||
import com.shuwei.dish.match.net.NetViewModel
|
||||
import com.shuwei.dish.match.ui.InitActivity
|
||||
import com.shuwei.dish.match.utils.ActivityManager
|
||||
@@ -183,22 +181,6 @@ open class BaseActivity : AppCompatActivity() {
|
||||
|
||||
|
||||
val netViewModel: NetViewModel by viewModels()
|
||||
fun queryGoodsList(
|
||||
param: MutableMap<String, Any>,
|
||||
onSuccess: (MutableList<CookFoodGoodsEntity>?) -> Unit,
|
||||
onFailure: (String, String) -> Unit
|
||||
) {
|
||||
netViewModel.queryGoodsList(param, onSuccess, onFailure)
|
||||
}
|
||||
|
||||
fun querySeasoningList(
|
||||
param: MutableMap<String, Any>,
|
||||
onSuccess: (MutableList<SeasoningEntity>?) -> Unit,
|
||||
onFailure: (String, String) -> Unit
|
||||
) {
|
||||
netViewModel.querySeasoningList(param, onSuccess, onFailure)
|
||||
}
|
||||
|
||||
|
||||
private var permissionCallback: ((isGranted: Boolean) -> Unit)? = null
|
||||
private var activityCallback: ((intent: Intent?) -> Unit)? = null
|
||||
|
||||
@@ -130,7 +130,7 @@ class FoodSearchDialog(
|
||||
if (!goodsName.isNullOrBlank()) {
|
||||
param["goodsName"] = goodsName!!
|
||||
}
|
||||
activity.queryGoodsList(param = param, onSuccess = {
|
||||
activity.netViewModel.queryGoodsList(param = param, onSuccess = {
|
||||
loadGoodsList(it)
|
||||
}, onFailure = { _, msg ->
|
||||
activity.toast(msg)
|
||||
|
||||
@@ -181,7 +181,7 @@ class SeasoningSearchDialog(
|
||||
if (!goodsName.isNullOrBlank()) {
|
||||
param["goodsName"] = goodsName!!
|
||||
}
|
||||
activity.querySeasoningList(param = param, onSuccess = {
|
||||
activity.netViewModel.querySeasoningList(param = param, onSuccess = {
|
||||
loadGoodsList(it)
|
||||
}, onFailure = { _, msg ->
|
||||
activity.toast(msg)
|
||||
|
||||
@@ -7,6 +7,8 @@ import com.shuwei.dish.match.entity.CookFoodEntity
|
||||
import com.shuwei.dish.match.entity.CookFoodGoodsEntity
|
||||
import com.shuwei.dish.match.entity.SeasoningEntity
|
||||
import kotlinx.coroutines.launch
|
||||
import kotlinx.coroutines.sync.Mutex
|
||||
import kotlinx.coroutines.sync.withLock
|
||||
import kotlin.collections.forEach
|
||||
|
||||
class AppViewModel(private val rep: AppRepository) : ViewModel() {
|
||||
@@ -149,14 +151,13 @@ class AppViewModel(private val rep: AppRepository) : ViewModel() {
|
||||
// }
|
||||
// }
|
||||
|
||||
private var isProcessing = false
|
||||
private val loadSeasoningMutex = Mutex()
|
||||
fun loadSeasoning(action: (MutableList<SeasoningEntity>) -> Unit) {
|
||||
if (loadSeasoningMutex.isLocked) return
|
||||
viewModelScope.launch {
|
||||
if (isProcessing) return@launch
|
||||
isProcessing = true
|
||||
val list = rep.getAllStream()
|
||||
action(list)
|
||||
isProcessing = false
|
||||
loadSeasoningMutex.withLock {
|
||||
action(rep.getAllStream())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user