diff --git a/README.md b/README.md index 52545f3..f86d81b 100755 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ TODO - 0.7.0 - 0.7.1 - 0.8.0 + - 0.8.1 ## Installation root build.gradle @@ -60,7 +61,7 @@ allprojects { project build.gradle ```groovy dependencies { - commonMainApi("dev.icerock.moko:media:0.8.0") + commonMainApi("dev.icerock.moko:media:0.8.1") } ``` diff --git a/buildSrc/src/main/kotlin/Deps.kt b/buildSrc/src/main/kotlin/Deps.kt index f4fd427..c123aa0 100755 --- a/buildSrc/src/main/kotlin/Deps.kt +++ b/buildSrc/src/main/kotlin/Deps.kt @@ -16,7 +16,7 @@ object Deps { private const val mokoMvvmVersion = "0.10.0" private const val mokoPermissionsVersion = "0.9.0" private const val mokoTestVersion = "0.2.1" - const val mokoMediaVersion = "0.8.0" + const val mokoMediaVersion = "0.8.1" object Android { const val compileSdk = 28 diff --git a/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/AdaptivePresentationDelegateToContinuation.kt b/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/AdaptivePresentationDelegateToContinuation.kt new file mode 100644 index 0000000..d6b6843 --- /dev/null +++ b/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/AdaptivePresentationDelegateToContinuation.kt @@ -0,0 +1,13 @@ +/* + * Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.media.picker + +import platform.UIKit.UIAdaptivePresentationControllerDelegateProtocol +import platform.darwin.NSObject +import kotlin.coroutines.Continuation + +internal expect class AdaptivePresentationDelegateToContinuation( + continuation: Continuation<*> +) : NSObject, UIAdaptivePresentationControllerDelegateProtocol diff --git a/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/ios/MediaPickerController.kt b/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/ios/MediaPickerController.kt index c8cfe32..ee52432 100644 --- a/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/ios/MediaPickerController.kt +++ b/media/src/iosMain/kotlin/dev/icerock/moko/media/picker/ios/MediaPickerController.kt @@ -7,6 +7,7 @@ package dev.icerock.moko.media.picker.ios import dev.icerock.moko.media.Bitmap import dev.icerock.moko.media.FileMedia import dev.icerock.moko.media.Media +import dev.icerock.moko.media.picker.AdaptivePresentationDelegateToContinuation import dev.icerock.moko.media.picker.DEFAULT_MAX_IMAGE_HEIGHT import dev.icerock.moko.media.picker.DEFAULT_MAX_IMAGE_WIDTH import dev.icerock.moko.media.picker.DocumentPickerDelegateToContinuation @@ -25,6 +26,7 @@ import platform.UIKit.UIViewController import kotlin.coroutines.suspendCoroutine import platform.CoreServices.kUTTypeData import platform.UIKit.UIDocumentPickerMode +import platform.UIKit.presentationController class MediaPickerController( override val permissionsController: PermissionsController, @@ -49,9 +51,12 @@ class MediaPickerController( @Suppress("ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE") var delegatePtr: ImagePickerDelegateToContinuation? // strong reference to delegate (view controller have weak ref) + var presentationDelegate: AdaptivePresentationDelegateToContinuation? val media = suspendCoroutine { continuation -> val localDelegatePtr = ImagePickerDelegateToContinuation(continuation) delegatePtr = localDelegatePtr + val localPresentationDelegatePtr = AdaptivePresentationDelegateToContinuation(continuation) + presentationDelegate = localPresentationDelegatePtr val controller = UIImagePickerController() controller.sourceType = source.toSourceType() @@ -62,17 +67,21 @@ class MediaPickerController( animated = true, completion = null ) + controller.presentationController?.delegate = localPresentationDelegatePtr } delegatePtr = null - + presentationDelegate = null return media.preview } override suspend fun pickFiles(): FileMedia { var delegatePtr: DocumentPickerDelegateToContinuation? // strong reference to delegate (view controller have weak ref) + var presentationDelegate: AdaptivePresentationDelegateToContinuation? val fileMedia = suspendCoroutine { continuation -> val localDelegatePtr = DocumentPickerDelegateToContinuation(continuation) delegatePtr = localDelegatePtr + val localPresentationDelegatePtr = AdaptivePresentationDelegateToContinuation(continuation) + presentationDelegate = localPresentationDelegatePtr val controller = UIDocumentPickerViewController( documentTypes = listOf(kStandardFileTypesId), @@ -84,8 +93,10 @@ class MediaPickerController( animated = true, completion = null ) + controller.presentationController?.delegate = localPresentationDelegatePtr } delegatePtr = null + presentationDelegate = null return fileMedia } diff --git a/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/AdaptivePresentationDelegateToContinuation.kt b/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/AdaptivePresentationDelegateToContinuation.kt new file mode 100644 index 0000000..02ddce9 --- /dev/null +++ b/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/AdaptivePresentationDelegateToContinuation.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2021 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + +package dev.icerock.moko.media.picker + +import platform.UIKit.UIAdaptivePresentationControllerDelegateProtocol +import platform.UIKit.UIPresentationController +import platform.darwin.NSObject +import kotlin.coroutines.Continuation + +internal actual class AdaptivePresentationDelegateToContinuation actual constructor( + private val continuation: Continuation<*> +) : NSObject(), UIAdaptivePresentationControllerDelegateProtocol { + override fun presentationControllerDidDismiss(presentationController: UIPresentationController) { + continuation.resumeWith(Result.failure(CanceledException())) + } +} \ No newline at end of file diff --git a/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegateToContinuation.kt b/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegateToContinuation.kt index 48698c6..c790838 100644 --- a/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegateToContinuation.kt +++ b/media/src/iosX64Main/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegateToContinuation.kt @@ -30,6 +30,11 @@ internal actual class ImagePickerDelegateToContinuation actual constructor( private val continuation: Continuation ) : NSObject(), UINavigationControllerDelegateProtocol, UIImagePickerControllerDelegateProtocol { + override fun imagePickerControllerDidCancel(picker: UIImagePickerController) { + picker.dismissModalViewControllerAnimated(true) + continuation.resumeWith(Result.failure(CanceledException())) + } + override fun imagePickerController( picker: UIImagePickerController, didFinishPickingMediaWithInfo: Map diff --git a/sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/ImageSelectionViewModel.kt b/sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/ImageSelectionViewModel.kt index 9533c1d..51fc893 100755 --- a/sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/ImageSelectionViewModel.kt +++ b/sample/mpp-library/src/commonMain/kotlin/com/icerockdev/library/ImageSelectionViewModel.kt @@ -32,7 +32,7 @@ class ImageSelectionViewModel( try { val file = mediaPickerController.pickFiles() _textState.value = file.name - } catch(canceled: CanceledException) { + } catch (canceled: CanceledException) { _textState.value = "canceled" } catch (exc: Exception) { _textState.value = exc.toString() @@ -46,6 +46,8 @@ class ImageSelectionViewModel( val image = mediaPickerController.pickImage(source) _selectedImage.value = image _textState.value = "image selected" + } catch (canceled: CanceledException) { + _textState.value = "canceled" } catch (exc: Exception) { exc.printStackTrace() _selectedImage.value = null