Skip to content

Commit

Permalink
If the Share target intent opens with the new post screen already act…
Browse files Browse the repository at this point in the history
…ive, just updates the post instead of creating a new one.
  • Loading branch information
vitorpamplona committed Oct 17, 2024
1 parent 44d8b8f commit c0ba6f5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,10 @@ open class NewPostViewModel : ViewModel() {
draftTextChanges.trySend("")
}

open fun addToMessage(it: String) {
updateMessage(TextFieldValue(message.text + " " + it))
}

open fun updateMessage(it: TextFieldValue) {
message = it
urlPreview = findUrlInMessage()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ fun AppNavigation(
popEnterTransition = { scaleIn },
popExitTransition = { slideOutVerticallyToBottom },
) {
val draftMessage = it.message()
val draftMessage = it.arguments?.getString("message")?.ifBlank { null }
val attachment =
it.arguments?.getString("attachment")?.ifBlank { null }?.let {
Uri.parse(it)
Expand All @@ -304,6 +304,7 @@ fun AppNavigation(
val version = it.arguments?.getString("version")
val draft = it.arguments?.getString("draft")
val enableMessageInterface = it.arguments?.getBoolean("enableMessageInterface") ?: false

NewPostScreen(
message = draftMessage,
attachment = attachment,
Expand Down Expand Up @@ -333,9 +334,12 @@ private fun NavigateIfIntentRequested(
accountStateViewModel: AccountStateViewModel,
) {
val activity = LocalContext.current.getActivity()
var newAccount by remember { mutableStateOf<String?>(null) }

if (activity.intent.action == Intent.ACTION_SEND) {
// avoids restarting the new Post screen when the intent is for the screen.
// Microsoft's swift key sends Gifs as new actions
if (isBaseRoute(nav.controller, Route.NewPost.base)) return

// saves the intent to avoid processing again
var message by remember {
mutableStateOf(
Expand All @@ -356,6 +360,8 @@ private fun NavigateIfIntentRequested(
media = null
message = null
} else {
var newAccount by remember { mutableStateOf<String?>(null) }

var currentIntentNextPage by remember {
mutableStateOf(
activity.intent
Expand Down Expand Up @@ -404,12 +410,16 @@ private fun NavigateIfIntentRequested(
val consumer =
Consumer<Intent> { intent ->
if (intent.action == Intent.ACTION_SEND) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
nav.newStack(buildNewPostRoute(draftMessage = it))
}
// avoids restarting the new Post screen when the intent is for the screen.
// Microsoft's swift key sends Gifs as new actions
if (!isBaseRoute(nav.controller, Route.NewPost.base)) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.let {
nav.newStack(buildNewPostRoute(draftMessage = it))
}

(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
nav.newStack(buildNewPostRoute(attachment = it))
(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
nav.newStack(buildNewPostRoute(attachment = it))
}
}
} else {
val uri = intent.data?.toString()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ sealed class Route(
)
}

fun isBaseRoute(
navController: NavHostController,
startsWith: String,
): Boolean =
navController.currentBackStackEntry
?.destination
?.route
?.startsWith(startsWith) ?: false

fun getRouteWithArguments(navController: NavHostController): String? {
val currentEntry = navController.currentBackStackEntry ?: return null
return getRouteWithArguments(currentEntry.destination, currentEntry.arguments)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
package com.vitorpamplona.amethyst.ui.screen.loggedIn

import android.Manifest
import android.content.Intent
import android.graphics.Bitmap
import android.net.Uri
import android.os.Build
import android.os.Parcelable
import android.util.Log
import android.util.Size
import android.widget.Toast
Expand Down Expand Up @@ -119,6 +121,7 @@ import androidx.compose.ui.text.style.TextDirection
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.util.Consumer
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import coil.compose.AsyncImage
Expand Down Expand Up @@ -146,7 +149,8 @@ import com.vitorpamplona.amethyst.ui.components.InvoiceRequest
import com.vitorpamplona.amethyst.ui.components.LoadUrlPreview
import com.vitorpamplona.amethyst.ui.components.VideoView
import com.vitorpamplona.amethyst.ui.components.ZapRaiserRequest
import com.vitorpamplona.amethyst.ui.navigation.INav
import com.vitorpamplona.amethyst.ui.navigation.Nav
import com.vitorpamplona.amethyst.ui.navigation.getActivity
import com.vitorpamplona.amethyst.ui.note.BaseUserPicture
import com.vitorpamplona.amethyst.ui.note.CancelIcon
import com.vitorpamplona.amethyst.ui.note.CloseIcon
Expand Down Expand Up @@ -203,12 +207,13 @@ fun NewPostScreen(
draft: Note? = null,
enableMessageInterface: Boolean = false,
accountViewModel: AccountViewModel,
nav: INav,
nav: Nav,
) {
val postViewModel: NewPostViewModel = viewModel()
postViewModel.wantsDirectMessage = enableMessageInterface

val context = LocalContext.current
val activity = context.getActivity()

val scrollState = rememberScrollState()
val scope = rememberCoroutineScope()
Expand Down Expand Up @@ -250,6 +255,27 @@ fun NewPostScreen(
NostrSearchEventOrUserDataSource.stop()
}
}

DisposableEffect(nav, activity) {
// Microsoft's swift key sends Gifs as new actions

val consumer =
Consumer<Intent> { intent ->
if (intent.action == Intent.ACTION_SEND) {
intent.getStringExtra(Intent.EXTRA_TEXT)?.ifBlank { null }?.let {
postViewModel.addToMessage(it)
}

(intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {
postViewModel.selectImage(it)
}
}
}

activity.addOnNewIntentListener(consumer)
onDispose { activity.removeOnNewIntentListener(consumer) }
}

Scaffold(
topBar = {
TopAppBar(
Expand Down

0 comments on commit c0ba6f5

Please sign in to comment.