Skip to content

Commit ab1d0da

Browse files
committed
Fix clickable links in chat messages
1 parent b68420f commit ab1d0da

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

‎app/src/main/java/com/example/ui/MainScreen.kt‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import android.annotation.SuppressLint
44
import android.content.ClipData
55
import android.content.ClipboardManager
66
import android.content.Context
7+
import android.content.Intent
78
import android.net.Uri
89
import android.provider.OpenableColumns
910
import android.webkit.CookieManager
@@ -63,6 +64,7 @@ import androidx.compose.foundation.rememberScrollState
6364
import androidx.compose.foundation.shape.CircleShape
6465
import androidx.compose.foundation.shape.RoundedCornerShape
6566
import androidx.compose.foundation.text.BasicTextField
67+
import androidx.compose.foundation.text.ClickableText
6668
import androidx.compose.foundation.text.KeyboardOptions
6769
import androidx.compose.material.icons.Icons
6870
import androidx.compose.material.icons.filled.Add
@@ -1181,24 +1183,46 @@ private fun ChatBubble(
11811183
splitMessage(message.text).forEach { part ->
11821184
when (part) {
11831185
is MessagePart.Text -> if (part.value.isNotBlank()) {
1184-
Text(
1185-
part.value.trim(),
1186+
val linkedText = buildAnnotatedString {
1187+
val value = part.value.trim()
1188+
append(value)
1189+
// Keep the original message intact while annotating each URL.
1190+
Regex("""https?://[^\\s<>]+""").findAll(value).forEach { match ->
1191+
val cleanUrl = match.value.trimEnd('.', ',', ';', ':', '!', '?', ')', ']', '}')
1192+
if (cleanUrl.isNotBlank()) {
1193+
addStringAnnotation(
1194+
tag = "URL",
1195+
annotation = cleanUrl,
1196+
start = match.range.first,
1197+
end = match.range.first + cleanUrl.length
1198+
)
1199+
}
1200+
}
1201+
}
1202+
ClickableText(
1203+
text = linkedText,
11861204
style = TextStyle(
11871205
textDirection = TextDirection.ContentOrRtl,
1188-
shadow = SoftTextShadow
1206+
shadow = SoftTextShadow,
1207+
color = MaterialTheme.colorScheme.onSurface
11891208
),
11901209
lineHeight = 23.sp,
1191-
modifier = Modifier.fillMaxWidth().pointerInput(Unit) {
1192-
detectTapGestures(onTap = { offset ->
1193-
// Simple URL detection and opening
1194-
val urlRegex = Regex("""(https?://[^\s]+)""")
1195-
val matches = urlRegex.findAll(part.value)
1196-
matches.forEach { match ->
1197-
// This is a naive implementation; for real URL handling,
1198-
// use TextLinkScope in M3 or similar.
1199-
// For now, let's just show an toast or try to open browser if we can detect the click.
1210+
modifier = Modifier.fillMaxWidth(),
1211+
onClick = { offset ->
1212+
linkedText.getStringAnnotations("URL", offset, offset)
1213+
.firstOrNull()?.let { annotation ->
1214+
runCatching {
1215+
context.startActivity(
1216+
Intent(Intent.ACTION_VIEW, Uri.parse(annotation.item))
1217+
)
1218+
}.onFailure {
1219+
android.widget.Toast.makeText(
1220+
context,
1221+
"لینک قابل باز کردن نیست",
1222+
android.widget.Toast.LENGTH_SHORT
1223+
).show()
1224+
}
12001225
}
1201-
})
12021226
}
12031227
)
12041228
}

0 commit comments

Comments
 (0)