From d569f33a4487aabf055453e7429dcbf9edee5669 Mon Sep 17 00:00:00 2001 From: Alexey Nesterov Date: Tue, 14 May 2024 19:34:11 +0700 Subject: [PATCH] #34 add more common logic at PickerDelegate --- .../moko/media/picker/CameraPickerDelegate.kt | 30 ++++++++++++------- .../moko/media/picker/FilePickerDelegate.kt | 17 +++++------ .../media/picker/GalleryPickerDelegate.kt | 28 ++++++++++------- .../moko/media/picker/ImagePickerDelegate.kt | 6 +++- .../moko/media/picker/MediaPickerDelegate.kt | 16 +++++----- .../moko/media/picker/PickerDelegate.kt | 23 +++++++++++++- 6 files changed, 81 insertions(+), 39 deletions(-) diff --git a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/CameraPickerDelegate.kt b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/CameraPickerDelegate.kt index 8957a8b..3bf92b2 100644 --- a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/CameraPickerDelegate.kt +++ b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/CameraPickerDelegate.kt @@ -5,13 +5,14 @@ package dev.icerock.moko.media.picker import android.content.Context +import android.graphics.Bitmap import android.net.Uri import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultRegistry import androidx.activity.result.contract.ActivityResultContracts internal class CameraPickerDelegate : - ImagePickerDelegate() { + ImagePickerDelegate() { override fun registerActivityResult( context: Context, @@ -40,14 +41,12 @@ internal class CameraPickerDelegate : fun pick( maxWidth: Int, maxHeight: Int, - callback: (Result) -> Unit, + callback: (Result) -> Unit, outputUri: Uri, ) { - this.callback?.let { - it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) - this.callback = null - } - this.callback = CallbackData( + super.pick(callback) + + this.callback = CameraPickerCallbackData( callback, outputUri, maxWidth, @@ -59,12 +58,23 @@ internal class CameraPickerDelegate : ) } - class CallbackData( - val callback: (Result) -> Unit, + override fun createCallback( + callback: (Result) -> Unit, + ): CameraPickerCallbackData = CameraPickerCallbackData( + callback, + Uri.EMPTY, + 0, + 0, + ) + + override fun launchActivityResult() = Unit + + class CameraPickerCallbackData( + override val callback: (Result) -> Unit, val outputUri: Uri, val maxWidth: Int, val maxHeight: Int, - ) + ) : CallbackData() companion object { private const val PICK_CAMERA_IMAGE_KEY = "PickCameraImageKey" diff --git a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/FilePickerDelegate.kt b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/FilePickerDelegate.kt index 6a24f7e..e18e236 100644 --- a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/FilePickerDelegate.kt +++ b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/FilePickerDelegate.kt @@ -15,7 +15,7 @@ import dev.icerock.moko.media.FileMedia import java.io.File internal class FilePickerDelegate : - PickerDelegate>() { + PickerDelegate, FileMedia>() { override fun registerActivityResult( context: Context, @@ -60,14 +60,11 @@ internal class FilePickerDelegate : callback.invoke(result) } - fun pick(callback: (Result) -> Unit) { - this.callback?.let { - it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) - this.callback = null - } - - this.callback = CallbackData(callback) + override fun createCallback( + callback: (Result) -> Unit, + ): FilePickerCallbackData = FilePickerCallbackData(callback) + override fun launchActivityResult() { pickerLauncherHolder.value?.launch( arrayOf( "*/*", @@ -75,7 +72,9 @@ internal class FilePickerDelegate : ) } - class CallbackData(val callback: (Result) -> Unit) + class FilePickerCallbackData( + override val callback: (Result) -> Unit + ) : CallbackData() companion object { private const val PICK_FILE_KEY = "PickFileKey" diff --git a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/GalleryPickerDelegate.kt b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/GalleryPickerDelegate.kt index b11a6d4..3cc8823 100644 --- a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/GalleryPickerDelegate.kt +++ b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/GalleryPickerDelegate.kt @@ -5,13 +5,14 @@ package dev.icerock.moko.media.picker import android.content.Context +import android.graphics.Bitmap import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.ActivityResultRegistry import androidx.activity.result.PickVisualMediaRequest import androidx.activity.result.contract.ActivityResultContracts internal class GalleryPickerDelegate : - ImagePickerDelegate() { + ImagePickerDelegate() { override fun registerActivityResult( context: Context, @@ -40,14 +41,11 @@ internal class GalleryPickerDelegate : fun pick( maxWidth: Int, maxHeight: Int, - callback: (Result) -> Unit, + callback: (Result) -> Unit, ) { - this.callback?.let { - it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) - this.callback = null - } + super.pick(callback) - this.callback = CallbackData( + this.callback = GalleryPickerCallbackData( callback, maxWidth, maxHeight, @@ -58,11 +56,21 @@ internal class GalleryPickerDelegate : ) } - class CallbackData( - val callback: (Result) -> Unit, + override fun createCallback( + callback: (Result) -> Unit, + ): GalleryPickerCallbackData = GalleryPickerCallbackData( + callback, + 0, + 0, + ) + + override fun launchActivityResult() = Unit + + class GalleryPickerCallbackData( + override val callback: (Result) -> Unit, val maxWidth: Int, val maxHeight: Int, - ) + ) : CallbackData() companion object { private const val PICK_GALLERY_IMAGE_KEY = "PickGalleryImageKey" diff --git a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegate.kt b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegate.kt index a9bcc59..11af7d7 100644 --- a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegate.kt +++ b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/ImagePickerDelegate.kt @@ -1,10 +1,14 @@ +/* + * Copyright 2024 IceRock MAG Inc. Use of this source code is governed by the Apache 2.0 license. + */ + package dev.icerock.moko.media.picker import android.content.Context import android.net.Uri import dev.icerock.moko.media.BitmapUtils -internal abstract class ImagePickerDelegate : PickerDelegate() { +internal abstract class ImagePickerDelegate, I, D> : PickerDelegate() { @Suppress("ReturnCount") protected fun processResult( diff --git a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/MediaPickerDelegate.kt b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/MediaPickerDelegate.kt index 095a286..01bdae8 100644 --- a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/MediaPickerDelegate.kt +++ b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/MediaPickerDelegate.kt @@ -13,7 +13,7 @@ import dev.icerock.moko.media.Media import dev.icerock.moko.media.MediaFactory internal class MediaPickerDelegate : - PickerDelegate() { + PickerDelegate() { override fun registerActivityResult( context: Context, @@ -38,19 +38,19 @@ internal class MediaPickerDelegate : callback.invoke(result) } - fun pick(callback: (Result) -> Unit) { - this.callback?.let { - it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) - this.callback = null - } - this.callback = CallbackData(callback) + override fun createCallback( + callback: (Result) -> Unit, + ): MediaPickerCallbackData = MediaPickerCallbackData(callback) + override fun launchActivityResult() { pickerLauncherHolder.value?.launch( PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageAndVideo) ) } - class CallbackData(val callback: (Result) -> Unit) + class MediaPickerCallbackData( + override val callback: (Result) -> Unit, + ) : CallbackData() companion object { private const val PICK_MEDIA_KEY = "PickMediaKey" diff --git a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/PickerDelegate.kt b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/PickerDelegate.kt index 823b189..b4c8bdf 100644 --- a/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/PickerDelegate.kt +++ b/media/src/androidMain/kotlin/dev/icerock/moko/media/picker/PickerDelegate.kt @@ -18,7 +18,7 @@ import kotlinx.coroutines.flow.MutableStateFlow * @param type of callback * @param type of the input required to launch */ -internal abstract class PickerDelegate { +internal abstract class PickerDelegate, I, D> { protected var callback: C? = null @@ -49,4 +49,25 @@ internal abstract class PickerDelegate { context: Context, activityResultRegistry: ActivityResultRegistry, ): ActivityResultLauncher + + fun pick( + callback: (Result) -> Unit, + ) { + this.callback?.let { + it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) + this.callback = null + } + + this.callback = createCallback(callback) + + launchActivityResult() + } + + abstract fun createCallback(callback: (Result) -> Unit): C + + abstract fun launchActivityResult() + + abstract class CallbackData { + abstract val callback: (Result) -> Unit + } }