From 14679751c3ad502a7c834dd62f1b93ca5308fa3b Mon Sep 17 00:00:00 2001 From: Philip Simpson Date: Mon, 16 Dec 2024 19:40:24 +1030 Subject: [PATCH] Remove the padding from description --- .../views/extensions/SpannedKtTest.kt | 44 +++++++++++++++++++ .../podcasts/view/podcast/PodcastAdapter.kt | 4 +- .../pocketcasts/views/extensions/Spanned.kt | 43 ++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 app/src/androidTest/java/au/com/shiftyjelly/pocketcasts/views/extensions/SpannedKtTest.kt create mode 100644 modules/services/views/src/main/java/au/com/shiftyjelly/pocketcasts/views/extensions/Spanned.kt diff --git a/app/src/androidTest/java/au/com/shiftyjelly/pocketcasts/views/extensions/SpannedKtTest.kt b/app/src/androidTest/java/au/com/shiftyjelly/pocketcasts/views/extensions/SpannedKtTest.kt new file mode 100644 index 00000000000..90d82d40dbd --- /dev/null +++ b/app/src/androidTest/java/au/com/shiftyjelly/pocketcasts/views/extensions/SpannedKtTest.kt @@ -0,0 +1,44 @@ +package au.com.shiftyjelly.pocketcasts.views.extensions + +import android.text.SpannableStringBuilder +import org.junit.Assert.assertEquals +import org.junit.Test + +class SpannedKtTest { + + @Test + fun trimPaddingEmptyString() { + val input = SpannableStringBuilder("") + val result = input.trimPadding() + assertEquals("", result.toString()) + } + + @Test + fun trimPaddingNoPadding() { + val input = SpannableStringBuilder("No padding") + val result = input.trimPadding() + assertEquals("No padding", result.toString()) + } + + @Test + fun trimPaddingTopPadding() { + val input = SpannableStringBuilder("\n\nTop padding") + val result = input.trimPadding() + assertEquals("Top padding", result.toString()) + } + + @Test + fun trimPaddingBottomPadding() { + val input = SpannableStringBuilder("Bottom padding\n\n") + val result = input.trimPadding() + assertEquals("Bottom padding", result.toString()) + } + + @Test + fun trimPaddingTopAndBottomPadding() { + val input = SpannableStringBuilder("\n\nTop and bottom padding\n\n") + assertEquals("\n\nTop and bottom padding\n\n", input.toString()) + val result = input.trimPadding() + assertEquals("Top and bottom padding", result.toString()) + } +} diff --git a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt index 90907b6f836..306aaab91b8 100644 --- a/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt +++ b/modules/features/podcasts/src/main/java/au/com/shiftyjelly/pocketcasts/podcasts/view/podcast/PodcastAdapter.kt @@ -70,6 +70,7 @@ import au.com.shiftyjelly.pocketcasts.utils.featureflag.FeatureFlag import au.com.shiftyjelly.pocketcasts.views.extensions.hide import au.com.shiftyjelly.pocketcasts.views.extensions.show import au.com.shiftyjelly.pocketcasts.views.extensions.toggleVisibility +import au.com.shiftyjelly.pocketcasts.views.extensions.trimPadding import au.com.shiftyjelly.pocketcasts.views.helper.AnimatorUtil import au.com.shiftyjelly.pocketcasts.views.helper.SwipeButtonLayoutFactory import au.com.shiftyjelly.pocketcasts.views.helper.toCircle @@ -306,12 +307,13 @@ class PodcastAdapter( text = podcast.displayableNextEpisodeDate(context) } holder.binding.bottom.description.text = if (FeatureFlag.isEnabled(Feature.PODCAST_HTML_DESCRIPTION)) { - Html.fromHtml(podcast.podcastDescription, Html.FROM_HTML_MODE_COMPACT) + Html.fromHtml(podcast.podcastDescription, Html.FROM_HTML_MODE_COMPACT).trimPadding() } else { podcast.podcastDescription } holder.binding.bottom.description.setLinkTextColor(tintColor) holder.binding.bottom.description.readMore(3) + holder.binding.bottom.description.setPadding(0, 0, 0, 0) holder.binding.bottom.authorText.text = podcast.author holder.binding.bottom.authorText.isVisible = podcast.author.isNotBlank() holder.binding.bottom.authorImage.isVisible = podcast.author.isNotBlank() diff --git a/modules/services/views/src/main/java/au/com/shiftyjelly/pocketcasts/views/extensions/Spanned.kt b/modules/services/views/src/main/java/au/com/shiftyjelly/pocketcasts/views/extensions/Spanned.kt new file mode 100644 index 00000000000..97f7ba539fd --- /dev/null +++ b/modules/services/views/src/main/java/au/com/shiftyjelly/pocketcasts/views/extensions/Spanned.kt @@ -0,0 +1,43 @@ +package au.com.shiftyjelly.pocketcasts.views.extensions + +import android.text.SpannableStringBuilder +import android.text.Spanned +import timber.log.Timber + +/** + * Removes top and bottom padding. + */ +fun Spanned.trimPadding(): Spanned { + val spannable = this as? SpannableStringBuilder ?: return this + + try { + var trimStart = 0 + var trimEnd = 0 + + var text = spannable.toString() + + if (text.isEmpty()) { + return this + } + + while (text.startsWith("\n")) { + text = text.substring(1) + trimStart += 1 + } + if (trimStart > 0) { + spannable.delete(0, trimStart) + } + + while (text.endsWith("\n")) { + text = text.substring(0, text.length - 1) + trimEnd += 1 + } + if (trimEnd > 0) { + spannable.delete(spannable.length - trimEnd, spannable.length) + } + } catch (e: Exception) { + Timber.e(e) + } + + return spannable +}