Prateek Kumar
12/07/2023, 11:20 AMContext to Koin when using it have shared UI across platoform?
I am trying to use Datastore in Shared Module , And used actual/expect to have diff platform modules to feed Koin.
actual fun platformModules(): Module = module {
factory <DatastoreProvider>{ DatastoreProviderImpl(get()) }
}
And have a single UI for all platforms
@Composable
fun App() {
KoinApplication(allKoinModules) {
Navigator(HomeScreen)
}
}
Datastore needs Android Context , How can we do that?Spoudel347
12/07/2023, 11:23 AMPrateek Kumar
12/07/2023, 11:29 AMSpoudel347
12/07/2023, 11:32 AMPrateek Kumar
12/07/2023, 11:33 AMSpoudel347
12/07/2023, 11:37 AMapp-initializer is useful for common modules/libraries because if the end user creates their own application class, the application class method won't work.Prateek Kumar
12/07/2023, 11:39 AMSpoudel347
12/07/2023, 11:40 AMJacob Ras
12/07/2023, 12:12 PMactual fun platformModules(): Module = module {
factory <DatastoreProvider>{ DatastoreProviderImpl(get()) }
}
It's an actual implementation of a platform module. Good. It provides the DataStoreProviderImpl and in its constructor does get() to get the Android Context (which is available by default in Koin on Android). Also good! Doesn't it work?Prateek Kumar
12/07/2023, 12:26 PMJacob Ras
12/07/2023, 12:32 PMandroid source set). You still use the modules from the common code, it's just that the starting is in a platform source set. Like in the KMP example: https://github.com/InsertKoinIO/hello-kmp/blob/main/androidApp/src/main/java/com/example/helloworldkmp/android/MainApplication.kt That's what I'm doing in my projects.
Another way would be to dynamically add a module that provides the Android Context to the Koin graph/tree from your custom Application. Seems that possible but I haven't done that with Koin. See https://github.com/InsertKoinIO/koin/issues/419Pablichjenkov
12/07/2023, 1:10 PMPrateek Kumar
12/09/2023, 7:05 PM@Composable
fun AndroidApp(context: Context) {
KoinApplication(moduleList = {
sharedModule + module {
factory<Context> { context.applicationContext }
}
}) {
Navigator(HomeScreen)
}
}
Something like this.