NTP-35382 | replaced deprecated clickableText with BasicText (#14954)

This commit is contained in:
Shaurya Rehan
2025-02-13 12:46:40 +05:30
committed by GitHub
parent 8752caf20d
commit e0e7f25bcf

View File

@@ -24,6 +24,7 @@ import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
@@ -47,7 +48,7 @@ import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.text.BasicText
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.Card
@@ -95,6 +96,7 @@ import androidx.compose.ui.graphics.Brush
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.layout.layoutId
import androidx.compose.ui.platform.LocalConfiguration
@@ -107,6 +109,7 @@ import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.AnnotatedString
import androidx.compose.ui.text.SpanStyle
import androidx.compose.ui.text.TextLayoutResult
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.buildAnnotatedString
import androidx.compose.ui.text.font.FontWeight
@@ -2111,9 +2114,14 @@ fun PrimaryAndSecondaryButton(
}
@Composable
fun CustomClickableText(fullText: String, clickableText: String, url: String, modifier: Modifier) {
fun CustomClickableText(
fullText: String,
clickableText: String,
url: String,
modifier: Modifier = Modifier,
) {
val annotatedString = buildAnnotatedString {
append(text = fullText)
append(fullText)
addStyle(
style =
SpanStyle(
@@ -2127,46 +2135,59 @@ fun CustomClickableText(fullText: String, clickableText: String, url: String, mo
)
val clickableTextIndex = fullText.indexOf(clickableText)
addStyle(
style =
SpanStyle(
fontFamily = naviFontFamily,
fontWeight = getFontWeight(FontWeightEnum.NAVI_BODY_DEMI_BOLD),
fontSize = 12.sp,
color = ctaPrimary,
textDecoration = TextDecoration.Underline,
),
start = clickableTextIndex,
end = clickableTextIndex + clickableText.length,
)
if (clickableTextIndex >= 0) {
addStyle(
style =
SpanStyle(
fontFamily = naviFontFamily,
fontWeight = getFontWeight(FontWeightEnum.NAVI_BODY_DEMI_BOLD),
fontSize = 12.sp,
color = ctaPrimary,
textDecoration = TextDecoration.Underline,
),
start = clickableTextIndex,
end = clickableTextIndex + clickableText.length,
)
addStringAnnotation(
tag = "URL",
annotation = url,
start = clickableTextIndex,
end = clickableTextIndex + clickableText.length,
)
addStringAnnotation(
tag = "URL",
annotation = url,
start = clickableTextIndex,
end = clickableTextIndex + clickableText.length,
)
}
}
val uriHandler = LocalUriHandler.current
var textLayoutResult by remember { mutableStateOf<TextLayoutResult?>(null) }
ClickableText(
BasicText(
text = annotatedString,
onClick = { offset ->
annotatedString
.getStringAnnotations(tag = "URL", start = offset, end = offset)
.firstOrNull()
?.let { annotation ->
if (annotation.item.isNotEmpty()) {
try {
uriHandler.openUri(annotation.item)
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
modifier =
modifier.pointerInput(Unit) {
detectTapGestures { tapOffset ->
textLayoutResult?.let { layoutResult ->
val clickedOffset = layoutResult.getOffsetForPosition(tapOffset)
val annotation =
annotatedString
.getStringAnnotations(
tag = "URL",
start = clickedOffset,
end = clickedOffset,
)
.firstOrNull()
if (annotation != null) {
try {
uriHandler.openUri(annotation.item)
} catch (e: Exception) {
FirebaseCrashlytics.getInstance().recordException(e)
}
}
}
}
},
modifier = modifier,
},
onTextLayout = { textLayoutResult = it },
)
}