raamcosta / compose-destinations

Annotation processing library for type-safe Jetpack Compose navigation with no boilerplate.
https://composedestinations.rafaelcosta.xyz
Apache License 2.0
3.15k stars 131 forks source link

can't I use viewModel and DestinationsNavigator at the same time? #117

Closed coodin closed 2 years ago

coodin commented 2 years ago

how can I use both of viewModel which use hilt and navController or DestinationsNavigator as my composable function parameters, by the way I use navArgsDelegate argument parameter of Destination annotation to clarify arguments passed by navigate function. I am asking this question because I can't provide two of them at the same time, it gave me an error for DestinationsNavigator which was , the library have been wanted DestinationsNavigator from me but as I understood it should have been given by library.

raamcosta commented 2 years ago

You most definitely can :)

Can you copy paste the error you get when using both?

coodin commented 2 years ago

yes you are 100% right after I realize the gradlew kspDebugKotlin command doesn't work for me to build new or changed function names or any code changes related to it so I had to rebuild the project and after that it worked. did I misuse command kspDebugKotlin? by the way what does it mean Debug between ksp and Kotlin for this command?

raamcosta commented 2 years ago

You can just run the app and it will generate the classes, no need for specific command. But Debug should be a combination of flavour and buildType. Sometimes it’s useful if you need to use a file that was not yet generated.

I’ll close then, let me know below if there is any other issue.

coodin commented 2 years ago

thank you very much by the way your library is amazing, I hope android team see your library and select it as default in next major release of navigation compose.

raamcosta commented 2 years ago

Never gonna happen ahah 😝 thanks !

HermanAleksey commented 1 year ago

Have the same issue. This is my SplashScreen. And with viewModel it doesn't work.

@RootNavGraph(start = true) @Destination @Composable fun SplashScreen( viewModel: SplashScreenViewModel, navigator: DestinationsNavigator )

But as soon as i remove viewModel: SplashScreenViewModel param - it works.

My ViewModel for this screen:

@HiltViewModel class SplashScreenViewModel @Inject constructor( private val userDataStore: UserDataStore, private val loginUseCase: LoginUseCase ) : ViewModel()

versions:

plugin com.google.devtools.ksp version 1.7.10-1.0.6

implementation "io.github.raamcosta.compose-destinations:core:1.6.20-beta" ksp "io.github.raamcosta.compose-destinations:ksp:1.6.20-beta"

compose version is 1.2.1

getting this error: FATAL EXCEPTION: main Process: com.example.meditationcomposeapp, PID: 22255 java.lang.RuntimeException: SplashScreenViewModel was requested, but it is not present at com.ramcosta.composedestinations.navigation.DestinationDependenciesContainer.require(DestinationDependenciesContainer.kt:98) at com.example.meditationcomposeapp.presentation.screens.destinations.SplashScreenDestination.Content(SplashScreenDestination.kt:56) at com.ramcosta.composedestinations.DefaultNavHostEngine.CallComposable(DefaultNavHostEngine.kt:165) at com.ramcosta.composedestinations.DefaultNavHostEngine.access$CallComposable(DefaultNavHostEngine.kt:29) at com.ramcosta.composedestinations.DefaultNavHostEngine$addComposable$1.invoke(DefaultNavHostEngine.kt:111) at com.ramcosta.composedestinations.DefaultNavHostEngine$addComposable$1.invoke(DefaultNavHostEngine.kt:110) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116) at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34) at androidx.navigation.compose.NavHostKt$NavHost$4$2.invoke(NavHost.kt:163) at androidx.navigation.compose.NavHostKt$NavHost$4$2.invoke(NavHost.kt:162)

raamcosta commented 1 year ago

This is not an issue, you need to pass the VM explicitly in some way. Check here: https://composedestinations.rafaelcosta.xyz/common-use-cases/providing-viewmodels

HermanAleksey commented 1 year ago

This is not an issue, you need to pass the VM explicitly in some way. Check here: https://composedestinations.rafaelcosta.xyz/common-use-cases/providing-viewmodels

Thank you very much! Now everything is in place. Link is super usefull and explain everything i needed.

raamcosta commented 1 year ago

Great! Glad it helped

waheed006 commented 8 months ago

Sir @raamcosta, I follow the above link, the viewmodel is shared but data is not coming with viewmodel. Can you please help me in this.