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

Fix file picker presentation issue in iOS #170

Open
wants to merge 2 commits 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 @@ -160,12 +160,25 @@ public actual object FileKit {
// Assign the delegate to the picker controller
pickerController.delegate = documentPickerDelegate

// Present the picker controller
UIApplication.sharedApplication.firstKeyWindow?.rootViewController?.presentViewController(
pickerController,
animated = true,
completion = null
)
// Check if a view controller is already presenting another view controller
val rootViewController = UIApplication.sharedApplication.firstKeyWindow?.rootViewController
if (rootViewController?.presentedViewController != null) {
rootViewController.dismissViewControllerAnimated(true) {
// Present the picker controller
rootViewController.presentViewController(
pickerController,
animated = true,
completion = null
)
}
} else {
// Present the picker controller
rootViewController?.presentViewController(
pickerController,
animated = true,
completion = null
)
}
}
}

Expand Down Expand Up @@ -211,12 +224,25 @@ public actual object FileKit {
controller.delegate = phPickerDelegate
controller.presentationController?.delegate = phPickerDismissDelegate

// Present the picker controller
UIApplication.sharedApplication.firstKeyWindow?.rootViewController?.presentViewController(
controller,
animated = true,
completion = null
)
// Check if a view controller is already presenting another view controller
val rootViewController = UIApplication.sharedApplication.firstKeyWindow?.rootViewController
if (rootViewController?.presentedViewController != null) {
rootViewController.dismissViewControllerAnimated(true) {
// Present the picker controller
rootViewController.presentViewController(
controller,
animated = true,
completion = null
)
}
} else {
// Present the picker controller
rootViewController?.presentViewController(
controller,
animated = true,
completion = null
)
}
}

return@withContext withContext(Dispatchers.IO) {
Expand Down
43 changes: 9 additions & 34 deletions samples/sample-core/appleApps/iOSApp/ContentView.swift
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
//
// ContentView.swift
// iOSApp
//
// Created by Vincent Guillebaud on 04/04/2024.
//

import SwiftUI
import KMPObservableViewModelSwiftUI
import SamplePickerKt
Expand All @@ -13,37 +6,23 @@ struct ContentView: View {
@StateViewModel
var viewModel = MainViewModel(platformSettings: nil)

@State private var sheetIsPresented: Bool = false

var body: some View {
let uiState = viewModel.uiState.value as? MainUiState

// Convert Set to Array
let files = Array(uiState?.files ?? [])

VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")

Button("Single image picker") {
viewModel.pickImage()
}

Button("Multiple images picker") {
viewModel.pickImages()
}

Button("Single file picker, only png") {
viewModel.pickFile()
}

Button("Multiple file picker, only png") {
viewModel.pickFiles()
}

Button("Directory picker") {
viewModel.pickDirectory()
Button("ShowSheet") {
sheetIsPresented.toggle()
}
.sheet(isPresented: $sheetIsPresented, content: {
Button("Multiple file picker, only png") {
viewModel.pickFiles()
}
})

if uiState?.loading == true {
ProgressView()
Expand All @@ -59,7 +38,3 @@ struct ContentView: View {
.padding()
}
}

#Preview {
ContentView()
}