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

Zeeshan Syed

03/02/2023, 11:31 AM
Hi everyone. I'm setting up a MultiPlatform project with two platforms (JVM and Android). And I have a common UI using Compose. Let's say I also have a function which reads an NFC card connected to the JVM machine/Android. For this, I created a
actual/expect fun readCard()
Now the issue is, the function needs 3 strings of keys to read the card. So, I made the code as
expect fun readCard(k1: String, k2: String, k3: String).
In addition to this, for Android, it needs a
Tag
object as well. Now, how do I pass it to the function. I cannot add the Tag as 4th argument because in common:desktopMain, it doesn't know about what Tag is and it doesn't need. Alternatively I can make it as
Any
. But do you also think that is the only option or is there any better way?
b

Big Chungus

03/02/2023, 11:44 AM
Expect/actual tag too and require it for jvm actual too and just don't use it?
Although it sounds like a job for interfaces and DI
z

Zeeshan Syed

03/02/2023, 11:48 AM
That Tag class is a part of the Android SDK. So, I cannot put that in arguments
b

Big Chungus

03/02/2023, 11:49 AM
You can. actual typealias on android, shell class on jvm
Although I'm not sure how you'd go about obtaining it on common. Maybe another expect provider fun?
z

Zeeshan Syed

03/02/2023, 11:53 AM
Yeah. In common for expect function also I cannot add this Tag argument. Even for the provider, we have to declare the return type as well right.
b

Big Chungus

03/02/2023, 11:56 AM
Which would be your expect Tag
z

Zeeshan Syed

03/02/2023, 12:00 PM
This code is in common module. Tag class not available here.
neither I can put as argument nor I can set as return type. That Tag class is available only in android module or in the common:androidMain
Copy code
// commonMain
expect class Tag

// androidMain
actual typealias Tag = android.nfc.Tag

// jvmMain
actual class Tag
z

Zeeshan Syed

03/02/2023, 12:37 PM
Ohh.. that's neat. I failed to understand it without an example 😅. Thank you.
f

Friedger

07/04/2023, 12:27 PM
@Zeeshan Syed Is there a library for NFC?`Something like https://github.com/revtel/react-native-nfc-manager for react native?
2 Views