增删改防止重复点击

This commit is contained in:
2025-10-16 21:45:02 +08:00
parent 05ff710872
commit 7b60fb965f
3 changed files with 42 additions and 6 deletions

View File

@@ -94,12 +94,16 @@ class CountdownViewModel(
private fun addCountdown(countdown: Countdown) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
countdownRepository.insertCountdown(countdown)
sendEvent(CountdownEffect.ShowMessage(getString(Res.string.countdown_add_success)))
sendEvent(CountdownEffect.NavigateBack)
} catch (e: Exception) {
val errStr = getString(Res.string.countdown_add_failed)
updateState { copy(isLoading = false, error = e.message ?: errStr) }
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false, isProcessing = false) }
}
}
}
@@ -107,12 +111,16 @@ class CountdownViewModel(
private fun updateCountdown(countdown: Countdown) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
countdownRepository.updateCountdown(countdown)
sendEvent(CountdownEffect.ShowMessage(getString(Res.string.countdown_update_success)))
sendEvent(CountdownEffect.NavigateBack)
} catch (e: Exception) {
val errStr = getString(Res.string.countdown_update_failed)
updateState { copy(isLoading = false, error = e.message ?: errStr) }
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false, isProcessing = false) }
}
}
}
@@ -120,11 +128,15 @@ class CountdownViewModel(
private fun deleteCountdown(countdownId: String) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
countdownRepository.deleteCountdown(countdownId)
sendEvent(CountdownEffect.ShowMessage(getString(Res.string.countdown_delete_success)))
} catch (e: Exception) {
val errStr = getString(Res.string.countdown_delete_failed)
updateState { copy(isLoading = false, error = e.message ?: errStr) }
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false, isProcessing = false) }
}
}
}