FileKit icon indicating copy to clipboard operation
FileKit copied to clipboard

how to pass PlatformFile via compose navigation since its not Serializable

Open hellosagar opened this issue 10 months ago • 1 comments

hellosagar avatar Apr 03 '25 21:04 hellosagar

Haven't tried it with Compose Navigation, but I'm pretty sure it would work the same way. You could just pass the path as a String and then recreate a PlatformFile object on the receiving end.

val path = file.path // file is a PlatformFile
...
navigate(SomeDestination(path))

// on the receiving end

val file = PlatformFile(path)

However, the default PlatformFile constructor didn't seem to work for me for Android with urls starting with content://. Therefore, I created this extension:

expect fun String.toPlatformFile(): PlatformFile  // commonMain
actual fun String.toPlatformFile() = PlatformFile(toUri()) // androidMain <-- this one is different 
actual fun String.toPlatformFile(): PlatformFile = PlatformFile(this) // desktopMain
actual fun String.toPlatformFile() = PlatformFile(this) // desktopMain

val file = path.toPlatformFile()

And with that it worked perfectly well for me.

yannickpulver avatar Apr 11 '25 20:04 yannickpulver

Hi @hellosagar and @yannickpulver!

That's a good idea, I'll try to make PlatformFile Serializable that could be useful in many situations.

vinceglb avatar Jul 26 '25 15:07 vinceglb