diff --git a/app/src/main/java/com/vitorpamplona/amethyst/service/lnurl/LnInvoiceUtil.kt b/app/src/main/java/com/vitorpamplona/amethyst/service/lnurl/LnInvoiceUtil.kt index c2864d57b..9587c8322 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/service/lnurl/LnInvoiceUtil.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/service/lnurl/LnInvoiceUtil.kt @@ -152,4 +152,22 @@ object LnInvoiceUtil { null } } + + /** + * If the string contains an LN invoice, returns a Pair of the start and end + * positions of the invoice in the string. Otherwise, returns (0, 0). This is + * used to ensure we don't accidentally cut an invoice in the middle when taking + * only a portion of the available text. + */ + fun locateInvoice(input: String?): Pair { + if (input == null) { + return Pair(0, 0) + } + val matcher = invoicePattern.matcher(input) + return if (matcher.find()) { + Pair(matcher.start(), matcher.end()) + } else { + Pair(0, 0) + } + } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ExpandableRichTextViewer.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ExpandableRichTextViewer.kt index 5e31fa293..6f53d4203 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ExpandableRichTextViewer.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/components/ExpandableRichTextViewer.kt @@ -26,8 +26,11 @@ import androidx.compose.ui.res.stringResource import androidx.compose.ui.unit.dp import androidx.navigation.NavController import com.vitorpamplona.amethyst.R +import com.vitorpamplona.amethyst.service.lnurl.LnInvoiceUtil import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel +const val SHORT_TEXT_LENGTH = 350 + @Composable fun ExpandableRichTextViewer( content: String, @@ -40,7 +43,16 @@ fun ExpandableRichTextViewer( ) { var showFullText by remember { mutableStateOf(false) } - val text = if (showFullText) content else content.take(350) + val text = if (showFullText) { + content + } else { + val (lnStart, lnEnd) = LnInvoiceUtil.locateInvoice(content) + if (lnStart < SHORT_TEXT_LENGTH && lnEnd > 0) { + content.take(lnEnd) + } else { + content.take(SHORT_TEXT_LENGTH) + } + } Box(contentAlignment = Alignment.BottomCenter) { // CompositionLocalProvider(LocalLayoutDirection provides LayoutDirection.Rtl) {