增删改防止重复点击

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

@@ -162,12 +162,16 @@ class CategoryViewModel(private val categoryRepository: CategoryRepository) :
private fun addCategory(category: Category) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
categoryRepository.insertCategory(category)
sendEvent(CategoryEffect.ShowMessage(getString(Res.string.category_add_success)))
sendEvent(CategoryEffect.NavigateBack)
} catch (e: Exception) {
val errStr = getString(Res.string.category_add_failed)
updateState { copy(isLoading = false, error = e.message ?: errStr) }
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false, isProcessing = false) }
}
}
}
@@ -175,13 +179,17 @@ class CategoryViewModel(private val categoryRepository: CategoryRepository) :
private fun updateCategory(category: Category) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
categoryRepository.updateCategory(category)
sendEvent(CategoryEffect.ShowMessage(getString(Res.string.category_update_success)))
sendEvent(CategoryEffect.NavigateBack)
hideEditDialog()
} catch (e: Exception) {
val errStr = getString(Res.string.category_update_failed)
updateState { copy(isLoading = false, error = e.message ?: errStr) }
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false, isProcessing = false) }
}
}
}
@@ -189,12 +197,16 @@ class CategoryViewModel(private val categoryRepository: CategoryRepository) :
private fun deleteCategory(categoryId: String) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
categoryRepository.deleteCategory(categoryId)
sendEvent(CategoryEffect.ShowMessage(getString(Res.string.category_delete_success)))
hideDeleteDialog()
} catch (e: Exception) {
val errStr = getString(Res.string.category_delete_failed)
updateState { copy(isLoading = false, error = e.message ?: errStr) }
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false, isProcessing = false) }
}
}
}

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) }
}
}
}

View File

@@ -134,12 +134,16 @@ class TaskViewModel(
private fun addTask(task: Task) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
taskRepository.insertTask(task)
sendEvent(TaskEffect.ShowMessage(getString(Res.string.task_add_success)))
sendEvent(TaskEffect.NavigateBack)
} catch (e: Exception) {
val errStr = getString(Res.string.task_add_failed)
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false,isProcessing = false) }
}
}
}
@@ -151,12 +155,16 @@ class TaskViewModel(
private fun updateTask(task: Task) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
taskRepository.updateTask(task)
sendEvent(TaskEffect.ShowMessage(getString(Res.string.task_update_success)))
sendEvent(TaskEffect.NavigateBack)
} catch (e: Exception) {
val errStr = getString(Res.string.task_update_failed)
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false,isProcessing = false) }
}
}
}
@@ -168,11 +176,15 @@ class TaskViewModel(
private fun deleteTask(taskId: String) {
viewModelScope.launch {
try {
if (state.value.isProcessing) return@launch
updateState { copy(isLoading = false, isProcessing = true) }
taskRepository.deleteTask(taskId)
sendEvent(TaskEffect.ShowMessage(getString(Res.string.task_delete_success)))
} catch (e: Exception) {
val errStr = getString(Res.string.task_delete_failed)
updateState { copy(error = e.message ?: errStr) }
} finally {
updateState { copy(isLoading = false,isProcessing = false) }
}
}
}