Skip to content

Commit

Permalink
Remove the padding from description
Browse files Browse the repository at this point in the history
  • Loading branch information
geekygecko committed Dec 16, 2024
1 parent 719ffe9 commit 1467975
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 1467975

Please sign in to comment.