From d0b6e96c2f67e6b65c4ca4b9edfeb2cb54f0ca71 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 17 Jan 2023 11:16:50 -0500 Subject: [PATCH] Possibility to remove mentions from replies. --- .../vitorpamplona/amethyst/model/Account.kt | 10 +++++--- .../amethyst/ui/actions/NewPostView.kt | 25 ++++++------------- .../amethyst/ui/actions/NewPostViewModel.kt | 25 ++++++++++++++++--- .../amethyst/ui/note/ReplyInformation.kt | 13 +++++++--- 4 files changed, 46 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt index cffc7aed0a..bfede65c89 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Account.kt @@ -90,13 +90,17 @@ class Account(val loggedIn: Persona, val followingChannels: MutableSet = } } - fun sendPost(message: String, replyingTo: Note?) { + fun sendPost(message: String, originalNote: Note?, modifiedMentions: List?) { if (!isWriteable()) return - val replyToEvent = replyingTo?.event + val replyToEvent = originalNote?.event if (replyToEvent is TextNoteEvent) { + val modifiedMentionsHex = modifiedMentions?.map { it.pubkeyHex }?.toSet() ?: emptySet() + val repliesTo = replyToEvent.replyTos.plus(replyToEvent.id.toHex()) - val mentions = replyToEvent.mentions.plus(replyToEvent.pubKey.toHex()) + val mentions = replyToEvent.mentions.plus(replyToEvent.pubKey.toHex()).filter { + it in modifiedMentionsHex + } val signedEvent = TextNoteEvent.create( msg = message, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt index f4816df5a6..f5d661b7d5 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostView.kt @@ -41,17 +41,17 @@ import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.ui.components.UrlPreview import com.vitorpamplona.amethyst.ui.components.imageExtension import com.vitorpamplona.amethyst.ui.navigation.UploadFromGallery +import com.vitorpamplona.amethyst.ui.note.ReplyInformation import kotlinx.coroutines.delay import nostr.postr.events.TextNoteEvent @OptIn(ExperimentalComposeUiApi::class) @Composable -fun NewPostView(onClose: () -> Unit, replyingTo: Note? = null, account: Account) { - val postViewModel: NewPostViewModel = viewModel().apply { - this.replyingTo = replyingTo - this.account = account - } +fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, account: Account) { + val postViewModel: NewPostViewModel = viewModel() + + postViewModel.load(account, baseReplyTo) val context = LocalContext.current @@ -103,18 +103,9 @@ fun NewPostView(onClose: () -> Unit, replyingTo: Note? = null, account: Account) ) } - if (replyingTo != null && replyingTo.event is TextNoteEvent) { - Row( - verticalAlignment = Alignment.CenterVertically, - ) { - val replyList = replyingTo.replyTo!!.plus(replyingTo).joinToString(", ", "", "", 2) { it.idDisplayHex } - val withList = replyingTo.mentions!!.plus(replyingTo.author!!).joinToString(", ", "", "", 2) { it.toBestDisplayName() } - - Text( - "in reply to ${replyList} with ${withList}", - fontSize = 13.sp, - color = MaterialTheme.colors.onSurface.copy(alpha = 0.32f) - ) + if (postViewModel.replyTos != null && baseReplyTo?.event is TextNoteEvent) { + ReplyInformation(postViewModel.replyTos, postViewModel.mentions, "✖ ") { + postViewModel.removeFromReplyList(it) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index 79b16afb73..dda4aa082a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -11,18 +11,33 @@ import androidx.compose.runtime.setValue import androidx.lifecycle.ViewModel import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Note +import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.ui.components.isValidURL import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator class NewPostViewModel: ViewModel() { - var account: Account? = null - var replyingTo: Note? = null + private var account: Account? = null + private var originalNote: Note? = null + + var mentions by mutableStateOf?>(null) + var replyTos by mutableStateOf?>(null) var message by mutableStateOf("") var urlPreview by mutableStateOf(null) + fun load(account: Account, replyingTo: Note?) { + originalNote = replyingTo + replyingTo?.let { replyNote -> + this.replyTos = (replyNote.replyTo ?: mutableListOf()).plus(replyNote).toMutableList() + replyNote.author?.let { replyUser -> + this.mentions = (replyNote.mentions ?: emptyList()).plus(replyUser) + } + } + this.account = account + } + fun sendPost() { - account?.sendPost(message, replyingTo) + account?.sendPost(message, originalNote, mentions) message = "" urlPreview = null } @@ -54,4 +69,8 @@ class NewPostViewModel: ViewModel() { } } } + + fun removeFromReplyList(it: User) { + mentions = mentions?.minus(it) + } } \ No newline at end of file diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReplyInformation.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReplyInformation.kt index 49f11f5245..8e7b3e48b4 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReplyInformation.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReplyInformation.kt @@ -24,6 +24,13 @@ import com.vitorpamplona.amethyst.model.User @Composable fun ReplyInformation(replyTo: MutableList?, mentions: List?, navController: NavController) { + ReplyInformation(replyTo, mentions) { + navController.navigate("User/${it.pubkeyHex}") + } +} + +@Composable +fun ReplyInformation(replyTo: MutableList?, mentions: List?, prefix: String = "", onUserTagClick: (User) -> Unit) { FlowRow() { if (mentions != null && mentions.isNotEmpty()) { if (replyTo != null && replyTo.isNotEmpty()) { @@ -39,11 +46,9 @@ fun ReplyInformation(replyTo: MutableList?, mentions: List?, navCont innerUser?.let { myUser -> ClickableText( - AnnotatedString("@${myUser.toBestDisplayName()}"), + AnnotatedString("${prefix}@${myUser.toBestDisplayName()}"), style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary.copy(alpha = 0.52f), fontSize = 13.sp), - onClick = { - navController.navigate("User/${myUser.pubkeyHex}") - } + onClick = { onUserTagClick(myUser) } ) if (idx < mentions.size - 2) {