From 0da9a3c9b4658af60a491b54931764fff4b512cc Mon Sep 17 00:00:00 2001 From: Hogu59 Date: Fri, 26 Jul 2024 10:10:06 +0900 Subject: [PATCH] :ambulance: fix navigation implementation --- .../presentation/detail/DetailRecipeFragment.kt | 7 ++++--- .../presentation/detail/DetailRecipeViewModel.kt | 11 +++++++---- .../src/main/res/layout/fragment_detail_recipe.xml | 2 +- android/app/src/main/res/navigation/nav_graph.xml | 6 +++++- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeFragment.kt b/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeFragment.kt index 0f7660c0..d7fb67ed 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeFragment.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeFragment.kt @@ -14,7 +14,7 @@ class DetailRecipeFragment : Fragment() { private val args: DetailRecipeFragmentArgs by navArgs() private val binding by lazy { FragmentDetailRecipeBinding.inflate(layoutInflater) } private val viewModel by lazy { DetailRecipeViewModel(recipe) } - private lateinit var recipe: Recipe + private val recipe: Recipe by lazy { args.recipe } override fun onCreateView( inflater: LayoutInflater, @@ -32,8 +32,9 @@ class DetailRecipeFragment : Fragment() { } private fun observeNavigationEvent() { - viewModel.navigateToStepEvent.observe(viewLifecycleOwner) { shouldNavigate -> - if (shouldNavigate) { + viewModel.navigateToStepEvent.observe(viewLifecycleOwner) { navigationEvent -> + val navigationAvailable = navigationEvent.getContentIfNotHandled() ?: return@observe + if (navigationAvailable) { navigateToStep() } } diff --git a/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeViewModel.kt b/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeViewModel.kt index b274de5b..6017b61a 100644 --- a/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeViewModel.kt +++ b/android/app/src/main/java/net/pengcook/android/presentation/detail/DetailRecipeViewModel.kt @@ -5,12 +5,15 @@ import androidx.lifecycle.LiveData import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel import net.pengcook.android.presentation.core.model.Recipe +import net.pengcook.android.presentation.core.util.Event -class DetailRecipeViewModel(private val recipe: Recipe) : ViewModel() { - private val _navigateToStepEvent = MutableLiveData() - val navigateToStepEvent: LiveData get() = _navigateToStepEvent +class DetailRecipeViewModel( + private val recipe: Recipe, +) : ViewModel() { + private val _navigateToStepEvent = MutableLiveData>() + val navigateToStepEvent: LiveData> get() = _navigateToStepEvent fun onNavigateToMakingStep() { - _navigateToStepEvent.value = true + _navigateToStepEvent.value = Event(true) } } diff --git a/android/app/src/main/res/layout/fragment_detail_recipe.xml b/android/app/src/main/res/layout/fragment_detail_recipe.xml index 3513f7e4..054d6680 100644 --- a/android/app/src/main/res/layout/fragment_detail_recipe.xml +++ b/android/app/src/main/res/layout/fragment_detail_recipe.xml @@ -268,7 +268,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="6dp" - android:text="@{Recipe.difficulty}" + android:text="@{String.valueOf(Recipe.difficulty)}" android:textColor="@color/black" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/tv_difficulty_title" diff --git a/android/app/src/main/res/navigation/nav_graph.xml b/android/app/src/main/res/navigation/nav_graph.xml index c9ae3caa..3a8ebd0d 100644 --- a/android/app/src/main/res/navigation/nav_graph.xml +++ b/android/app/src/main/res/navigation/nav_graph.xml @@ -106,7 +106,11 @@ + android:label="RecipeStepFragment" > + +