Skip to content

Commit

Permalink
Merge pull request #50 from icerockdev/develop
Browse files Browse the repository at this point in the history
Release 0.8.1
  • Loading branch information
Alex009 authored Apr 22, 2021
2 parents 7df0ff6 + c6ad040 commit 553da35
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ TODO
- 0.7.0
- 0.7.1
- 0.8.0
- 0.8.1

## Installation
root build.gradle
Expand All @@ -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")
}
```

Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
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 553da35

Please sign in to comment.