From 9b6eec5fdde30fb96ee285210a62a0238d979786 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Thu, 21 Sep 2023 08:11:25 +0200 Subject: [PATCH 1/4] Introduced line limit in addition to existing character limit for "show more" folding of text --- .../ui/components/ExpandableRichTextViewer.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 e3551cb1f..3d24290b2 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 @@ -31,6 +31,7 @@ import com.vitorpamplona.amethyst.ui.theme.secondaryButtonBackground import com.vitorpamplona.quartz.events.ImmutableListOfLists const val SHORT_TEXT_LENGTH = 350 +const val SHORTEN_AFTER_LINES = 10 @Composable fun ExpandableRichTextViewer( @@ -45,11 +46,24 @@ fun ExpandableRichTextViewer( var showFullText by remember { mutableStateOf(false) } val whereToCut = remember(content) { - // Cuts the text in the first space after 350 + // Cuts the text in the first space or new line after SHORT_TEXT_LENGTH characters val firstSpaceAfterCut = content.indexOf(' ', SHORT_TEXT_LENGTH).let { if (it < 0) content.length else it } val firstNewLineAfterCut = content.indexOf('\n', SHORT_TEXT_LENGTH).let { if (it < 0) content.length else it } - minOf(firstSpaceAfterCut, firstNewLineAfterCut) + // or after SHORTEN_AFTER_LINES lines + val numberOfLines = content.count { it == '\n' } + + if (numberOfLines > SHORTEN_AFTER_LINES) { + val shortContent = content.lines().take(SHORTEN_AFTER_LINES) + var charactersInLines = 0 + for (line in shortContent) { + // +1 because new line character is omitted from .lines + charactersInLines += (line.length + 1) + } + charactersInLines + } else { + minOf(firstSpaceAfterCut, firstNewLineAfterCut) + } } val text by remember(content) { From ccb2c626a79a521c8e51676d6caea5b9181eae3d Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Thu, 21 Sep 2023 08:27:16 +0200 Subject: [PATCH 2/4] Combine new line limit with previous character checking (use the smallest) --- .../amethyst/ui/components/ExpandableRichTextViewer.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 3d24290b2..97eb86073 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 @@ -53,17 +53,18 @@ fun ExpandableRichTextViewer( // or after SHORTEN_AFTER_LINES lines val numberOfLines = content.count { it == '\n' } + var charactersInLines = minOf(firstSpaceAfterCut, firstNewLineAfterCut) + if (numberOfLines > SHORTEN_AFTER_LINES) { val shortContent = content.lines().take(SHORTEN_AFTER_LINES) - var charactersInLines = 0 + charactersInLines = 0 for (line in shortContent) { // +1 because new line character is omitted from .lines charactersInLines += (line.length + 1) } - charactersInLines - } else { - minOf(firstSpaceAfterCut, firstNewLineAfterCut) } + + minOf(firstSpaceAfterCut, firstNewLineAfterCut, charactersInLines) } val text by remember(content) { From 77afedb2d0e2763a037933cd60d6652f9380f5f0 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Thu, 21 Sep 2023 08:11:25 +0200 Subject: [PATCH 3/4] Introduced line limit in addition to existing character limit for "show more" folding of text --- .../ui/components/ExpandableRichTextViewer.kt | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) 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 e3551cb1f..3d24290b2 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 @@ -31,6 +31,7 @@ import com.vitorpamplona.amethyst.ui.theme.secondaryButtonBackground import com.vitorpamplona.quartz.events.ImmutableListOfLists const val SHORT_TEXT_LENGTH = 350 +const val SHORTEN_AFTER_LINES = 10 @Composable fun ExpandableRichTextViewer( @@ -45,11 +46,24 @@ fun ExpandableRichTextViewer( var showFullText by remember { mutableStateOf(false) } val whereToCut = remember(content) { - // Cuts the text in the first space after 350 + // Cuts the text in the first space or new line after SHORT_TEXT_LENGTH characters val firstSpaceAfterCut = content.indexOf(' ', SHORT_TEXT_LENGTH).let { if (it < 0) content.length else it } val firstNewLineAfterCut = content.indexOf('\n', SHORT_TEXT_LENGTH).let { if (it < 0) content.length else it } - minOf(firstSpaceAfterCut, firstNewLineAfterCut) + // or after SHORTEN_AFTER_LINES lines + val numberOfLines = content.count { it == '\n' } + + if (numberOfLines > SHORTEN_AFTER_LINES) { + val shortContent = content.lines().take(SHORTEN_AFTER_LINES) + var charactersInLines = 0 + for (line in shortContent) { + // +1 because new line character is omitted from .lines + charactersInLines += (line.length + 1) + } + charactersInLines + } else { + minOf(firstSpaceAfterCut, firstNewLineAfterCut) + } } val text by remember(content) { From 59608e9acc657255d9739ba0ee1c590ab09e2715 Mon Sep 17 00:00:00 2001 From: David Kaspar Date: Thu, 21 Sep 2023 08:27:16 +0200 Subject: [PATCH 4/4] Combine new line limit with previous character checking (use the smallest) --- .../amethyst/ui/components/ExpandableRichTextViewer.kt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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 3d24290b2..97eb86073 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 @@ -53,17 +53,18 @@ fun ExpandableRichTextViewer( // or after SHORTEN_AFTER_LINES lines val numberOfLines = content.count { it == '\n' } + var charactersInLines = minOf(firstSpaceAfterCut, firstNewLineAfterCut) + if (numberOfLines > SHORTEN_AFTER_LINES) { val shortContent = content.lines().take(SHORTEN_AFTER_LINES) - var charactersInLines = 0 + charactersInLines = 0 for (line in shortContent) { // +1 because new line character is omitted from .lines charactersInLines += (line.length + 1) } - charactersInLines - } else { - minOf(firstSpaceAfterCut, firstNewLineAfterCut) } + + minOf(firstSpaceAfterCut, firstNewLineAfterCut, charactersInLines) } val text by remember(content) {