raamcosta / compose-destinations

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

View Model Instance #300

Closed AdzeB closed 1 year ago

AdzeB commented 1 year ago

Hello!

I have an issue. I want to pass the value, let's say, email in the VM from one screen to another, but when I get to the other screen, there's no value. Is there a way to share the same instance of the VM?

I appreciate any help you can provide.

raamcosta commented 1 year ago

Hi 👋

I'm not sure I understand your issue. Are you trying to pass email as a navigation argument from one screen to another? If yes, check this: https://composedestinations.rafaelcosta.xyz/destination-arguments/navigation-arguments

If you want to share the VM between multiple screens, check this: https://composedestinations.rafaelcosta.xyz/common-use-cases/providing-viewmodels#share-viewmodels-between-multiple-destinations

AdzeB commented 1 year ago

I tried the share view models methods, but I get errors implementing that.

I get the following Too many arguments for public inline fun <reified D : Any, T> DependenciesContainerBuilder<T>.dependency(dependency: D): Unit defined in com.ramcosta.composedestinations.navigation

With regards to the share it to all the screens using activity, I am currently implementing my destinationNavHost in the main activity under a MopdalBottomSheetLayout, so I don't know how I will have access to the facility(I am new to jetpack compose)

AdzeB commented 1 year ago

@raamcosta

raamcosta commented 1 year ago

That seems like you’re calling the method wrong 😅

Can you paste the code here where you’re calling dependency method?

AdzeB commented 1 year ago

this is how it looks now

    setContent {
        CompositionLocalProvider(UserState provides userState) {
            AppLoadTheme {
                val navController = rememberAnimatedNavController()
                val bottomSheetNavigator = rememberBottomSheetNavigator()
                navController.navigatorProvider.addNavigator(bottomSheetNavigator)
                ModalBottomSheetLayout(bottomSheetNavigator = bottomSheetNavigator ) {
                    DestinationsNavHost(
                        navGraph = NavGraphs.root,
                        navController = navController,
                        engine = rememberNavHostEngine(),
                        dependenciesContainerBuilder = {
                            dependency(hiltViewModel<OTPViewModel>())
                        }
                    )
                }

              }

            }
        }
AdzeB commented 1 year ago

I tried this as well.

dependency(NavGraphs.root){ val parentEntry = remember(navBackStackEntry){ navController.getBackStackEntry(NavGraphs.root.route) } hiltViewModel(parentEntry) }

I have a user sign-up screen, the current flow is to get the user to enter his/her email address, then goes to the OTP screen once the user verifies then to the complete signup screen where the email is read-only.|

The problem I have is by the time the user gets to the OTP screen, the user email is null, the activity has been seemly recreated

raamcosta commented 1 year ago

    DestinationsNavHost(
        navGraph = NavGraphs.root,
        // ....
        dependenciesContainerBuilder = {
            dependency(NavGraphs.root) {
                val parentEntry = remember(navBackStackEntry) {
                    navController.getBackStackEntry(NavGraphs.root.route)
                }

                hiltViewModel<OTPViewModel>(parentEntry)
            }
        }
    )

The above should work, if it doesn't, show me the error.

AdzeB commented 1 year ago

I get this

image
raamcosta commented 1 year ago

What version of the library are you using?

AdzeB commented 1 year ago
implementation 'io.github.raamcosta.compose-destinations:animations-core:1.5.1-beta'
ksp 'io.github.raamcosta.compose-destinations:ksp:1.5.1-beta'
raamcosta commented 1 year ago

I see. update it to 1.5.27-beta please. Documentation is matching latest version only 🙂

AdzeB commented 1 year ago

Okay let me try it

AdzeB commented 1 year ago

It worked no errors

AdzeB commented 1 year ago

Thank you very much, I managed to get the value to different screens.