Skip to content

Commit 873de57

Browse files
Implement extension function to remove orphan words (#566)
* Implement QEText that will remove orphan words * Add tests for String.removeOrphans
1 parent c8ac4dc commit 873de57

File tree

5 files changed

+74
-2
lines changed

5 files changed

+74
-2
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.gravatar.quickeditor.ui
2+
3+
internal val String.removeOrphans: String
4+
get() {
5+
val space = " "
6+
val index = lastIndexOf(space, ignoreCase = true)
7+
return if (index < 0) this else this.replaceRange(index, index + space.length, "\u00A0")
8+
}

gravatar-quickeditor/src/main/java/com/gravatar/quickeditor/ui/components/QESectionMessage.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ import androidx.compose.ui.res.stringResource
88
import androidx.compose.ui.tooling.preview.Preview
99
import androidx.compose.ui.unit.sp
1010
import com.gravatar.quickeditor.R
11+
import com.gravatar.quickeditor.ui.removeOrphans
1112
import com.gravatar.ui.GravatarTheme
1213

1314
@Composable
1415
internal fun QESectionMessage(message: String, modifier: Modifier = Modifier) {
1516
Text(
16-
text = message,
17+
text = message.removeOrphans,
1718
fontSize = 15.sp,
1819
color = MaterialTheme.colorScheme.tertiary,
1920
modifier = modifier,
2021
)
2122
}
2223

2324
@Composable
24-
@Preview(showBackground = true)
25+
@Preview(showBackground = true, widthDp = 300)
2526
private fun QESectionMessagePreview() {
2627
GravatarTheme {
2728
QESectionMessage(message = stringResource(id = R.string.gravatar_qe_avatar_picker_description))
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.gravatar.quickeditor.ui
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Test
5+
6+
internal class StringExtensionsTest {
7+
@Test
8+
fun `give a string with spaces when orphans removed then last space replaced with non-breaking`() {
9+
// Given
10+
val input = "This is a test string with spaces"
11+
val expected = "This is a test string with\u00A0spaces"
12+
13+
// When
14+
val actual = input.removeOrphans
15+
16+
// Then
17+
assertEquals(expected, actual)
18+
}
19+
20+
@Test
21+
fun `give a string without spaces when orphans removed then string remains unchanged`() {
22+
// Given
23+
val input = "ThisIsATestStringWithoutSpaces"
24+
25+
// When
26+
val actual = input.removeOrphans
27+
28+
// Then
29+
assertEquals(input, actual)
30+
}
31+
32+
@Test
33+
fun `given an empty string when orphans removed then string remains unchanged`() {
34+
// Given
35+
val input = ""
36+
37+
// When
38+
val actual = input.removeOrphans
39+
40+
// Then
41+
assertEquals(input, actual)
42+
}
43+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.gravatar.quickeditor.ui.components
2+
3+
import androidx.compose.foundation.layout.width
4+
import androidx.compose.material3.Surface
5+
import androidx.compose.ui.Modifier
6+
import androidx.compose.ui.unit.dp
7+
import com.gravatar.quickeditor.ui.gravatarScreenshotTest
8+
import com.gravatar.uitestutils.RoborazziTest
9+
import org.junit.Test
10+
11+
class QESectionMessageTest : RoborazziTest() {
12+
@Test
13+
fun qeTextTestNoOrphan() = gravatarScreenshotTest {
14+
Surface(
15+
modifier = Modifier.width(220.dp),
16+
) {
17+
QESectionMessage(message = "This is a long text that should not break just before the last word.")
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)