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
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"))
}
}
}
@@ -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