Hi guys! I want to implement text share functional...
# multiplatform
a
Hi guys! I want to implement text share functionality in KMP, how it be done as Android involves context and I'm using Koin for DI..?
p
You can create an interface in commonMain kotlin and implement it in iOS-Swift and Android side.
Something like
Copy code
Interface StringResolver{
   fun getStringById(stringId: Int) : String
}
Then provide to koin each implementation the swift one and the Android one.
a
I have done this, now struggling after this. Usage with Koin
p
What fails more specifically?
a
Copy code
class TextSharing_iOS: TextSharing {
    override fun shareText(text: String) {
        val activityItems = listOf(text)
        val activityViewController = UIActivityViewController(activityItems, null)

        val application = UIApplication.sharedApplication
        application.keyWindow?.rootViewController?.presentViewController(
            activityViewController,
            animated = true,
            completion = null
        )
    }
}
Copy code
class AndroidTextSharing(private val context: Context): TextSharing {
    override fun shareText(text: String) {
        val shareIntent = Intent().apply {
            action = Intent.ACTION_SEND
            putExtra(Intent.EXTRA_TEXT, text)
            type = "text/plain"
        }
        context.startActivity(
            Intent.createChooser(shareIntent, context.getString(
                R.string.app_name)))
    }
}
Now how to use in commonMain?
p
It seems that TextSharing_iOS is not in Swift Better doing it in swift
a
I want to use it in Compose Multiplatform
341 Views