TransactionLinkComposable.kt (3134B)
1 /* 2 * This file is part of GNU Taler 3 * (C) 2023 Taler Systems S.A. 4 * 5 * GNU Taler is free software; you can redistribute it and/or modify it under the 6 * terms of the GNU General Public License as published by the Free Software 7 * Foundation; either version 3, or (at your option) any later version. 8 * 9 * GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY 10 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 * A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License along with 14 * GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 17 package net.taler.wallet.transactions 18 19 import androidx.compose.foundation.layout.Column 20 import androidx.compose.foundation.layout.padding 21 import androidx.compose.foundation.text.ClickableText 22 import androidx.compose.material3.MaterialTheme 23 import androidx.compose.material3.Text 24 import androidx.compose.runtime.Composable 25 import androidx.compose.ui.Alignment.Companion.CenterHorizontally 26 import androidx.compose.ui.Modifier 27 import androidx.compose.ui.graphics.Color 28 import androidx.compose.ui.platform.LocalContext 29 import androidx.compose.ui.text.SpanStyle 30 import androidx.compose.ui.text.TextStyle 31 import androidx.compose.ui.text.buildAnnotatedString 32 import androidx.compose.ui.text.style.TextAlign 33 import androidx.compose.ui.text.withStyle 34 import androidx.compose.ui.tooling.preview.Preview 35 import androidx.compose.ui.unit.dp 36 import androidx.compose.ui.unit.sp 37 import net.taler.wallet.compose.TalerSurface 38 import net.taler.wallet.getAttrColor 39 40 @Composable 41 // FIXME this assumes that it is used in a column and applies its own padding, not really re-usable 42 fun TransactionLinkComposable(label: String, info: String, onClick: () -> Unit) { 43 Text( 44 modifier = Modifier.padding(top = 16.dp, start = 16.dp, end = 16.dp), 45 text = label, 46 style = MaterialTheme.typography.bodyMedium, 47 ) 48 val context = LocalContext.current 49 val linkColor = Color(context.getAttrColor(android.R.attr.textColorLink)) 50 val annotatedString = buildAnnotatedString { 51 pushStringAnnotation(tag = "url", annotation = info) 52 withStyle(style = SpanStyle(color = linkColor)) { 53 append(info) 54 } 55 pop() 56 } 57 ClickableText( 58 modifier = Modifier.padding(top = 8.dp, start = 16.dp, end = 16.dp, bottom = 16.dp), 59 text = annotatedString, 60 style = TextStyle(fontSize = 24.sp, textAlign = TextAlign.Center), 61 ) { offset -> 62 annotatedString.getStringAnnotations( 63 tag = "url", 64 start = offset, 65 end = offset, 66 ).firstOrNull()?.let { 67 onClick() 68 } 69 } 70 } 71 72 @Preview 73 @Composable 74 fun TransactionLinkComposablePreview() { 75 TalerSurface { 76 Column( 77 horizontalAlignment = CenterHorizontally, 78 ) { 79 TransactionLinkComposable( 80 label = "This is a label", 81 info = "This is some fulfillment message" 82 ) {} 83 } 84 } 85 }