-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
719ffe9
commit 1467975
Showing
3 changed files
with
90 additions
and
1 deletion.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
app/src/androidTest/java/au/com/shiftyjelly/pocketcasts/views/extensions/SpannedKtTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...s/services/views/src/main/java/au/com/shiftyjelly/pocketcasts/views/extensions/Spanned.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |