https://kotlinlang.org logo
#multiplatform
Title
# multiplatform
a

Ahmad Hassan

11/01/2023, 11:41 AM
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

Pablichjenkov

11/02/2023, 12:18 PM
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

Ahmad Hassan

11/02/2023, 12:22 PM
I have done this, now struggling after this. Usage with Koin
p

Pablichjenkov

11/02/2023, 12:27 PM
What fails more specifically?
a

Ahmad Hassan

11/02/2023, 12:30 PM
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

Pablichjenkov

11/02/2023, 12:53 PM
It seems that TextSharing_iOS is not in Swift Better doing it in swift
a

Ahmad Hassan

11/03/2023, 9:42 AM
I want to use it in Compose Multiplatform
7 Views