-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to access destination arguments in a ViewModel via the SavedStateHandle? #95
Comments
For the bonus question. We do this https://github.com/HedvigInsurance/android/blob/082e966c5023ee9efaede2f5b61cc4df7b860d32/app/navigation/navigation-compose-typed/src/main/kotlin/com/hedvig/android/navigation/compose/typed/DestinationScopedViewModel.kt#L16-L25
then I would guess that hiltViewModel also accepts it as a parameter to get the right scope |
@kroegerama since library is only extension of Jetpack navigation for compose, you can access arguments from If you for example look at quick start. It would probably be nice to have additional extension, that parses arguments, based on destination arguments, if possible. |
Oh yes, I am currently using the "raw" method to access the attributes: val args = MyNavArgs(
handle["myId"]!!,
handle.get<String>("myBoolean")!!.toBoolean()
) Especially, the |
+1 on this. I'm considering adopting this library, and giving SavedStateHandles the same type-safety afforded to Bundles in decoding would be super helpful. Either that, or opening up a simple API for the underlying Appreciate your work on this library! :D |
I'm sorry for the late answer, I somehow missed this issue. We explicitly pass the wanted navigation arguments to the viewModel through the parameters. In koin using parametersOf(), in Hilt using the assisted parameters. Usually, we pass the whole destination object. import org.koin.androidx.compose.getViewModel
val viewModel: ProfileViewModel = getViewModel { parametersOf(destination) } |
Why not read it from the state? My POV:
|
Closing as answered. If anything, don't hesitate to comment. |
In my opinion, this library definitely needs a way to extract the destination classes from a Also, creating |
The NavHost state is properly restored, the backstack entry is properly decoded and the VM is properly created using those nav params. So my answer is: No, this is not the only correct way.
I disagree, I believe the VM inputs should be clear and not hidden in a map. SavedStateHandle is an implementation detail of state retention, not an ideal way to provide necessary inputs. But ok, let's be open-minded here - is this feature documented anywhere - I mean - is there a contract that promises SavedStateHandle to contain navigation inputs? If so, I'm very ok to add it. If not, I am open to giving you the possibility. |
Thanks for reopening the issue. I really appreciate it. I'm not sure, if you could count that as documented feature, but google is using this in their official docs:
However, this is your library after all. And if this is against your typical use cases, I can completely see, why you do not want (or have the time) to implement it. It's just that I really like this library and that's the only nice-to-have feature it's missing for me. |
|
Might wanna edit that last message to say |
I am currently migrating an app to use your library. I have compared a few typed navigation compose libraries and found your approach to be the best for my liking.
I have one question, though: How can I access the navigation arguments inside a
ViewModel
? TheViewModel
is injected in my screens viaviewModel: MyViewModel = hiltViewModel()
.My
ViewModel
has the defaulthandle: SavedStateHandle
argument.(Bonus question: What's the best way to create
NavGraph
-ScopedViewModel
s using your library?)The text was updated successfully, but these errors were encountered: