From 35001c0b002ffae5d4caaefe134dd72bd3b5d948 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Mon, 12 May 2025 20:56:16 +0200 Subject: [PATCH] moved sharing intent back to view --- .../amethyst/ui/components/ShareHelper.kt | 33 ++++--------------- .../ui/components/ZoomableContentView.kt | 27 +++++++++++++-- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt index e32bcd05c..b248959a5 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ShareHelper.kt @@ -21,26 +21,23 @@ package com.vitorpamplona.amethyst.ui.components import android.content.Context -import android.content.Intent import android.net.Uri import android.util.Log import androidx.core.content.FileProvider import com.vitorpamplona.amethyst.Amethyst -import kotlinx.coroutines.CoroutineScope -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import java.io.File import java.io.FileInputStream import java.io.IOException -// TODO use passed in context type rather than hard coding +// TODO use passed in mime type rather than hard coding // TODO Add unit tests for sharehelper -// TODO Move intent sharing back to ZoomableContentView for coroutine management? +// TODO Rely on passed in mime type for image type? object ShareHelper { - fun shareImageFromUrl( + fun getSharableUriFromUrl( context: Context, imageUrl: String, - ) { + mimeType: String, + ): Uri { try { // Safely get snapshot and file val snapshot = @@ -55,8 +52,8 @@ object ShareHelper { val fileExtension = getImageExtension(file) val fileCopy = prepareSharableImageFile(context, file, fileExtension) - // Share the file - shareMediaFile(context, getSharableUri(context, fileCopy), "image/*") + // Return sharable uri + return getSharableUri(context, fileCopy) } } catch (e: IOException) { Log.e("ShareHelper", "Error sharing image", e) @@ -115,20 +112,4 @@ object ShareHelper { "${context.packageName}.provider", file, ) - - private fun shareMediaFile( - context: Context, - uri: Uri, - mimeType: String = "image/*", - ) { - val shareIntent = - Intent(Intent.ACTION_SEND).apply { - type = mimeType - putExtra(Intent.EXTRA_STREAM, uri) - addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) - } - CoroutineScope(Dispatchers.Main).launch { - context.startActivity(Intent.createChooser(shareIntent, "Share Image")) - } - } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt index a933e38da..c9ec199f9 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/components/ZoomableContentView.kt @@ -20,6 +20,9 @@ */ package com.vitorpamplona.amethyst.ui.components +import android.content.Context +import android.content.Intent +import android.net.Uri import android.util.Log import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibilityScope @@ -102,8 +105,10 @@ import com.vitorpamplona.quartz.nip94FileMetadata.tags.DimensionTag import com.vitorpamplona.quartz.utils.sha256.sha256 import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.persistentListOf +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.delay +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import kotlin.time.Duration.Companion.seconds @@ -758,10 +763,11 @@ fun ShareImageAction( videoUri?.let { if (videoUri.isNotEmpty()) { DropdownMenuItem( + // TODO localise text = { Text("Share image...") }, onClick = { - ShareHelper.shareImageFromUrl(context, videoUri) - + val uri = ShareHelper.getSharableUriFromUrl(context, videoUri, mimeType) + shareMediaFile(context, uri, mimeType) onDismiss() }, ) @@ -772,6 +778,23 @@ fun ShareImageAction( } } +private fun shareMediaFile( + context: Context, + uri: Uri, + mimeType: String = "image/*", +) { + val shareIntent = + Intent(Intent.ACTION_SEND).apply { + type = mimeType + putExtra(Intent.EXTRA_STREAM, uri) + addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) + } + // TODO is this the right scope to avoid leaks? + CoroutineScope(Dispatchers.Main).launch { + context.startActivity(Intent.createChooser(shareIntent, "Share Image")) + } +} + private suspend fun verifyHash(content: MediaUrlContent): Boolean? { if (content.hash == null) return null