You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
When the user opens a camera through the UIImagePickerController and then cancels or makes a photo the application "reloads" or losses its state. Not sure whether it's a bug of Compose or not, I'm using a following function to obtain an instance of my router:
So, from my understanding it should not be reinstantiated.
From the logs I see that ComposeUIViewController invokes children composables one the application become active after the camera and with the function above my NavigationState get reinstantiated and I don't know why.
Implementation to open the Camera & Gallery
// TODO need to store somewhere globally, otherwise iOS wipes it out and these are never called back
private var imagePickerCoordinator: ImagePickerCoordinator? = null
private var imagePickerController: UIImagePickerController? = null
@Composable
internal actual fun ImagePickerResultHandler(
source: ImageSource?,
onRequestSourceDialog: () -> Unit,
onShowPermissionRationale: (Permission) -> Unit,
onLoading: (Boolean) -> Unit,
onError: (String) -> Unit,
onResult: (RuntimeImage) -> Unit
) {
val fileManager = LocalFileManager.current
val currentController = UIApplication.sharedApplication.keyWindow?.rootViewController ?: LocalUIViewController.current
imagePickerCoordinator = remember {
ImagePickerCoordinator(onImagePicked = { onResult(RuntimeImage(it)) }, onPickerDismissed = onRequestSourceDialog)
}
imagePickerController = remember(imagePickerCoordinator) {
UIImagePickerController().apply {
delegate = imagePickerCoordinator
}
}
LaunchedEffect(source) {
when (source) {
ImageSource.Camera -> {
Napier.d(tag = "IMAGE_PICKER") { "Camera" }
imagePickerController?.sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypeCamera
imagePickerController?.cameraCaptureMode = UIImagePickerControllerCameraCaptureMode.UIImagePickerControllerCameraCaptureModePhoto
currentController.showViewController(imagePickerController!!, null)
}
ImageSource.Gallery -> {
imagePickerController?.sourceType = UIImagePickerControllerSourceType.UIImagePickerControllerSourceTypePhotoLibrary
currentController.showViewController(imagePickerController!!, null)
}
is ImageSource.RemoteUrl -> {
fileManager.getImageFromCacheOrDownload(source.url, force = true)
.collect { state ->
when (state) {
is ApiState.Failed -> onError(state.reason)
ApiState.Idle -> {}
ApiState.Loading -> onLoading(true)
is ApiState.Succeed -> state.data.representation?.image?.let(onResult) ?: onError("")
}
}
}
null -> Unit
}
}
}
private class ImagePickerCoordinator(
private val onImagePicked: (ByteArray) -> Unit,
private val onPickerDismissed: () -> Unit
) : NSObject(), UIImagePickerControllerDelegateProtocol, UINavigationControllerDelegateProtocol {
override fun imagePickerController(
picker: UIImagePickerController,
didFinishPickingImage: UIImage,
editingInfo: Map<Any?, *>?
) {
Napier.d(tag = "IMAGE_PICKER") { "imagePickerController" }
picker.dismissViewControllerAnimated(true, null)
val imageBytes = UIImagePNGRepresentation(didFinishPickingImage)?.toByteArray()
imageBytes?.run(onImagePicked)
}
override fun imagePickerControllerDidCancel(picker: UIImagePickerController) {
Napier.d(tag = "IMAGE_PICKER") { "imagePickerControllerDidCancel" }
picker.dismissViewControllerAnimated(true, null)
onPickerDismissed()
}
}
Affected platforms
Select one of the platforms below:
iOS
Versions
Kotlin version*: 1.9.20
Compose Multiplatform version*: 1.5.10
OS version(s)* (required for Desktop and iOS issues): iOS 15
OS architecture (x86 or arm64): arm64
To Reproduce
Expected behavior
Since I'm using remember I'm expecting to retrieve a same instance of the router.
Screenshots
Screen.Recording.2023-12-10.at.1.54.28.PM.mov
Additional context
Obviously, it works fine if I save the NavigationState globally, but should I do that ? From my understanding, it's something that remember is responsible, doesn't it ?
The text was updated successfully, but these errors were encountered:
Describe the bug
When the user opens a camera through the
UIImagePickerController
and then cancels or makes a photo the application "reloads" or losses its state. Not sure whether it's a bug of Compose or not, I'm using a following function to obtain an instance of my router:So, from my understanding it should not be reinstantiated.
From the logs I see that
ComposeUIViewController
invokes children composables one the application become active after the camera and with the function above myNavigationState
get reinstantiated and I don't know why.Implementation to open the Camera & Gallery
Affected platforms
Select one of the platforms below:
Versions
To Reproduce
Expected behavior
Since I'm using
remember
I'm expecting to retrieve a same instance of the router.Screenshots
Screen.Recording.2023-12-10.at.1.54.28.PM.mov
Additional context
Obviously, it works fine if I save the
NavigationState
globally, but should I do that ? From my understanding, it's something thatremember
is responsible, doesn't it ?The text was updated successfully, but these errors were encountered: