@@ -4,19 +4,19 @@ import android.content.Context
44import android.content.Intent
55import androidx.compose.foundation.layout.Row
66import androidx.compose.foundation.layout.padding
7- import androidx.compose.foundation.text.ClickableText
87import androidx.compose.material.Icon
98import androidx.compose.material.IconButton
109import androidx.compose.material.MaterialTheme
10+ import androidx.compose.material.Text
1111import androidx.compose.material.icons.Icons
1212import androidx.compose.material.icons.filled.Share
1313import androidx.compose.runtime.Composable
1414import androidx.compose.runtime.remember
1515import androidx.compose.ui.Alignment
1616import androidx.compose.ui.Modifier
1717import androidx.compose.ui.platform.LocalContext
18- import androidx.compose.ui.platform.LocalUriHandler
1918import androidx.compose.ui.res.stringResource
19+ import androidx.compose.ui.text.LinkAnnotation
2020import androidx.compose.ui.text.SpanStyle
2121import androidx.compose.ui.text.buildAnnotatedString
2222import androidx.compose.ui.text.withStyle
@@ -49,68 +49,57 @@ fun CommandView(
4949 onNavigate : (String ) -> Unit = {},
5050) {
5151 val codeColor = MaterialTheme .colors.primary
52- val annotatedString = remember(codeColor) {
52+ val annotatedString = remember(elements, codeColor) {
5353 buildAnnotatedString {
54- elements.forEachIndexed { index, element ->
54+ elements.forEach { element ->
5555 when (element) {
56- is CommandElement .Text -> append(element.text)
56+ is CommandElement .Text -> {
57+ append(element.text)
58+ }
5759 is CommandElement .Man -> {
58- pushStringAnnotation(tag = " $index " , annotation = element.man)
60+ val start = this .length
5961 withStyle(style = SpanStyle (color = codeColor)) {
6062 append(element.man)
6163 }
62- pop()
64+ val end = this .length
65+ addLink(
66+ LinkAnnotation .Clickable (
67+ tag = " man:${element.man} " ,
68+ linkInteractionListener = {
69+ val manCommand = databaseHelper.getCommand(element.man)
70+ if (manCommand != null ) {
71+ onNavigate(" command?commandId=${manCommand.id} &commandName=${manCommand.name} " )
72+ }
73+ },
74+ ),
75+ start,
76+ end,
77+ )
6378 }
64-
6579 is CommandElement .Url -> {
66- pushStringAnnotation(tag = " $index " , annotation = element.url)
80+ val start = this .length
6781 withStyle(style = SpanStyle (color = codeColor)) {
6882 append(element.command)
6983 }
70- pop()
84+ val end = this .length
85+ addLink(
86+ LinkAnnotation .Url (element.url),
87+ start,
88+ end,
89+ )
7190 }
7291 }
7392 }
7493 }
7594 }
7695
77- val uriHandler = LocalUriHandler .current
78- Row (modifier = Modifier .padding(start = 12 .dp, end = 4 .dp, top = 8 .dp, bottom = 8 .dp)) {
79- ClickableText (
96+ Row (modifier = Modifier .padding(start = 12 .dp, end = 4 .dp, top = 6 .dp, bottom = 6 .dp)) {
97+ Text (
8098 text = annotatedString,
8199 modifier = Modifier
82100 .weight(1f )
83101 .align(Alignment .CenterVertically ),
84102 style = MaterialTheme .typography.subtitle2,
85- onClick = { offset ->
86- elements.forEachIndexed { index, element ->
87- if (element is CommandElement .Url ) {
88- annotatedString.getStringAnnotations(
89- tag = " $index " ,
90- start = offset,
91- end = offset,
92- )
93- .firstOrNull()
94- ?.let {
95- uriHandler.openUri(it.item)
96- }
97- }
98- if (element is CommandElement .Man ) {
99- annotatedString.getStringAnnotations(
100- tag = " $index " ,
101- start = offset,
102- end = offset,
103- )
104- .firstOrNull()
105- ?.let {
106- val manCommand = databaseHelper.getCommand(it.item)
107- if (manCommand != null ) {
108- onNavigate(" command?commandId=${manCommand.id} &commandName=${manCommand.name} " )
109- }
110- }
111- }
112- }
113- },
114103 )
115104
116105 val context = LocalContext .current
0 commit comments