From 635c7a32ae7cf36a0d09633c85ea55c0c1d93583 Mon Sep 17 00:00:00 2001 From: Giannis Stamatopoulos Date: Fri, 20 Sep 2024 13:24:00 +0300 Subject: [PATCH 1/2] Refactor: Replace deprecated ClickableText. - Brought Compose's ClickableText Composable inside the project. --- .../ec/uilogic/component/ClickableText.kt | 61 +++++++++++++++++++ .../uilogic/component/content/ContentTitle.kt | 2 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt diff --git a/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt b/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt new file mode 100644 index 00000000..ecffa456 --- /dev/null +++ b/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt @@ -0,0 +1,61 @@ +package eu.europa.ec.uilogic.component + +import androidx.compose.foundation.gestures.detectTapGestures +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.text.AnnotatedString +import androidx.compose.ui.text.TextLayoutResult +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.style.TextOverflow + +/** + * Composable function that displays text and allows the user to click on it. + * The click event provides the character offset within the text that was clicked. + * + * @param text The text to display. + * @param modifier Modifier to be applied to the Text composable. + * @param style Style configuration for the text. + * @param softWrap Whether the text should break at soft line breaks. + * @param overflow How visual overflow should be handled. + * @param maxLines An optional maximum number of lines for the text to span, wrapping if necessary. + * @param onTextLayout Callback that is executed when a new text layout is calculated. + * @param onClick Callback that is executed when the user clicks on the text. The callback provides + * the character offset within the text that was clicked. + */ +@Composable +fun ClickableText( + text: AnnotatedString, + modifier: Modifier = Modifier, + style: TextStyle = TextStyle.Default, + softWrap: Boolean = true, + overflow: TextOverflow = TextOverflow.Clip, + maxLines: Int = Int.MAX_VALUE, + onTextLayout: (TextLayoutResult) -> Unit = {}, + onClick: (Int) -> Unit, +) { + val layoutResult = remember { mutableStateOf(null) } + val pressIndicator = Modifier.pointerInput(onClick) { + detectTapGestures { pos -> + layoutResult.value?.let { layoutResult -> + onClick(layoutResult.getOffsetForPosition(pos)) + } + } + } + + Text( + text = text, + modifier = modifier.then(pressIndicator), + style = style, + softWrap = softWrap, + overflow = overflow, + maxLines = maxLines, + onTextLayout = { + layoutResult.value = it + onTextLayout(it) + } + ) +} \ No newline at end of file diff --git a/ui-logic/src/main/java/eu/europa/ec/uilogic/component/content/ContentTitle.kt b/ui-logic/src/main/java/eu/europa/ec/uilogic/component/content/ContentTitle.kt index 4debe2e3..f4025cb8 100644 --- a/ui-logic/src/main/java/eu/europa/ec/uilogic/component/content/ContentTitle.kt +++ b/ui-logic/src/main/java/eu/europa/ec/uilogic/component/content/ContentTitle.kt @@ -25,7 +25,6 @@ import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.wrapContentHeight import androidx.compose.foundation.layout.wrapContentWidth -import androidx.compose.foundation.text.ClickableText import androidx.compose.foundation.text.InlineTextContent import androidx.compose.foundation.text.appendInlineContent import androidx.compose.material3.MaterialTheme @@ -47,6 +46,7 @@ import eu.europa.ec.businesslogic.util.safeLet import eu.europa.ec.resourceslogic.theme.values.textPrimaryDark import eu.europa.ec.resourceslogic.theme.values.textSecondaryDark import eu.europa.ec.uilogic.component.AppIcons +import eu.europa.ec.uilogic.component.ClickableText import eu.europa.ec.uilogic.component.utils.SIZE_SMALL import eu.europa.ec.uilogic.component.utils.SPACING_MEDIUM import eu.europa.ec.uilogic.component.utils.VSpacer From ff882c63ebf4c4b43d49f75921ffef0dd4732165 Mon Sep 17 00:00:00 2001 From: Giannis Stamatopoulos Date: Fri, 20 Sep 2024 13:45:37 +0300 Subject: [PATCH 2/2] fix: Added copyright notice to ClickableText.kt file. --- .../europa/ec/uilogic/component/ClickableText.kt | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt b/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt index ecffa456..196aad84 100644 --- a/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt +++ b/ui-logic/src/main/java/eu/europa/ec/uilogic/component/ClickableText.kt @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2023 European Commission + * + * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by the European + * Commission - subsequent versions of the EUPL (the "Licence"); You may not use this work + * except in compliance with the Licence. + * + * You may obtain a copy of the Licence at: + * https://joinup.ec.europa.eu/software/page/eupl + * + * Unless required by applicable law or agreed to in writing, software distributed under + * the Licence is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF + * ANY KIND, either express or implied. See the Licence for the specific language + * governing permissions and limitations under the Licence. + */ + package eu.europa.ec.uilogic.component import androidx.compose.foundation.gestures.detectTapGestures