Skip to content

Commit

Permalink
Merge pull request #49 from Dorofeev/#48-picker-close-handle
Browse files Browse the repository at this point in the history
#48 added handling of close picker screen
  • Loading branch information
Alex009 committed Apr 22, 2021
2 parents 6ffc870 + 3876bd0 commit 46bbdfe
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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<Media> { continuation ->
val localDelegatePtr = ImagePickerDelegateToContinuation(continuation)
delegatePtr = localDelegatePtr
val localPresentationDelegatePtr = AdaptivePresentationDelegateToContinuation(continuation)
presentationDelegate = localPresentationDelegatePtr

val controller = UIImagePickerController()
controller.sourceType = source.toSourceType()
Expand All @@ -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<FileMedia> { continuation ->
val localDelegatePtr = DocumentPickerDelegateToContinuation(continuation)
delegatePtr = localDelegatePtr
val localPresentationDelegatePtr = AdaptivePresentationDelegateToContinuation(continuation)
presentationDelegate = localPresentationDelegatePtr

val controller = UIDocumentPickerViewController(
documentTypes = listOf(kStandardFileTypesId),
Expand All @@ -84,8 +93,10 @@ class MediaPickerController(
animated = true,
completion = null
)
controller.presentationController?.delegate = localPresentationDelegatePtr
}
delegatePtr = null
presentationDelegate = null
return fileMedia
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ internal actual class ImagePickerDelegateToContinuation actual constructor(
private val continuation: Continuation<Media>
) : NSObject(), UINavigationControllerDelegateProtocol, UIImagePickerControllerDelegateProtocol {

override fun imagePickerControllerDidCancel(picker: UIImagePickerController) {
picker.dismissModalViewControllerAnimated(true)
continuation.resumeWith(Result.failure(CanceledException()))
}

override fun imagePickerController(
picker: UIImagePickerController,
didFinishPickingMediaWithInfo: Map<Any?, *>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down

0 comments on commit 46bbdfe

Please sign in to comment.