Skip to content

Commit

Permalink
Implement extension function to remove orphan words (#566)
Browse files Browse the repository at this point in the history
* Implement QEText that will remove orphan words

* Add tests for String.removeOrphans
  • Loading branch information
AdamGrzybkowski authored Jan 29, 2025
1 parent c8ac4dc commit 873de57
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.gravatar.quickeditor.ui

internal val String.removeOrphans: String
get() {
val space = " "
val index = lastIndexOf(space, ignoreCase = true)
return if (index < 0) this else this.replaceRange(index, index + space.length, "\u00A0")
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.sp
import com.gravatar.quickeditor.R
import com.gravatar.quickeditor.ui.removeOrphans
import com.gravatar.ui.GravatarTheme

@Composable
internal fun QESectionMessage(message: String, modifier: Modifier = Modifier) {
Text(
text = message,
text = message.removeOrphans,
fontSize = 15.sp,
color = MaterialTheme.colorScheme.tertiary,
modifier = modifier,
)
}

@Composable
@Preview(showBackground = true)
@Preview(showBackground = true, widthDp = 300)
private fun QESectionMessagePreview() {
GravatarTheme {
QESectionMessage(message = stringResource(id = R.string.gravatar_qe_avatar_picker_description))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.gravatar.quickeditor.ui

import org.junit.Assert.assertEquals
import org.junit.Test

internal class StringExtensionsTest {
@Test
fun `give a string with spaces when orphans removed then last space replaced with non-breaking`() {
// Given
val input = "This is a test string with spaces"
val expected = "This is a test string with\u00A0spaces"

// When
val actual = input.removeOrphans

// Then
assertEquals(expected, actual)
}

@Test
fun `give a string without spaces when orphans removed then string remains unchanged`() {
// Given
val input = "ThisIsATestStringWithoutSpaces"

// When
val actual = input.removeOrphans

// Then
assertEquals(input, actual)
}

@Test
fun `given an empty string when orphans removed then string remains unchanged`() {
// Given
val input = ""

// When
val actual = input.removeOrphans

// Then
assertEquals(input, actual)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.gravatar.quickeditor.ui.components

import androidx.compose.foundation.layout.width
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.gravatar.quickeditor.ui.gravatarScreenshotTest
import com.gravatar.uitestutils.RoborazziTest
import org.junit.Test

class QESectionMessageTest : RoborazziTest() {
@Test
fun qeTextTestNoOrphan() = gravatarScreenshotTest {
Surface(
modifier = Modifier.width(220.dp),
) {
QESectionMessage(message = "This is a long text that should not break just before the last word.")
}
}
}

0 comments on commit 873de57

Please sign in to comment.