Skip to content

callback on file picker selection #276

New issue

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

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

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -36,6 +36,7 @@ public actual suspend fun <Out> FileKit.openFilePicker(
title: String?,
directory: PlatformFile?,
dialogSettings: FileKitDialogSettings,
onSelection: ((Int) -> Unit)?
): Out? = withContext(Dispatchers.IO) {
// Throw exception if registry is not initialized
val registry = FileKit.registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,22 @@ public expect suspend fun <Out> FileKit.openFilePicker(
title: String? = null,
directory: PlatformFile? = null,
dialogSettings: FileKitDialogSettings = FileKitDialogSettings.createDefault(),
onSelection: ((Int) -> Unit)? = null,
): Out?

public suspend fun FileKit.openFilePicker(
type: FileKitType = FileKitType.File(),
title: String? = null,
directory: PlatformFile? = null,
dialogSettings: FileKitDialogSettings = FileKitDialogSettings.createDefault(),
onSelection: ((Int) -> Unit)? = null,
): PlatformFile? {
return openFilePicker(
type = type,
mode = FileKitMode.Single,
title = title,
directory = directory,
dialogSettings = dialogSettings,
onSelection = onSelection,
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,15 @@ public actual suspend fun <Out> FileKit.openFilePicker(
title: String?,
directory: PlatformFile?,
dialogSettings: FileKitDialogSettings,
onSelection: ((Int) -> Unit)?,
): Out? = when (type) {
// Use PHPickerViewController for images and videos
is FileKitType.Image,
is FileKitType.Video,
is FileKitType.ImageAndVideo -> callPhPicker(
mode = mode,
type = type
type = type,
onSelection = onSelection,
)?.map { PlatformFile(it) }?.let { mode.parseResult(it) }

// Use UIDocumentPickerViewController for other types
Expand Down Expand Up @@ -295,6 +297,7 @@ private suspend fun callPicker(
private suspend fun <Out> callPhPicker(
mode: FileKitMode<Out>,
type: FileKitType,
onSelection: ((Int) -> Unit)? = null, // <-- new optional callback
): List<NSURL>? = withContext(Dispatchers.Main) {
val pickerResults: List<PHPickerResult> = suspendCoroutine { continuation ->
// Create a picker delegate
Expand Down Expand Up @@ -324,7 +327,6 @@ private suspend fun <Out> callPhPicker(
PHPickerFilter.videosFilter,
)
)

else -> throw IllegalArgumentException("Unsupported type: $type")
}

Expand All @@ -341,6 +343,9 @@ private suspend fun <Out> callPhPicker(
)
}

// NEW: Notify about the number of selected items as soon as the user picks
onSelection?.invoke(pickerResults.size)

val fileManager = NSFileManager.defaultManager

return@withContext withContext(Dispatchers.IO) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public actual suspend fun <Out> FileKit.openFilePicker(
title: String?,
directory: PlatformFile?,
dialogSettings: FileKitDialogSettings,
onSelection: ((Int) -> Unit)?
): Out? = withContext(Dispatchers.Default) {
suspendCoroutine { continuation ->
// Create input element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public actual suspend fun <Out> FileKit.openFilePicker(
title: String?,
directory: PlatformFile?,
dialogSettings: FileKitDialogSettings,
onSelection: ((Int) -> Unit)?
): Out? = callPicker(
mode = when (mode) {
is FileKitMode.Single -> Mode.Single
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public actual suspend fun <Out> FileKit.openFilePicker(
title: String?,
directory: PlatformFile?,
dialogSettings: FileKitDialogSettings,
onSelection: ((Int) -> Unit)?
): Out? = withContext(Dispatchers.Default) {
suspendCoroutine { continuation ->
// Create input element
Expand Down