From 4de75e36504ee4596a49c598fa89281d3b799203 Mon Sep 17 00:00:00 2001 From: Gimun Kim <101035437+kmkim2689@users.noreply.github.com> Date: Tue, 22 Oct 2024 22:31:19 +0900 Subject: [PATCH] :bug: fix order changes in step images --- .../making/RecipeMakingViewModel2.kt | 52 +++++++++++-------- 1 file changed, 30 insertions(+), 22 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingViewModel2.kt b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingViewModel2.kt index 05e9c49c..6794d9ce 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingViewModel2.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/making/RecipeMakingViewModel2.kt @@ -261,28 +261,30 @@ class RecipeMakingViewModel2 } override fun onConfirm() { - if (!validateDescriptionForm()) { - _uiEvent.value = Event(RecipeMakingEvent2.DescriptionFormNotCompleted) - _isMakingStepButtonClicked.value = true - return - } - - currentStepImages.value?.forEach { - if (!it.uploaded || it.cookingTime.isEmpty() || it.description.isEmpty()) { + viewModelScope.launch { + if (!validateDescriptionForm()) { _uiEvent.value = Event(RecipeMakingEvent2.DescriptionFormNotCompleted) _isMakingStepButtonClicked.value = true - return + return@launch } - } - if (currentStepImages.value.isNullOrEmpty()) { - _uiEvent.value = Event(RecipeMakingEvent2.DescriptionFormNotCompleted) - return - } + val currentStepImages = stepMakingRepository.fetchRecipeSteps().getOrNull() - _isLoading.value = true + currentStepImages?.forEach { + if (!it.imageUploaded || it.cookingTime.isEmpty() || it.description.isEmpty()) { + _uiEvent.value = Event(RecipeMakingEvent2.DescriptionFormNotCompleted) + _isMakingStepButtonClicked.value = true + return@launch + } + } + + if (currentStepImages.isNullOrEmpty()) { + _uiEvent.value = Event(RecipeMakingEvent2.DescriptionFormNotCompleted) + return@launch + } + + _isLoading.value = true - viewModelScope.launch { val recipeCreation = recipeCreation() if (recipeCreation == null) { _isLoading.value = false @@ -429,12 +431,18 @@ class RecipeMakingViewModel2 } override fun onStepImageClick(item: RecipeStepImage) { - _uiEvent.value = - Event( - RecipeMakingEvent2.NavigateToMakingStep( - sequence = currentStepImages.value?.indexOf(item) ?: return, - ), - ) + viewModelScope.launch(coroutineExceptionHandler) { + val recipeId = recipeId + if (recipeId != null) { + saveRecipeSteps(recipeId) + } + _uiEvent.value = + Event( + RecipeMakingEvent2.NavigateToMakingStep( + sequence = currentStepImages.value?.indexOf(item) ?: return@launch, + ), + ) + } } override fun navigationAction() {