-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement QEText that will remove orphan words
- Loading branch information
1 parent
a366224
commit 3f0cedd
Showing
4 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
Binary file added
BIN
+15.3 KB
...ests/roborazzi/com.gravatar.quickeditor.ui.components.QETextTest.qeTextTest.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
75 changes: 75 additions & 0 deletions
75
gravatar-quickeditor/src/main/java/com/gravatar/quickeditor/ui/components/QEText.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,75 @@ | ||
package com.gravatar.quickeditor.ui.components | ||
|
||
import androidx.compose.material3.LocalTextStyle | ||
import androidx.compose.material3.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.TextLayoutResult | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.font.FontFamily | ||
import androidx.compose.ui.text.font.FontStyle | ||
import androidx.compose.ui.text.font.FontWeight | ||
import androidx.compose.ui.text.style.LineBreak | ||
import androidx.compose.ui.text.style.TextAlign | ||
import androidx.compose.ui.text.style.TextDecoration | ||
import androidx.compose.ui.text.style.TextOverflow | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.TextUnit | ||
|
||
/** | ||
* Wrapper for the default Text component to make sure line is not broken just before the last word. | ||
*/ | ||
@Composable | ||
internal fun QEText( | ||
text: String, | ||
modifier: Modifier = Modifier, | ||
color: Color = Color.Unspecified, | ||
fontSize: TextUnit = TextUnit.Unspecified, | ||
fontStyle: FontStyle? = null, | ||
fontWeight: FontWeight? = null, | ||
fontFamily: FontFamily? = null, | ||
letterSpacing: TextUnit = TextUnit.Unspecified, | ||
textDecoration: TextDecoration? = null, | ||
textAlign: TextAlign? = null, | ||
lineHeight: TextUnit = TextUnit.Unspecified, | ||
overflow: TextOverflow = TextOverflow.Clip, | ||
softWrap: Boolean = true, | ||
maxLines: Int = Int.MAX_VALUE, | ||
minLines: Int = 1, | ||
onTextLayout: ((TextLayoutResult) -> Unit)? = null, | ||
style: TextStyle = LocalTextStyle.current, | ||
) { | ||
Text( | ||
text = text.removeOrphans, | ||
modifier = modifier, | ||
color = color, | ||
fontSize = fontSize, | ||
fontStyle = fontStyle, | ||
fontWeight = fontWeight, | ||
fontFamily = fontFamily, | ||
letterSpacing = letterSpacing, | ||
textDecoration = textDecoration, | ||
textAlign = textAlign, | ||
lineHeight = lineHeight, | ||
overflow = overflow, | ||
softWrap = softWrap, | ||
maxLines = maxLines, | ||
minLines = minLines, | ||
onTextLayout = onTextLayout, | ||
style = style.copy(lineBreak = LineBreak.Paragraph), | ||
) | ||
} | ||
|
||
private 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") | ||
} | ||
|
||
@Preview(showBackground = true, widthDp = 380) | ||
@Composable | ||
private fun QETextPreview() { | ||
QEText(text = "This is a long text that should not break just before the last word.") | ||
} |
20 changes: 20 additions & 0 deletions
20
gravatar-quickeditor/src/test/java/com/gravatar/quickeditor/ui/components/QETextTest.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,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 QETextTest : RoborazziTest() { | ||
@Test | ||
fun qeTextTest() = gravatarScreenshotTest { | ||
Surface( | ||
modifier = Modifier.width(250.dp), | ||
) { | ||
QEText(text = "This is a long text that should not break just before the last word.") | ||
} | ||
} | ||
} |