Platform independent image picking
This commit is contained in:
@@ -161,6 +161,10 @@ kotlin {
|
||||
// Google Sign-In
|
||||
implementation("com.google.android.gms:play-services-auth:21.4.0")
|
||||
implementation(libs.androidx.work.runtime.ktx)
|
||||
// rememberLauncherForActivityResult()
|
||||
implementation("androidx.activity:activity-compose:1.11.0")
|
||||
// PickVisualMediaRequest + ActivityResultContracts.*
|
||||
implementation("androidx.activity:activity:1.11.0")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
package app.klottr.images
|
||||
|
||||
import androidx.activity.compose.rememberLauncherForActivityResult
|
||||
import androidx.activity.result.PickVisualMediaRequest
|
||||
import androidx.activity.result.contract.ActivityResultContracts
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import app.klottr.net.ImageHandle
|
||||
|
||||
@Composable
|
||||
actual fun rememberVisualMediaPicker(): VisualMediaPicker {
|
||||
// Holds the in-flight callback across recompositions.
|
||||
val callbackState = remember { mutableStateOf<((ImageHandle?) -> Unit)?>(null) }
|
||||
|
||||
val launcher = rememberLauncherForActivityResult(
|
||||
contract = ActivityResultContracts.PickVisualMedia()
|
||||
) { uri ->
|
||||
val cb = callbackState.value
|
||||
callbackState.value = null
|
||||
cb?.invoke(uri?.let { ImageHandle.UriString(it.toString()) })
|
||||
}
|
||||
|
||||
return remember {
|
||||
object : VisualMediaPicker {
|
||||
override fun pickImage(onResult: (ImageHandle?) -> Unit) {
|
||||
// Prevent overlapping requests. Ignore if one is already active.
|
||||
if (callbackState.value != null) return
|
||||
callbackState.value = onResult
|
||||
launcher.launch(
|
||||
PickVisualMediaRequest(ActivityResultContracts.PickVisualMedia.ImageOnly)
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package app.klottr.images
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import app.klottr.net.ImageHandle
|
||||
|
||||
/**
|
||||
* Simple, callback-based image picker.
|
||||
* Actuals will present native UI and return an ImageHandle (e.g., UriString or Bytes).
|
||||
*/
|
||||
interface VisualMediaPicker {
|
||||
fun pickImage(onResult: (ImageHandle?) -> Unit)
|
||||
}
|
||||
|
||||
@Composable
|
||||
expect fun rememberVisualMediaPicker(): VisualMediaPicker
|
||||
@@ -0,0 +1,74 @@
|
||||
package app.klottr.images
|
||||
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.remember
|
||||
import app.klottr.net.ImageHandle
|
||||
import kotlinx.cinterop.memScoped
|
||||
import kotlinx.cinterop.refTo
|
||||
import platform.Foundation.NSData
|
||||
import platform.Foundation.create
|
||||
import platform.PhotosUI.PHPickerConfiguration
|
||||
import platform.PhotosUI.PHPickerFilter
|
||||
import platform.PhotosUI.PHPickerResult
|
||||
import platform.PhotosUI.PHPickerViewController
|
||||
import platform.PhotosUI.PHPickerViewControllerDelegateProtocol
|
||||
import platform.UIKit.UIApplication
|
||||
import platform.UIKit.UIViewController
|
||||
import platform.darwin.NSObject
|
||||
import kotlin.experimental.ExperimentalObjCName
|
||||
|
||||
private fun topViewController(): UIViewController? {
|
||||
// Simple best-effort presenter; adjust if you have an app-level root controller handy.
|
||||
val keyWindow = UIApplication.sharedApplication.keyWindow
|
||||
?: UIApplication.sharedApplication.connectedScenes.firstOrNull()
|
||||
?.valueForKey("delegate") as? NSObject // fallback is intentionally basic
|
||||
return (UIApplication.sharedApplication.keyWindow?.rootViewController)
|
||||
}
|
||||
|
||||
private class PickerDelegate(
|
||||
private val onDone: (ImageHandle?) -> Unit
|
||||
) : NSObject(), PHPickerViewControllerDelegateProtocol {
|
||||
|
||||
override fun picker(picker: PHPickerViewController, didFinishPicking: kotlin.collections.List<*>) {
|
||||
// Dismiss immediately; resolve first item (if any) asynchronously via itemProvider.
|
||||
picker.dismissViewControllerAnimated(true, completion = null)
|
||||
|
||||
val first = didFinishPicking.firstOrNull() as? PHPickerResult ?: return onDone(null)
|
||||
val provider = first.itemProvider
|
||||
when {
|
||||
provider?.hasItemConformingToTypeIdentifier("public.image") == true -> {
|
||||
provider.loadDataRepresentationForTypeIdentifier("public.image") { data, _ ->
|
||||
val bytes = (data as? NSData)?.let { ns ->
|
||||
ByteArray(ns.length.toInt()).also { dest ->
|
||||
memScoped {
|
||||
val buffer = dest.refTo(0)
|
||||
platform.Foundation.NSDataToByteArray(ns, dest)
|
||||
}
|
||||
}
|
||||
}
|
||||
onDone(bytes?.let { ImageHandle.Bytes(it) })
|
||||
}
|
||||
}
|
||||
else -> onDone(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
actual fun rememberVisualMediaPicker(): VisualMediaPicker {
|
||||
return remember {
|
||||
object : VisualMediaPicker {
|
||||
override fun pickImage(onResult: (ImageHandle?) -> Unit) {
|
||||
val cfg = PHPickerConfiguration().apply {
|
||||
filter = PHPickerFilter.imagesFilter()
|
||||
selectionLimit = 1
|
||||
}
|
||||
val controller = PHPickerViewController(configuration = cfg).apply {
|
||||
delegate = PickerDelegate(onResult)
|
||||
}
|
||||
topViewController()?.presentViewController(controller, true, completion = null)
|
||||
?: onResult(null)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user