In KMP koin how do you create platform specific cl...
# koin
m
In KMP koin how do you create platform specific classes that depend on android Context on android but have empty constructor on ios (e.g.
class MyDataStore(context: Context)
) /
class MyDataStore
? 1. Create a separate module for each platform and use separate constructors. A lot of boilerplate. 2. Use nullable context and provide null on ios. Not pretty. 3. Provide koin's
androidContext()
directly in the constructor like
MyDataStore(context = androidContext())
. Not even sure if it's possible tbh. 4. Anything else?
1
d
If you need context in the constructor then probably it’s the best to have either common interface implemented in each module or expect/actual class and then only Android would have context, and you can use Koin to provide the dependencies… (your number 1 option, I would say that it’s least boilerplate since the context is only available in Android anyways) if you need context as a function param then you can define it via type alias
Copy code
// common code
expect class UiContext

@Composable
expect fun uiContext(): UiContext

//android code
actual typealias UiContext = Activity // or Context

@Composable
actual fun uiContext(): UiContext = requireActivity()

// ios code
actual typealias UiContext = UIViewController

@Composable
actual fun uiContext(): UiContext = LocalUIViewController.current
think smart 1
gratitude thank you 1