mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2026-08-10 00:16:59 +00:00
feat: add Share as Image (local file) and rename upload flow to Share as Image Url
Adds a second "Share as Image" entry to a note's 3-dot menu that renders the same framed card, captures it to a PNG and hands the local file straight to the Android share sheet — no preview, no upload. The existing upload-and-share-URL flow is renamed to "Share as Image Url". Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01JM7z36GaqCm612rjPnX2G9
This commit is contained in:
@@ -59,6 +59,7 @@ import com.vitorpamplona.amethyst.ui.navigation.routes.isSameRoute
|
||||
import com.vitorpamplona.amethyst.ui.note.PayViaIntentScreen
|
||||
import com.vitorpamplona.amethyst.ui.note.UpdateReactionTypeScreen
|
||||
import com.vitorpamplona.amethyst.ui.note.nip22Comments.ReplyCommentPostScreen
|
||||
import com.vitorpamplona.amethyst.ui.note.share.ShareNoteAsImageFileScreen
|
||||
import com.vitorpamplona.amethyst.ui.note.share.ShareNoteAsImageScreen
|
||||
import com.vitorpamplona.amethyst.ui.screen.AccountSessionManager
|
||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountSwitcherAndLeftDrawerLayout
|
||||
@@ -417,6 +418,7 @@ fun BuildNavigation(
|
||||
composableFromEndArgs<Route.Profile> { ProfileScreen(it.id, accountViewModel, nav) }
|
||||
composableFromEndArgs<Route.Note> { ThreadScreen(it.id, accountViewModel, nav) }
|
||||
composableFromEndArgs<Route.ShareNoteAsImage> { ShareNoteAsImageScreen(it.id, accountViewModel, nav) }
|
||||
composableFromEndArgs<Route.ShareNoteAsImageFile> { ShareNoteAsImageFileScreen(it.id, accountViewModel, nav) }
|
||||
composableFromEndArgs<Route.ContactListUsers> { ContactListUsersScreen(it.noteId, accountViewModel, nav) }
|
||||
composableFromEndArgs<Route.Hashtag> { HashtagScreen(it, accountViewModel, nav) }
|
||||
composableFromEndArgs<Route.Geohash> { GeoHashScreen(it, accountViewModel, nav) }
|
||||
|
||||
@@ -436,6 +436,10 @@ sealed class Route {
|
||||
val id: String,
|
||||
) : Route()
|
||||
|
||||
@Serializable data class ShareNoteAsImageFile(
|
||||
val id: String,
|
||||
) : Route()
|
||||
|
||||
@Serializable data class ContactListUsers(
|
||||
val noteId: String,
|
||||
) : Route()
|
||||
|
||||
@@ -240,6 +240,11 @@ fun NoteDropDownMenu(
|
||||
onDismiss()
|
||||
}
|
||||
M3ActionRow(icon = MaterialSymbols.Image, text = stringRes(R.string.share_as_image)) {
|
||||
val shareId = if (note is AddressableNote) note.address.toValue() else note.idHex
|
||||
nav.nav(Route.ShareNoteAsImageFile(shareId))
|
||||
onDismiss()
|
||||
}
|
||||
M3ActionRow(icon = MaterialSymbols.Image, text = stringRes(R.string.share_as_image_url)) {
|
||||
val shareId = if (note is AddressableNote) note.address.toValue() else note.idHex
|
||||
nav.nav(Route.ShareNoteAsImage(shareId))
|
||||
onDismiss()
|
||||
|
||||
+99
-1
@@ -120,6 +120,89 @@ fun ShareNoteAsImageScreen(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders [id]'s note into the same framed card as [ShareNoteAsImageScreen], captures it to a
|
||||
* bitmap, writes it to the cache as a PNG and hands the local file straight to the Android share
|
||||
* sheet — no preview, no upload. The screen only shows a brief progress indicator while the card
|
||||
* is being captured.
|
||||
*/
|
||||
@Composable
|
||||
fun ShareNoteAsImageFileScreen(
|
||||
id: String,
|
||||
accountViewModel: AccountViewModel,
|
||||
nav: INav,
|
||||
) {
|
||||
LoadNote(id, accountViewModel) { note ->
|
||||
if (note != null) {
|
||||
ShareNoteAsImageFileScreen(note, accountViewModel, nav)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ShareNoteAsImageFileScreen(
|
||||
note: Note,
|
||||
accountViewModel: AccountViewModel,
|
||||
nav: INav,
|
||||
) {
|
||||
val context = LocalContext.current
|
||||
val graphicsLayer = rememberGraphicsLayer()
|
||||
|
||||
// Guards against capturing/sharing more than once across recompositions.
|
||||
var shared by remember { mutableStateOf(false) }
|
||||
|
||||
Scaffold(
|
||||
topBar = { TopBarWithBackButton(stringRes(R.string.share_as_image), nav) },
|
||||
) { pad ->
|
||||
Box(
|
||||
modifier =
|
||||
Modifier
|
||||
.padding(pad)
|
||||
.fillMaxSize()
|
||||
.background(
|
||||
Brush.verticalGradient(
|
||||
listOf(
|
||||
MaterialTheme.colorScheme.surface,
|
||||
MaterialTheme.colorScheme.surfaceVariant,
|
||||
),
|
||||
),
|
||||
),
|
||||
contentAlignment = Alignment.Center,
|
||||
) {
|
||||
// Rendered (but unpainted) only to feed the snapshot; the user never sees this card.
|
||||
CaptureSource(
|
||||
note = note,
|
||||
graphicsLayer = graphicsLayer,
|
||||
accountViewModel = accountViewModel,
|
||||
nav = nav,
|
||||
)
|
||||
|
||||
GeneratingPreview()
|
||||
|
||||
LaunchedEffect(note) {
|
||||
// Wait a couple of frames so the card is measured and drawn, then let network
|
||||
// media settle before the final snapshot.
|
||||
withFrameNanos {}
|
||||
withFrameNanos {}
|
||||
graphicsLayer.toImageBitmap()
|
||||
delay(IMAGE_SETTLE_MS)
|
||||
val bitmap = graphicsLayer.toImageBitmap()
|
||||
|
||||
if (!shared) {
|
||||
shared = true
|
||||
val uri =
|
||||
withContext(Dispatchers.IO) {
|
||||
saveBitmapToCache(context, bitmap.asAndroidBitmap())
|
||||
}
|
||||
startShareImageFileIntent(context, uri)
|
||||
nav.popBack()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalMaterial3Api::class)
|
||||
@Composable
|
||||
fun ShareNoteAsImageScreen(
|
||||
@@ -208,7 +291,7 @@ fun ShareNoteAsImageScreen(
|
||||
}
|
||||
|
||||
Scaffold(
|
||||
topBar = { TopBarWithBackButton(stringRes(R.string.share_as_image), nav) },
|
||||
topBar = { TopBarWithBackButton(stringRes(R.string.share_as_image_url), nav) },
|
||||
bottomBar = {
|
||||
ShareBottomBar(
|
||||
serverName = selectedServer.name,
|
||||
@@ -451,6 +534,21 @@ private fun startShareUrlIntent(
|
||||
type = "text/plain"
|
||||
putExtra(Intent.EXTRA_TEXT, url)
|
||||
}
|
||||
val shareIntent = Intent.createChooser(sendIntent, stringRes(context, R.string.share_as_image_url))
|
||||
context.startActivity(shareIntent)
|
||||
}
|
||||
|
||||
private fun startShareImageFileIntent(
|
||||
context: Context,
|
||||
uri: Uri,
|
||||
) {
|
||||
val sendIntent =
|
||||
Intent().apply {
|
||||
action = Intent.ACTION_SEND
|
||||
type = PNG_MIME
|
||||
putExtra(Intent.EXTRA_STREAM, uri)
|
||||
addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
|
||||
}
|
||||
val shareIntent = Intent.createChooser(sendIntent, stringRes(context, R.string.share_as_image))
|
||||
context.startActivity(shareIntent)
|
||||
}
|
||||
|
||||
@@ -474,6 +474,7 @@
|
||||
<string name="quick_action_share_browser_link">Share Browser Link</string>
|
||||
<string name="quick_action_share">Share</string>
|
||||
<string name="share_as_image">Share as Image</string>
|
||||
<string name="share_as_image_url">Share as Image Url</string>
|
||||
<string name="share_as_image_generating">Generating preview…</string>
|
||||
<string name="share_as_image_watermark">Shared via Amethyst</string>
|
||||
<string name="quick_action_copy_user_id">Author ID</string>
|
||||
|
||||
Reference in New Issue
Block a user