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() } diff --git a/app/src/test/java/nl/ovfietsbeschikbaarheid/mapper/CheckModulesTest.kt b/app/src/test/java/nl/ovfietsbeschikbaarheid/mapper/CheckModulesTest.kt index 1952856..39ea655 100644 --- a/app/src/test/java/nl/ovfietsbeschikbaarheid/mapper/CheckModulesTest.kt +++ b/app/src/test/java/nl/ovfietsbeschikbaarheid/mapper/CheckModulesTest.kt @@ -1,6 +1,7 @@ package nl.ovfietsbeschikbaarheid.mapper import android.content.Context +import androidx.lifecycle.SavedStateHandle import nl.ovfietsbeschikbaarheid.di.appModule import org.junit.Test import org.koin.test.KoinTest @@ -11,6 +12,8 @@ class CheckModulesTest : KoinTest { @Test fun `check all modules`() { // Context is provided separately in MyApplication - appModule().verify(extraTypes = listOf(Context::class)) + // SavedStateHandle is supported by Koin out of the box since version 3.3.0: https://insert-koin.io/docs/reference/koin-android/viewmodel/#savedstatehandle-injection-330 + // I don't know why the verify function doesn't know this. + appModule().verify(extraTypes = listOf(Context::class, SavedStateHandle::class)) } } \ No newline at end of file