FileKit
FileKit copied to clipboard
how to pass PlatformFile via compose navigation since its not Serializable
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.
Hi @hellosagar and @yannickpulver!
That's a good idea, I'll try to make PlatformFile Serializable that could be useful in many situations.