Skip to content

Commit

Permalink
🚑 fix navigation implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Hogu59 committed Jul 26, 2024
1 parent 92578d5 commit 0da9a3c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Boolean>()
val navigateToStepEvent: LiveData<Boolean> get() = _navigateToStepEvent
class DetailRecipeViewModel(
private val recipe: Recipe,
) : ViewModel() {
private val _navigateToStepEvent = MutableLiveData<Event<Boolean>>()
val navigateToStepEvent: LiveData<Event<Boolean>> get() = _navigateToStepEvent

fun onNavigateToMakingStep() {
_navigateToStepEvent.value = true
_navigateToStepEvent.value = Event(true)
}
}
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/fragment_detail_recipe.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 5 additions & 1 deletion android/app/src/main/res/navigation/nav_graph.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@
<fragment
android:id="@+id/recipeStepFragment"
android:name="net.pengcook.android.presentation.step.RecipeStepFragment"
android:label="RecipeStepFragment" />
android:label="RecipeStepFragment" >
<action
android:id="@+id/action_recipeStepFragment_to_detailRecipeFragment"
app:destination="@id/detailRecipeFragment" />
</fragment>
<fragment
android:id="@+id/makingStepFragment"
android:name="net.pengcook.android.presentation.making.step.StepMakingFragment"
Expand Down

0 comments on commit 0da9a3c

Please sign in to comment.