Hi, what is the equivalent of `hiltViewModel()` i...
# koin
r
Hi, what is the equivalent of
hiltViewModel()
in koin? I want to get parameters passed to compose navigation using
SavedStateHandle
in my viewmodel.
x
there’s koin-view-model, where you can do
Copy code
viewModel { param -> 
  ViewModel(handle = param.get<SavedStateHandle>()) 
}
a
still problem with DSL?
what version?
r
I have a compose destination
Copy code
composable(
            route = "$Login/{$IS_BACK_ALLOWED}",
            arguments = listOf(
                navArgument(IS_BACK_ALLOWED) { type = NavType.BoolType }
            )
        ) {
            LoginScreen()
        }
My screen
Copy code
LoginScreen(
   loginViewModel: LoginViewModel = getViewModel()
)
My Viewmodel
Copy code
class LoginViewModel(handle: SavedStateHandle): ViewModel() {
    
       fun doSomething() {
          val isBackAllowed = handle.get<Boolean>(IS_BACK_ALLOWED)
       }
}
But
isBackAllowed
is always null koin version 3.1.4
Am I doing something wrong? Or is it not supported in koin ?
a
can you extract a sample project?
r
Sure will do and ping back
a
ok thanks 👍
r
Hi Arnaud, did you get a chance to go through the sample?
a
not yet
r
I am passing nav args in multiple destinations and right now I have to pass them from composable to viewmodel which is a pain 🥲. It would be great if you can have a look at the sample and provide your guidance. Thanks in advance! 🙂
a
yeah, extracting everything to prepare Compose projects and next Koin versions those days
give me more time to take a look
did you tried navigation graph ViewModel?
I ve seen your app code. It’s a limitation of Koin for now. Need to fix it
r
Thanks a lot for the reply! If you are accepting contributions would love to go through the code and submit my contribution for the same 🙂
a
koin-androidx-compose is in a sperated github project to help go over compose project
but have its in own sync rythm
from what I’ve seen, the “official” ktx
viewModel()
function is running against NavBackStackEntry
and then, internal SavedStateFactory is having default arguments from NavBackStackEntry
this is where, it’s missing something on Koin side
I would consider rewrite from scratch if needed, but we need to wire on Koin context to resolve things
feel free to come chat in #koin-dev
r
With my limited understanding I have made some changes and got it to work. But I am not sure how correct my changes are 🙈. I have pushed the changes in the same sample. Would be great if you can check below commit whenever you get some time. Eagerly waiting for your reply 🙂 https://github.com/rubenquadros/Koin-Compose-Sample/commit/b814bd1c99eed8c84c1c0c1a1b1e7f4fba73c9f2
a
interesting, let me check 🙂
also just a warning, as Compose is runiing against kotlin 1.5 and you pull version 1.6 from other dependencies
does it do the job your patch? seems a good quick patch for now 👍
r
yes it is doing the job.. will raise a pr and you can check again 🙂
a
great job 👍
👍 1
r
Please review the PR. Please let me know if any changes need to be done in the documentation as well 🙂 https://github.com/InsertKoinIO/koin/pull/1286
a
Let me check that yes 🙂