Skip to content

Commit

Permalink
chore: add message item box click (WPB-4986) (#2433)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandreferris committed Nov 21, 2023
1 parent f9d466a commit d0db739
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 21 deletions.
Expand Up @@ -24,6 +24,7 @@ import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -107,6 +108,7 @@ fun MessageItem(
onFailedMessageRetryClicked: (String) -> Unit = {},
onFailedMessageCancelClicked: (String) -> Unit = {},
onLinkClick: (String) -> Unit = {},
isContentClickable: Boolean = false,
onMessageClick: (messageId: String) -> Unit = {},
defaultBackgroundColor: Color = Color.Transparent,
shouldDisplayMessageStatus: Boolean = true,
Expand Down Expand Up @@ -140,17 +142,26 @@ fun MessageItem(
Modifier.background(defaultBackgroundColor)
}

Box(backgroundColorModifier) {
Box(
backgroundColorModifier
.clickable(enabled = isContentClickable, onClick = {
onMessageClick(message.header.messageId)
})
) {
// padding needed to have same top padding for avatar and rest composables in message item
val fullAvatarOuterPadding = dimensions().avatarClickablePadding + dimensions().avatarStatusBorderSize
Row(
Modifier
.fillMaxWidth()
.combinedClickable(
enabled = message.isAvailable,
onClick = { onMessageClick(message.header.messageId) },
onLongClick = remember(message) { { onLongClicked(message) } }
)
.apply {
if (!isContentClickable) {
combinedClickable(
enabled = message.isAvailable,
onClick = { },
onLongClick = remember(message) { { onLongClicked(message) } }
)
}
}
.padding(
end = dimensions().messageItemHorizontalPadding,
top = dimensions().messageItemVerticalPadding - fullAvatarOuterPadding,
Expand All @@ -174,7 +185,7 @@ fun MessageItem(
// because avatar takes start padding we don't need to add padding to message item
UserProfileAvatar(
avatarData = message.userAvatarData,
clickable = avatarClickable
clickable = if (isContentClickable) null else avatarClickable
)
} else {
// imitating width of space that avatar takes
Expand Down Expand Up @@ -216,7 +227,7 @@ fun MessageItem(
}

val currentOnImageClick = remember(message) {
Clickable(enabled = isAvailable, onClick = {
Clickable(enabled = isAvailable && !isContentClickable, onClick = {
onImageMessageClicked(
message,
source == MessageSource.Self
Expand All @@ -225,7 +236,7 @@ fun MessageItem(
onLongClicked(message)
})
}
val onLongClick: (() -> Unit)? = remember(message) {
val onLongClick: (() -> Unit)? = if (isContentClickable) null else remember(message) {
if (isAvailable) {
{ onLongClicked(message) }
} else {
Expand All @@ -245,7 +256,8 @@ fun MessageItem(
onImageClick = currentOnImageClick,
onLongClick = onLongClick,
onOpenProfile = onOpenProfile,
onLinkClick = onLinkClick
onLinkClick = onLinkClick,
clickable = !isContentClickable
)
}
if (isMyMessage && shouldDisplayMessageStatus) {
Expand Down Expand Up @@ -473,7 +485,8 @@ private fun MessageContent(
onChangeAudioPosition: (String, Int) -> Unit,
onLongClick: (() -> Unit)? = null,
onOpenProfile: (String) -> Unit,
onLinkClick: (String) -> Unit
onLinkClick: (String) -> Unit,
clickable: Boolean
) {
when (messageContent) {
is UIMessageContent.ImageMessage -> {
Expand Down Expand Up @@ -507,7 +520,8 @@ private fun MessageContent(
onOpenProfile = onOpenProfile,
buttonList = null,
messageId = message.header.messageId,
onLinkClick = onLinkClick
onLinkClick = onLinkClick,
clickable = clickable
)
PartialDeliveryInformation(messageContent.deliveryStatus)
}
Expand Down
Expand Up @@ -83,7 +83,8 @@ internal fun MessageBody(
onLongClick: (() -> Unit)? = null,
onOpenProfile: (String) -> Unit,
buttonList: List<MessageButton>?,
onLinkClick: (String) -> Unit
onLinkClick: (String) -> Unit,
clickable: Boolean = true
) {
val (displayMentions, text) = messageBody?.message?.let {
mapToDisplayMentions(it, LocalContext.current.resources)
Expand All @@ -107,7 +108,11 @@ internal fun MessageBody(
TablesExtension.create()
)
text?.also {
MarkdownDocument(Parser.builder().extensions(extensions).build().parse(it) as Document, nodeData)
MarkdownDocument(
Parser.builder().extensions(extensions).build().parse(it) as Document,
nodeData,
clickable
)
}
buttonList?.also {
MessageButtonsContent(
Expand Down
Expand Up @@ -55,6 +55,7 @@ fun SearchConversationMessagesResultsScreen(
defaultBackgroundColor = colorsScheme().backgroundVariant,
shouldDisplayMessageStatus = false,
shouldDisplayFooter = false,
isContentClickable = true,
onMessageClick = onMessageClick
)
}
Expand Down
Expand Up @@ -59,24 +59,36 @@ import kotlin.math.min
import org.commonmark.node.Text as nodeText

@Composable
fun MarkdownDocument(document: Document, nodeData: NodeData) {
MarkdownBlockChildren(document, nodeData)
fun MarkdownDocument(
document: Document,
nodeData: NodeData,
clickable: Boolean
) {
MarkdownBlockChildren(
document,
nodeData,
clickable
)
}

@Composable
fun MarkdownBlockChildren(parent: Node, nodeData: NodeData) {
fun MarkdownBlockChildren(
parent: Node,
nodeData: NodeData,
clickable: Boolean = true
) {
var child = parent.firstChild

var updateMentions = nodeData.mentions

while (child != null) {
val updatedNodeData = nodeData.copy(mentions = updateMentions)
when (child) {
is Document -> MarkdownDocument(child, updatedNodeData)
is Document -> MarkdownDocument(child, updatedNodeData, clickable)
is BlockQuote -> MarkdownBlockQuote(child, updatedNodeData)
is ThematicBreak -> MarkdownThematicBreak()
is Heading -> MarkdownHeading(child, updatedNodeData)
is Paragraph -> MarkdownParagraph(child, updatedNodeData) {
is Paragraph -> MarkdownParagraph(child, updatedNodeData, clickable) {
updateMentions = it
}

Expand Down
Expand Up @@ -29,7 +29,12 @@ import org.commonmark.node.Document
import org.commonmark.node.Paragraph

@Composable
fun MarkdownParagraph(paragraph: Paragraph, nodeData: NodeData, onMentionsUpdate: (List<DisplayMention>) -> Unit) {
fun MarkdownParagraph(
paragraph: Paragraph,
nodeData: NodeData,
clickable: Boolean,
onMentionsUpdate: (List<DisplayMention>) -> Unit
) {
val padding = if (paragraph.parent is Document) dimensions().spacing8x else dimensions().spacing0x
Box(modifier = Modifier.padding(bottom = padding)) {
val annotatedString = buildAnnotatedString {
Expand All @@ -43,7 +48,8 @@ fun MarkdownParagraph(paragraph: Paragraph, nodeData: NodeData, onMentionsUpdate
style = nodeData.style,
onLongClick = nodeData.onLongClick,
onOpenProfile = nodeData.onOpenProfile,
onClickLink = nodeData.onLinkClick
onClickLink = nodeData.onLinkClick,
clickable = clickable
)
}
}

0 comments on commit d0db739

Please sign in to comment.