For dependencies that need to be instantiated in n...
# koin
f
For dependencies that need to be instantiated in native code (like in Swift) and then injected into our shared Kotlin module, can this be handled with
koin-annotations
?
Copy code
// commonMain
interface GenerativeModel {
    suspend fun generateTextContent(prompt: String): String?
    suspend fun generateJsonContent(prompt: String): String?
    suspend fun generateImage(prompt: String): ByteArray?
}
// iosMain
fun initialiseKoin(generativeModel: GenerativeModel) {
    startKoin {
        modules(
            module { single<GenerativeModel> { generativeModel } }
        )
    }
}
// swift
@main
struct iOSApp: App {
    init() {
        initialiseKoin(generativeModel: GenerativeModelIOS())
    }
}
p
f
Of course, but I didn't find anything in the documentation about injecting instances from native code. It seems they are all created from
iosMain
.
p
I see what you mean. Right, I don't think there is anything that automatically binds the swift side implementation to the iOSMain module declaration. So this has to be done manually. What you could do is pass a global swift factory for all your implementations into Koin modules when koin is started. Like in this article: https://proandroiddev.com/how-to-use-swift-packages-in-kotlin-multiplatform-using-koin-c7d24fdbbbd7 Not ideal but at least centralize all the definitions factories in one place. I did create an issue in the Koin annotations repo, suggesting to create a swift API, using swift attributes (basically the equivalent of kotlin annotations) to mark swift definitions and have them automatically added into modules but it seems it hasn't been picked up by the Koin folks. Maybe it needs more upvotes.
Here: https://github.com/InsertKoinIO/koin-annotations/issues/168 It got closed since it is a feature request rather than an issue but we can find other channels to let the Koin team know we are interested in something like this.
a
Yes, the Koin - iOS bridge/equivalent is a real question 👍
👌 1
f
I got a partial solution here. It difficult to make as a stand-alone, I didn’t found a good solution
👍 1