From 6a80136ec68e07f70c12a611ab97c962fbb5e903 Mon Sep 17 00:00:00 2001 From: Cristan Meijer Date: Sun, 27 Oct 2024 13:20:09 +0100 Subject: [PATCH] Use SavedStateHandle in the ViewModel to get the nav args --- .../ui/navigation/Navigation.kt | 4 +--- .../ovfietsbeschikbaarheid/ui/screen/DetailScreen.kt | 3 +-- .../viewmodel/DetailsViewModel.kt | 12 ++++++------ 3 files changed, 8 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/navigation/Navigation.kt b/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/navigation/Navigation.kt index ed31803..02213b0 100644 --- a/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/navigation/Navigation.kt +++ b/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/navigation/Navigation.kt @@ -52,10 +52,8 @@ fun Navigation() { else slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.End) } - ) { backStackEntry -> - val locationCode = backStackEntry.arguments?.getString("locationCode")!! + ) { DetailScreen( - locationCode = locationCode, onAlternativeClicked = { alternative -> navController.navigate("detail/${alternative.locationCode}") }, diff --git a/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/screen/DetailScreen.kt b/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/screen/DetailScreen.kt index 7efe305..4af526a 100644 --- a/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/screen/DetailScreen.kt +++ b/app/src/main/java/nl/ovfietsbeschikbaarheid/ui/screen/DetailScreen.kt @@ -83,13 +83,12 @@ import java.util.Locale @Composable fun DetailScreen( - locationCode: String, viewModel: DetailsViewModel = koinViewModel(), onAlternativeClicked: (LocationOverviewModel) -> Unit, onBackClicked: () -> Unit ) { LaunchedEffect(Unit) { - viewModel.setLocationCode(locationCode) + viewModel.screenLaunched() } val context = LocalContext.current diff --git a/app/src/main/java/nl/ovfietsbeschikbaarheid/viewmodel/DetailsViewModel.kt b/app/src/main/java/nl/ovfietsbeschikbaarheid/viewmodel/DetailsViewModel.kt index 0d69561..36f268d 100644 --- a/app/src/main/java/nl/ovfietsbeschikbaarheid/viewmodel/DetailsViewModel.kt +++ b/app/src/main/java/nl/ovfietsbeschikbaarheid/viewmodel/DetailsViewModel.kt @@ -2,6 +2,7 @@ package nl.ovfietsbeschikbaarheid.viewmodel import androidx.compose.runtime.State import androidx.compose.runtime.mutableStateOf +import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.delay @@ -22,6 +23,7 @@ import java.time.ZoneId private const val MIN_REFRESH_TIME = 350L class DetailsViewModel( + savedStateHandle: SavedStateHandle, private val client: KtorApiClient, private val overviewRepository: OverviewRepository, private val stationRepository: StationRepository @@ -30,14 +32,12 @@ class DetailsViewModel( private val _screenState = mutableStateOf>(ScreenState.Loading) val screenState: State> = _screenState - private val _title = mutableStateOf("") - val title: State = _title + private val overviewModel: LocationOverviewModel = overviewRepository.getAllLocations().find { it.locationCode == savedStateHandle["locationCode"] }!! - private lateinit var overviewModel: LocationOverviewModel + private val _title = mutableStateOf(overviewModel.title) + val title: State = _title - fun setLocationCode(locationCode: String) { - overviewModel = overviewRepository.getAllLocations().find { it.locationCode == locationCode }!! - _title.value = overviewModel.title + fun screenLaunched() { viewModelScope.launch { doRefresh() }