Skip to content
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

[iOS] Picker not displayed when not on main view controller #136

Merged
merged 4 commits into from
Jul 29, 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 @@ -3,6 +3,7 @@ package com.mohamedrejeb.calf.picker
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.interop.LocalUIViewController
import com.mohamedrejeb.calf.core.InternalCalfApi
import com.mohamedrejeb.calf.io.KmpFile
import kotlinx.cinterop.BetaInteropApi
Expand All @@ -25,6 +26,7 @@ import platform.PhotosUI.PHPickerViewControllerDelegateProtocol
import platform.UIKit.UIApplication
import platform.UIKit.UIDocumentPickerDelegateProtocol
import platform.UIKit.UIDocumentPickerViewController
import platform.UIKit.UIViewController
import platform.UniformTypeIdentifiers.UTType
import platform.UniformTypeIdentifiers.UTTypeApplication
import platform.UniformTypeIdentifiers.UTTypeAudio
Expand Down Expand Up @@ -57,6 +59,7 @@ private fun rememberDocumentPickerLauncher(
onResult: (List<KmpFile>) -> Unit,
): FilePickerLauncher {
val scope = rememberCoroutineScope()
val currentUIViewController = LocalUIViewController.current

val delegate =
remember {
Expand Down Expand Up @@ -114,7 +117,7 @@ private fun rememberDocumentPickerLauncher(
}
}

return remember {
return remember(currentUIViewController) {
FilePickerLauncher(
type = type,
selectionMode = selectionMode,
Expand All @@ -126,7 +129,7 @@ private fun rememberDocumentPickerLauncher(
selectionMode = selectionMode,
)

UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController(
currentUIViewController.presentViewController(
pickerController,
true,
null,
Expand All @@ -143,6 +146,7 @@ private fun rememberImageVideoPickerLauncher(
onResult: (List<KmpFile>) -> Unit,
): FilePickerLauncher {
val scope = rememberCoroutineScope()
val currentUIViewController = LocalUIViewController.current

val pickerDelegate = remember {
object : NSObject(), PHPickerViewControllerDelegateProtocol {
Expand Down Expand Up @@ -172,7 +176,7 @@ private fun rememberImageVideoPickerLauncher(
}
}

return remember {
return remember(currentUIViewController) {
FilePickerLauncher(
type = type,
selectionMode = selectionMode,
Expand All @@ -184,7 +188,7 @@ private fun rememberImageVideoPickerLauncher(
selectionMode = selectionMode,
)

UIApplication.sharedApplication.keyWindow?.rootViewController?.presentViewController(
currentUIViewController.presentViewController(
imagePicker,
true,
null,
Expand Down Expand Up @@ -256,6 +260,7 @@ private fun createUIDocumentPickerViewController(
return pickerController
}


private fun createPHPickerViewController(
delegate: PHPickerViewControllerDelegateProtocol,
type: FilePickerFileType,
Expand Down Expand Up @@ -288,6 +293,7 @@ private fun createPHPickerViewController(
return picker
}


actual class FilePickerLauncher actual constructor(
type: FilePickerFileType,
selectionMode: FilePickerSelectionMode,
Expand Down