Is there any other way to have a `viewModel` scope...
# compose
n
Is there any other way to have a
viewModel
scoped to a navigation destination than using
hiltViewModel
?
y
So without DI at all?
n
Yep
y
I dont think there is any specific "ways" to do it without DI. You just have to create a new instance of your ViewModel manually and handle its lifecycles etc.
s
Maybe take a look at this https://github.com/airbnb/mavericks/blob/main/mvrx-compose/src/main/kotlin/com/airbnb/mvrx/compose/MavericksComposeExtensions.kt for inspiration to make your own? Or even the source code of
hiltViewModel
it's all public anyway.
i
Well,
viewModel()
also scopes to a navigation destination. All
hiltViewModel()
does is provide the a prebuilt Factory hooked up to Hilt.
👀 1
❤️ 1
m
It is possible. This is a sample of how I'm using it in the app: https://gist.github.com/SumeraMartin/e31a9bf7103d50d803665d1fe93a0a81
n
@Ian Lake Thanks for the information. I have visited the docs and that states the same thing. I thought only way to scope a ViewModel to a navigation destination is using a DI solution. But that's precisely not the case. I am pretty sure I have read these docs page before but somehow overlooked on these information.