mirror of
https://github.com/sudosylabs/vnidrop.git
synced 2026-08-05 10:29:58 +02:00
Add native file picker to core test UI
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package com.vnidrop.app.core
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import kotlinx.cinterop.ExperimentalForeignApi
|
||||
import platform.Foundation.NSURL
|
||||
import platform.UIKit.UIApplication
|
||||
import platform.UIKit.UIDocumentPickerDelegateProtocol
|
||||
import platform.UIKit.UIDocumentPickerViewController
|
||||
import platform.UIKit.UIModalPresentationFormSheet
|
||||
import platform.UniformTypeIdentifiers.UTTypeItem
|
||||
import platform.darwin.NSObject
|
||||
|
||||
private var retainedPickerDelegate: DocumentPickerDelegate? = null
|
||||
|
||||
@Composable
|
||||
actual fun rememberShareFilePicker(
|
||||
onFilePicked: (PickedShareFile) -> Unit,
|
||||
onError: (String) -> Unit,
|
||||
): ShareFilePicker = remember(onFilePicked, onError) {
|
||||
object : ShareFilePicker {
|
||||
@OptIn(ExperimentalForeignApi::class)
|
||||
override fun pickFile() {
|
||||
val presenter = UIApplication.sharedApplication.keyWindow?.rootViewController
|
||||
if (presenter == null) {
|
||||
onError("Could not find an iOS view controller for the document picker")
|
||||
return
|
||||
}
|
||||
|
||||
val picker = UIDocumentPickerViewController(forOpeningContentTypes = listOf(UTTypeItem), asCopy = false)
|
||||
val delegate = DocumentPickerDelegate(onFilePicked, onError)
|
||||
retainedPickerDelegate = delegate
|
||||
picker.delegate = delegate
|
||||
picker.modalPresentationStyle = UIModalPresentationFormSheet
|
||||
presenter.presentViewController(picker, animated = true, completion = null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
actual suspend fun sharePickedFile(
|
||||
repository: CoreRepository,
|
||||
file: PickedShareFile,
|
||||
transferName: String,
|
||||
senderName: String,
|
||||
) {
|
||||
repository.shareSecurityScopedFileUrl(file.value, file.displayName, transferName, senderName)
|
||||
}
|
||||
|
||||
private class DocumentPickerDelegate(
|
||||
private val onFilePicked: (PickedShareFile) -> Unit,
|
||||
private val onError: (String) -> Unit,
|
||||
) : NSObject(), UIDocumentPickerDelegateProtocol {
|
||||
override fun documentPicker(controller: UIDocumentPickerViewController, didPickDocumentsAtURLs: List<*>) {
|
||||
val url = didPickDocumentsAtURLs.firstOrNull() as? NSURL
|
||||
if (url == null) {
|
||||
onError("The selected iOS document URL was invalid")
|
||||
} else {
|
||||
val displayName = url.lastPathComponent ?: "transfer"
|
||||
onFilePicked(PickedShareFile(url.absoluteString ?: url.path.orEmpty(), displayName))
|
||||
}
|
||||
retainedPickerDelegate = null
|
||||
}
|
||||
|
||||
override fun documentPickerWasCancelled(controller: UIDocumentPickerViewController) {
|
||||
retainedPickerDelegate = null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user