Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

run save file on ios on main thread #124

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -80,51 +80,53 @@ public actual object FileKit {
extension: String,
initialDirectory: String?,
platformSettings: FileKitPlatformSettings?,
): PlatformFile? = suspendCoroutine { continuation ->
// Create a picker delegate
documentPickerDelegate = DocumentPickerDelegate(
onFilesPicked = { urls ->
val file = urls.firstOrNull()?.let { PlatformFile(it) }
continuation.resume(file)
},
onPickerCancelled = {
continuation.resume(null)
}
)
): PlatformFile? = withContext(Dispatchers.Main) {
suspendCoroutine { continuation ->
// Create a picker delegate
documentPickerDelegate = DocumentPickerDelegate(
onFilesPicked = { urls ->
val file = urls.firstOrNull()?.let { PlatformFile(it) }
continuation.resume(file)
},
onPickerCancelled = {
continuation.resume(null)
}
)

val fileName = "$baseName.$extension"
val fileName = "$baseName.$extension"

// Get the fileManager
val fileManager = NSFileManager.defaultManager
// Get the fileManager
val fileManager = NSFileManager.defaultManager

// Get the temporary directory
val fileComponents = fileManager.temporaryDirectory.pathComponents?.plus(fileName)
?: throw IllegalStateException("Failed to get temporary directory")
// Get the temporary directory
val fileComponents = fileManager.temporaryDirectory.pathComponents?.plus(fileName)
?: throw IllegalStateException("Failed to get temporary directory")

// Create a file URL
val fileUrl = NSURL.fileURLWithPathComponents(fileComponents)
?: throw IllegalStateException("Failed to create file URL")
// Create a file URL
val fileUrl = NSURL.fileURLWithPathComponents(fileComponents)
?: throw IllegalStateException("Failed to create file URL")

// Write the bytes to the temp file
writeBytesArrayToNsUrl(bytes, fileUrl)
// Write the bytes to the temp file
writeBytesArrayToNsUrl(bytes, fileUrl)

// Create a picker controller
val pickerController = UIDocumentPickerViewController(
forExportingURLs = listOf(fileUrl)
)
// Create a picker controller
val pickerController = UIDocumentPickerViewController(
forExportingURLs = listOf(fileUrl)
)

// Set the initial directory
initialDirectory?.let { pickerController.directoryURL = NSURL.fileURLWithPath(it) }
// Set the initial directory
initialDirectory?.let { pickerController.directoryURL = NSURL.fileURLWithPath(it) }

// Assign the delegate to the picker controller
pickerController.delegate = documentPickerDelegate
// Assign the delegate to the picker controller
pickerController.delegate = documentPickerDelegate

// Present the picker controller
UIApplication.sharedApplication.firstKeyWindow?.rootViewController?.presentViewController(
pickerController,
animated = true,
completion = null
)
// Present the picker controller
UIApplication.sharedApplication.firstKeyWindow?.rootViewController?.presentViewController(
pickerController,
animated = true,
completion = null
)
}
}

public actual suspend fun isSaveFileWithoutBytesSupported(): Boolean = true
Expand All @@ -133,38 +135,41 @@ public actual object FileKit {
mode: Mode,
contentTypes: List<UTType>,
initialDirectory: String?,
): List<NSURL>? = suspendCoroutine { continuation ->
// Create a picker delegate
documentPickerDelegate = DocumentPickerDelegate(
onFilesPicked = { urls -> continuation.resume(urls) },
onPickerCancelled = { continuation.resume(null) }
)

// Create a picker controller
val pickerController = UIDocumentPickerViewController(forOpeningContentTypes = contentTypes)

// Set the initial directory
initialDirectory?.let { pickerController.directoryURL = NSURL.fileURLWithPath(it) }

// Setup the picker mode
pickerController.allowsMultipleSelection = mode == Mode.Multiple

// Assign the delegate to the picker controller
pickerController.delegate = documentPickerDelegate

// Present the picker controller
UIApplication.sharedApplication.firstKeyWindow?.rootViewController?.presentViewController(
pickerController,
animated = true,
completion = null
)
): List<NSURL>? = withContext(Dispatchers.Main) {
suspendCoroutine { continuation ->
// Create a picker delegate
documentPickerDelegate = DocumentPickerDelegate(
onFilesPicked = { urls -> continuation.resume(urls) },
onPickerCancelled = { continuation.resume(null) }
)

// Create a picker controller
val pickerController =
UIDocumentPickerViewController(forOpeningContentTypes = contentTypes)

// Set the initial directory
initialDirectory?.let { pickerController.directoryURL = NSURL.fileURLWithPath(it) }

// Setup the picker mode
pickerController.allowsMultipleSelection = mode == Mode.Multiple

// Assign the delegate to the picker controller
pickerController.delegate = documentPickerDelegate

// Present the picker controller
UIApplication.sharedApplication.firstKeyWindow?.rootViewController?.presentViewController(
pickerController,
animated = true,
completion = null
)
}
}

@OptIn(ExperimentalForeignApi::class)
private suspend fun <Out> callPhPicker(
mode: PickerMode<Out>,
type: PickerType,
): List<NSURL>? {
): List<NSURL>? = withContext(Dispatchers.Main) {
val pickerResults: List<PHPickerResult> = suspendCoroutine { continuation ->
// Create a picker delegate
phPickerDelegate = PhPickerDelegate(
Expand Down Expand Up @@ -206,7 +211,7 @@ public actual object FileKit {
)
}

return withContext(Dispatchers.IO) {
return@withContext withContext(Dispatchers.IO) {
val fileManager = NSFileManager.defaultManager

pickerResults.mapNotNull { result ->
Expand Down