forked from icerockdev/moko-media
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
icerockdev#34 Add base PickerDelegate class
- Loading branch information
Alexey Nesterov
committed
May 7, 2024
1 parent
3a81ec6
commit 6ae0752
Showing
7 changed files
with
272 additions
and
259 deletions.
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
media/src/androidMain/kotlin/dev/icerock/moko/media/picker/CameraPickerDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* 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 androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.ActivityResultRegistry | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
|
||
internal class CameraPickerDelegate : | ||
ImagePickerDelegate<CameraPickerDelegate.CallbackData, Uri>() { | ||
|
||
override fun registerActivityResult( | ||
context: Context, | ||
activityResultRegistry: ActivityResultRegistry | ||
): ActivityResultLauncher<Uri> = activityResultRegistry.register( | ||
PICK_CAMERA_IMAGE_KEY, | ||
ActivityResultContracts.TakePicture(), | ||
) { result -> | ||
val callbackData = callback ?: return@register | ||
callback = null | ||
|
||
if (!result) { | ||
callbackData.callback.invoke(Result.failure(CanceledException())) | ||
return@register | ||
} | ||
|
||
processResult( | ||
context = context, | ||
callback = callbackData.callback, | ||
uri = callbackData.outputUri, | ||
maxImageWidth = callbackData.maxWidth, | ||
maxImageHeight = callbackData.maxHeight, | ||
) | ||
} | ||
|
||
fun pick( | ||
maxWidth: Int, | ||
maxHeight: Int, | ||
callback: (Result<android.graphics.Bitmap>) -> Unit, | ||
outputUri: Uri, | ||
) { | ||
this.callback?.let { | ||
it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) | ||
this.callback = null | ||
} | ||
this.callback = CallbackData( | ||
callback, | ||
outputUri, | ||
maxWidth, | ||
maxHeight, | ||
) | ||
|
||
pickerLauncherHolder.value?.launch( | ||
outputUri | ||
) | ||
} | ||
|
||
class CallbackData( | ||
val callback: (Result<android.graphics.Bitmap>) -> Unit, | ||
val outputUri: Uri, | ||
val maxWidth: Int, | ||
val maxHeight: Int, | ||
) | ||
|
||
companion object { | ||
private const val PICK_CAMERA_IMAGE_KEY = "PickCameraImageKey" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
media/src/androidMain/kotlin/dev/icerock/moko/media/picker/GalleryPickerDelegate.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* 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 androidx.activity.result.ActivityResultLauncher | ||
import androidx.activity.result.ActivityResultRegistry | ||
import androidx.activity.result.PickVisualMediaRequest | ||
import androidx.activity.result.contract.ActivityResultContracts | ||
|
||
internal class GalleryPickerDelegate : | ||
ImagePickerDelegate<GalleryPickerDelegate.CallbackData, PickVisualMediaRequest>() { | ||
|
||
override fun registerActivityResult( | ||
context: Context, | ||
activityResultRegistry: ActivityResultRegistry | ||
): ActivityResultLauncher<PickVisualMediaRequest> = activityResultRegistry.register( | ||
PICK_GALLERY_IMAGE_KEY, | ||
ActivityResultContracts.PickVisualMedia(), | ||
) { uri -> | ||
val callbackData = callback ?: return@register | ||
callback = null | ||
|
||
if (uri == null) { | ||
callbackData.callback.invoke(Result.failure(CanceledException())) | ||
return@register | ||
} | ||
|
||
processResult( | ||
context = context, | ||
callback = callbackData.callback, | ||
uri = uri, | ||
maxImageWidth = callbackData.maxWidth, | ||
maxImageHeight = callbackData.maxHeight, | ||
) | ||
} | ||
|
||
fun pick( | ||
maxWidth: Int, | ||
maxHeight: Int, | ||
callback: (Result<android.graphics.Bitmap>) -> Unit, | ||
) { | ||
this.callback?.let { | ||
it.callback.invoke(Result.failure(IllegalStateException("Callback should be null"))) | ||
this.callback = null | ||
} | ||
|
||
this.callback = CallbackData( | ||
callback, | ||
maxWidth, | ||
maxHeight, | ||
) | ||
|
||
pickerLauncherHolder.value?.launch( | ||
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly) | ||
) | ||
} | ||
|
||
class CallbackData( | ||
val callback: (Result<android.graphics.Bitmap>) -> Unit, | ||
val maxWidth: Int, | ||
val maxHeight: Int, | ||
) | ||
|
||
companion object { | ||
private const val PICK_GALLERY_IMAGE_KEY = "PickGalleryImageKey" | ||
} | ||
} |
Oops, something went wrong.