Skip to content

Commit

Permalink
Add copy message chip to user message
Browse files Browse the repository at this point in the history
  • Loading branch information
Taewan-P committed Oct 18, 2024
1 parent de12878 commit b2f6bbc
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ import dev.jeziellago.compose.markdowntext.MarkdownText
@Composable
fun UserChatBubble(
modifier: Modifier = Modifier,
text: String
text: String,
onCopyClick: () -> Unit
) {
val cardColor = CardColors(
containerColor = MaterialTheme.colorScheme.primaryContainer,
Expand All @@ -45,17 +46,20 @@ fun UserChatBubble(
disabledContainerColor = MaterialTheme.colorScheme.onPrimaryContainer.copy(alpha = 0.38f)
)

Card(
modifier = modifier,
shape = RoundedCornerShape(32.dp),
colors = cardColor
) {
MarkdownText(
modifier = Modifier.padding(16.dp),
markdown = text,
isTextSelectable = true,
linkifyMask = Linkify.WEB_URLS
)
Column(horizontalAlignment = Alignment.End) {
Card(
modifier = modifier,
shape = RoundedCornerShape(32.dp),
colors = cardColor
) {
MarkdownText(
modifier = Modifier.padding(16.dp),
markdown = text,
isTextSelectable = true,
linkifyMask = Linkify.WEB_URLS
)
}
CopyTextChip(onCopyClick)
}
}

Expand Down Expand Up @@ -97,38 +101,48 @@ fun OpponentChatBubble(
if (!isLoading) {
Row {
if (!isError) {
AssistChip(
onClick = onCopyClick,
label = { Text(stringResource(R.string.copy_text)) },
leadingIcon = {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_copy),
contentDescription = stringResource(R.string.copy_text),
modifier = Modifier.size(AssistChipDefaults.IconSize)
)
}
)
CopyTextChip(onCopyClick)
}
Spacer(modifier = Modifier.width(8.dp))
if (canRetry) {
AssistChip(
onClick = onRetryClick,
label = { Text(stringResource(R.string.retry)) },
leadingIcon = {
Icon(
Icons.Rounded.Refresh,
contentDescription = stringResource(R.string.retry),
modifier = Modifier.size(AssistChipDefaults.IconSize)
)
}
)
Spacer(modifier = Modifier.width(8.dp))
RetryChip(onRetryClick)
}
}
}
}
}
}

@Composable
private fun CopyTextChip(onCopyClick: () -> Unit) {
AssistChip(
onClick = onCopyClick,
label = { Text(stringResource(R.string.copy_text)) },
leadingIcon = {
Icon(
imageVector = ImageVector.vectorResource(id = R.drawable.ic_copy),
contentDescription = stringResource(R.string.copy_text),
modifier = Modifier.size(AssistChipDefaults.IconSize)
)
}
)
}

@Composable
private fun RetryChip(onRetryClick: () -> Unit) {
AssistChip(
onClick = onRetryClick,
label = { Text(stringResource(R.string.retry)) },
leadingIcon = {
Icon(
Icons.Rounded.Refresh,
contentDescription = stringResource(R.string.retry),
modifier = Modifier.size(AssistChipDefaults.IconSize)
)
}
)
}

@Composable
private fun BrandText(apiType: ApiType) {
Box(
Expand All @@ -153,7 +167,7 @@ fun UserChatBubblePreview() {
in Python?
""".trimIndent()
GPTMobileTheme {
UserChatBubble(text = sampleText)
UserChatBubble(text = sampleText, onCopyClick = {})
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ fun ChatScreen(
Spacer(modifier = Modifier.weight(1f))
UserChatBubble(
modifier = Modifier.widthIn(max = maximumChatBubbleWidth),
text = groupedMessages[key]!![0].content
text = groupedMessages[key]!![0].content,
onCopyClick = { clipboardManager.setText(AnnotatedString(groupedMessages[key]!![0].content.trim())) }
)
}
}
Expand Down Expand Up @@ -208,7 +209,11 @@ fun ChatScreen(
.padding(horizontal = 16.dp, vertical = 12.dp)
) {
Spacer(modifier = Modifier.weight(1f))
UserChatBubble(modifier = Modifier.widthIn(max = maximumChatBubbleWidth), text = userMessage.content)
UserChatBubble(
modifier = Modifier.widthIn(max = maximumChatBubbleWidth),
text = userMessage.content,
onCopyClick = { clipboardManager.setText(AnnotatedString(userMessage.content.trim())) }
)
}
}

Expand Down

0 comments on commit b2f6bbc

Please sign in to comment.