moved sharing intent back to view

This commit is contained in:
David Kaspar
2025-05-12 20:56:16 +02:00
parent ce96ab873f
commit 35001c0b00
2 changed files with 32 additions and 28 deletions
@@ -21,26 +21,23 @@
package com.vitorpamplona.amethyst.ui.components package com.vitorpamplona.amethyst.ui.components
import android.content.Context import android.content.Context
import android.content.Intent
import android.net.Uri import android.net.Uri
import android.util.Log import android.util.Log
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.Amethyst
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File import java.io.File
import java.io.FileInputStream import java.io.FileInputStream
import java.io.IOException 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 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 { object ShareHelper {
fun shareImageFromUrl( fun getSharableUriFromUrl(
context: Context, context: Context,
imageUrl: String, imageUrl: String,
) { mimeType: String,
): Uri {
try { try {
// Safely get snapshot and file // Safely get snapshot and file
val snapshot = val snapshot =
@@ -55,8 +52,8 @@ object ShareHelper {
val fileExtension = getImageExtension(file) val fileExtension = getImageExtension(file)
val fileCopy = prepareSharableImageFile(context, file, fileExtension) val fileCopy = prepareSharableImageFile(context, file, fileExtension)
// Share the file // Return sharable uri
shareMediaFile(context, getSharableUri(context, fileCopy), "image/*") return getSharableUri(context, fileCopy)
} }
} catch (e: IOException) { } catch (e: IOException) {
Log.e("ShareHelper", "Error sharing image", e) Log.e("ShareHelper", "Error sharing image", e)
@@ -115,20 +112,4 @@ object ShareHelper {
"${context.packageName}.provider", "${context.packageName}.provider",
file, 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"))
}
}
} }
@@ -20,6 +20,9 @@
*/ */
package com.vitorpamplona.amethyst.ui.components package com.vitorpamplona.amethyst.ui.components
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log import android.util.Log
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.AnimatedVisibilityScope import androidx.compose.animation.AnimatedVisibilityScope
@@ -102,8 +105,10 @@ import com.vitorpamplona.quartz.nip94FileMetadata.tags.DimensionTag
import com.vitorpamplona.quartz.utils.sha256.sha256 import com.vitorpamplona.quartz.utils.sha256.sha256
import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import kotlin.time.Duration.Companion.seconds import kotlin.time.Duration.Companion.seconds
@@ -758,10 +763,11 @@ fun ShareImageAction(
videoUri?.let { videoUri?.let {
if (videoUri.isNotEmpty()) { if (videoUri.isNotEmpty()) {
DropdownMenuItem( DropdownMenuItem(
// TODO localise
text = { Text("Share image...") }, text = { Text("Share image...") },
onClick = { onClick = {
ShareHelper.shareImageFromUrl(context, videoUri) val uri = ShareHelper.getSharableUriFromUrl(context, videoUri, mimeType)
shareMediaFile(context, uri, mimeType)
onDismiss() 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? { private suspend fun verifyHash(content: MediaUrlContent): Boolean? {
if (content.hash == null) return null if (content.hash == null) return null