Hi, I'm trying to use Compose alongside ViewModels...
# kodein
k
Hi, I'm trying to use Compose alongside ViewModels, but I'm pretty new to both. I'm using version 7.7.0 of kodein, and have a basic App :
Copy code
class App : Application(), DIAware {
    val homeModule: (app: App) -> DI.Module = { app ->

        DI.Module("homeModule") {
            bindProvider { HomeViewModel(app) }
        }
    }

    override val di: DI by DI.lazy {
        import(homeModule(this@App))
    }
[...]
}
ViewModel:
Copy code
class HomeViewModel(app: App) : AndroidViewModel(app), DIAware {

    override val di by closestDI()
    private val initializingUseCase: InitializingUseCase by instance()

    private val _status: MutableStateFlow<String> = MutableStateFlow("")
    val status: StateFlow<String> = _status.asStateFlow()

[...]
}
Home Composable:
Copy code
@Composable
fun Home() {
    val di = androidContextDI()
    val viewModel by di.instance() // -> This doesn't work
    val status by getLifecycleAwareState(flow = viewModel.status, initialValue = "")
    StatelessHome(status = status)
}
I can't find the correct way to instanciate my
viewModel
. The extensions from
kodein-di-framework-android-x-viewmodel
I found are based on
AppCompatActivity
r
Hi. using DI module for
android-x
could help
with the following dependencies
Copy code
implementation 'org.kodein.di:kodein-di-framework-compose:7.7.0'
    implementation 'org.kodein.di:kodein-di-framework-android-x:7.7.0'
    implementation 'org.kodein.di:kodein-di-framework-android-x-viewmodel:7.7.0'
k
Thanks a lot, I'll check it out
@romainbsl I've played around with your gist, but can't get it to work the way I want: I keep getting a brand new instance of
homeViewModel
whenever there's a Compose refresh. Here's my modified gist: https://gist.github.com/lucmazon/90644bce6b7215092356285c5dbcdf90 Any idea why that happens?
r
because we use a 
bindProvider {}
 that is invoked each time we call 
rememberInstance
1. Using a
bindSingleton
might work
2. We miss a
rememberViewModel
based on the
viewModel
delegate from
kodein-di-framework-android-x-viewmodel
it handles its own storage of the viewmodel based on the lifecycle owner
k
Yes, that was what I was looking for
r
Going with the 1. just work ATM, until a better integration with Compose
k
Alright, will do, thanks for your help
Should I report this somewhere?
r
you can create an issue on Github as a reminder, not sure I'll be able to do it right now
thanks 🙏
I add a
rememberViewModel
@Composable function
you can try it as a SNAPSHOT
maven { url = "<https://oss.sonatype.org/content/repositories/snapshots>" }
org.kodein.di:kodein-di-framework-compose:7.9.0-master-SNAPSHOT
k
It does work perfectly! I'm curious about what you did, but can't see any commit in here: https://github.com/Kodein-Framework/Kodein-DI-Compose . Is it on another repository?
r
Oups this project is not what you think. its a playground
k
aaah, that explains it ^^. So my issue is in the wrong place…
r
That is why I never saw it 😅