I'm trying to define a ViewModel that accepts a na...
# compose
c
I'm trying to define a ViewModel that accepts a navigation lambda as such
Copy code
@HiltViewModel
class MyScreenViewModel
@Inject
constructor(var navEvent: (String) -> Unit)
which makes sure that this navEvent is passed in.
Copy code
fun MyScreen(viewModel: MyScreenViewModel = hiltViewModel(//how do I pass something in))
Is there an easy way to pass in a lambda? I've passed arguments into viewModels with compose before but hiltViewModel did all the heavy lifting and exposed the argument via savedStateHandle. Can I somehow pass this lambda into my savedStateHandle? I'd be okay with that.
i
Hilt does not support assisted injection (values provided at runtime): https://github.com/google/dagger/issues/2287
c
Okay good. I thought I was overlooking something in the docs. I'll just provide a little init() method with the params. Less fancy (and safety), but it works and will fail fast. Thanks
k
Out of curiosity @Colton Idle, are you moving your navigation to ViewModel?
c
I'm passing lamdas to the viewModel where needed and so the navigate calls are still contained in my navHost
👍 1