Hi. There is a <file picker library> I'm contribut...
# multiplatform
g
Hi. There is a file picker library I'm contributing to and need some advice for a better API. My initial idea was to have a common code that would return String of selected file on all platforms, but... it is not that simple.
To pick a file on different platforms there is different object, for example on MacOS there is NSURL, on JVM there is java.io.File, on other platforms there will be other different objects (A String path will not work on Web). Currently this is the function in commonMain and it has implementations in androidTarget(), jvm(), js(IR), macosX64(), iosX64(), iosArm64() and iosSimulatorArm64()
Copy code
public interface MPFile<out T : Any> {
    // on JS this will be a file name, on other platforms it will be a file path
    public val path: String
    public val platformFile: T
    public suspend fun getFileByteArray(): ByteArray
}

public typealias FileSelected = (MPFile<Any>?) -> Unit

@Composable
public expect fun FilePicker(
    show: Boolean,
    initialDirectory: String? = null,
    fileExtensions: List<String> = emptyList(),
    title: String? = null,
    onFileSelected: FileSelected,
)
It works, but I'm forced to add arguments, which does not work or works differently, to all targets. for example
title
argument, it only has effect on JVM and MacOS. Return type is
MPFile<Any>
so it is always required to be cast to some platform specific object, here is the issue about that https://github.com/Wavesonics/compose-multiplatform-file-picker/issues/69. few functionality that I cant imagine being possible to cover via common code • on some platforms there is synchronous and asynchronous versions for file picker dialog • on Windows there are 2 variants of file pickers: GetOpenFileNameA and GetOpenFileNameW • There could be a case where you want to set a dialog title on Windows and not on MacOS Currently I think that there should be no common code written in this library. This should be done in other projects that depend on the library. How would you design the API for it?
cc: @Adam Brown
j
I would also note that some valid paths are not valid UTF-16 (or UTF-8) and cannot be represented as a String.
blob open mouth 2
At best you probably need to wait for kotlinx.io to have a path and file system abstraction to return something in common code.
g
Thanks for reply. Do they also plan to support web? I mean object returned via
<input type="file">
? that seems to be this https://developer.mozilla.org/en-US/docs/Web/API/File. Kotlinx.io library code in JS tries to load fs and path modules. This will not work in a browser, but only for Nodejs I guess all I can do is wait and it is better to keep the common code, okay, I think I know what to do. Common code that is already written will stay. Also there will be platform specific functions like FilePickerAndroid and FilePickerJvm, there was a PR for this already here https://github.com/Wavesonics/compose-multiplatform-file-picker/pull/89/commits/c7975da115a7f4be27eed5b7b341a0f8ea0e0cd7