So not compose specifically... but a question in h...
# compose-android
c
So not compose specifically... but a question in how you all approach compose + android and dealing with apis that require Android Context. In this case... my task at work is to implement some play services code which requires context. On one hand... I basically just want to add a method to my viewModel called
fun launchPlayStoreDialog(context: Context)
and basically do everything there, but then my ViewModel isn't easily unit testable because now I've included a hard android dependency. I guess you could say the same for if I tried to just do that stuff in a LaunchedEffect in compose. Would you all basically just have an lambda that bubbles up to the NavHost/etc to do this sort of thing? I know passing a context into a viewmodel isn't great (would that be a mem leak) but it makes sense as a one shot operation.
m
I usually add those functionalities to a separate class taking context as a dependency and inject this class where I need it.
c
In this case, it needs an activity context. hmmmmm
m
Yes, it can be easy if you are using a di lib.
Koin and hilt provides provides application context.
y
I think the official answer is keep these out of your ViewModel.
💯 3
It's tempting to put navigation which is also linked to the activity into the ViewModel. I think you are meant to refactor your logic and states, so that you UI code does this launch.
Pull up into a lambda so it's not in a stateless Composable you want to preview etc.