Possibility to remove mentions from replies.

This commit is contained in:
Vitor Pamplona 2023-01-17 11:16:50 -05:00
parent 0789752112
commit d0b6e96c2f
4 changed files with 46 additions and 27 deletions

View File

@ -90,13 +90,17 @@ class Account(val loggedIn: Persona, val followingChannels: MutableSet<String> =
} }
} }
fun sendPost(message: String, replyingTo: Note?) { fun sendPost(message: String, originalNote: Note?, modifiedMentions: List<User>?) {
if (!isWriteable()) return if (!isWriteable()) return
val replyToEvent = replyingTo?.event val replyToEvent = originalNote?.event
if (replyToEvent is TextNoteEvent) { if (replyToEvent is TextNoteEvent) {
val modifiedMentionsHex = modifiedMentions?.map { it.pubkeyHex }?.toSet() ?: emptySet()
val repliesTo = replyToEvent.replyTos.plus(replyToEvent.id.toHex()) 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( val signedEvent = TextNoteEvent.create(
msg = message, msg = message,

View File

@ -41,17 +41,17 @@ import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.components.UrlPreview import com.vitorpamplona.amethyst.ui.components.UrlPreview
import com.vitorpamplona.amethyst.ui.components.imageExtension import com.vitorpamplona.amethyst.ui.components.imageExtension
import com.vitorpamplona.amethyst.ui.navigation.UploadFromGallery import com.vitorpamplona.amethyst.ui.navigation.UploadFromGallery
import com.vitorpamplona.amethyst.ui.note.ReplyInformation
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import nostr.postr.events.TextNoteEvent import nostr.postr.events.TextNoteEvent
@OptIn(ExperimentalComposeUiApi::class) @OptIn(ExperimentalComposeUiApi::class)
@Composable @Composable
fun NewPostView(onClose: () -> Unit, replyingTo: Note? = null, account: Account) { fun NewPostView(onClose: () -> Unit, baseReplyTo: Note? = null, account: Account) {
val postViewModel: NewPostViewModel = viewModel<NewPostViewModel>().apply { val postViewModel: NewPostViewModel = viewModel()
this.replyingTo = replyingTo
this.account = account postViewModel.load(account, baseReplyTo)
}
val context = LocalContext.current val context = LocalContext.current
@ -103,18 +103,9 @@ fun NewPostView(onClose: () -> Unit, replyingTo: Note? = null, account: Account)
) )
} }
if (replyingTo != null && replyingTo.event is TextNoteEvent) { if (postViewModel.replyTos != null && baseReplyTo?.event is TextNoteEvent) {
Row( ReplyInformation(postViewModel.replyTos, postViewModel.mentions, "") {
verticalAlignment = Alignment.CenterVertically, postViewModel.removeFromReplyList(it)
) {
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)
)
} }
} }

View File

@ -11,18 +11,33 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModel
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.Note 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.isValidURL
import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator import com.vitorpamplona.amethyst.ui.components.noProtocolUrlValidator
class NewPostViewModel: ViewModel() { class NewPostViewModel: ViewModel() {
var account: Account? = null private var account: Account? = null
var replyingTo: Note? = null private var originalNote: Note? = null
var mentions by mutableStateOf<List<User>?>(null)
var replyTos by mutableStateOf<MutableList<Note>?>(null)
var message by mutableStateOf("") var message by mutableStateOf("")
var urlPreview by mutableStateOf<String?>(null) var urlPreview by mutableStateOf<String?>(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() { fun sendPost() {
account?.sendPost(message, replyingTo) account?.sendPost(message, originalNote, mentions)
message = "" message = ""
urlPreview = null urlPreview = null
} }
@ -54,4 +69,8 @@ class NewPostViewModel: ViewModel() {
} }
} }
} }
fun removeFromReplyList(it: User) {
mentions = mentions?.minus(it)
}
} }

View File

@ -24,6 +24,13 @@ import com.vitorpamplona.amethyst.model.User
@Composable @Composable
fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, navController: NavController) { fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, navController: NavController) {
ReplyInformation(replyTo, mentions) {
navController.navigate("User/${it.pubkeyHex}")
}
}
@Composable
fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, prefix: String = "", onUserTagClick: (User) -> Unit) {
FlowRow() { FlowRow() {
if (mentions != null && mentions.isNotEmpty()) { if (mentions != null && mentions.isNotEmpty()) {
if (replyTo != null && replyTo.isNotEmpty()) { if (replyTo != null && replyTo.isNotEmpty()) {
@ -39,11 +46,9 @@ fun ReplyInformation(replyTo: MutableList<Note>?, mentions: List<User>?, navCont
innerUser?.let { myUser -> innerUser?.let { myUser ->
ClickableText( ClickableText(
AnnotatedString("@${myUser.toBestDisplayName()}"), AnnotatedString("${prefix}@${myUser.toBestDisplayName()}"),
style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary.copy(alpha = 0.52f), fontSize = 13.sp), style = LocalTextStyle.current.copy(color = MaterialTheme.colors.primary.copy(alpha = 0.52f), fontSize = 13.sp),
onClick = { onClick = { onUserTagClick(myUser) }
navController.navigate("User/${myUser.pubkeyHex}")
}
) )
if (idx < mentions.size - 2) { if (idx < mentions.size - 2) {