Skip to content

Commit 23e8010

Browse files
committed
refactor: make use of when guards
1 parent d1377a3 commit 23e8010

4 files changed

Lines changed: 18 additions & 23 deletions

File tree

build-logic/src/main/kotlin/w2sv.android-library.gradle.kts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ android {
3535
}
3636

3737
publishing { singleVariant("release") { withSourcesJar() } }
38+
39+
kotlin.compilerOptions.freeCompilerArgs.add("-Xwhen-guards")
3840
}

composed-core/src/main/kotlin/com/w2sv/composed/core/Resources.kt

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ fun rememberStyledTextResource(@StringRes id: Int, vararg formatArgs: Any): Anno
5959

6060
/**
6161
* Converts a html-styled resource text to an [AnnotatedString].
62-
* #
62+
*
6363
* Tested with:
6464
* - bold: <b>
6565
* - italic: <i>
@@ -103,24 +103,20 @@ private fun spannableStringToAnnotatedString(text: Spanned, density: Density?):
103103
.forEach { span ->
104104
addStyle(
105105
when (span) {
106-
is StyleSpan -> when (span.style) {
107-
Typeface.NORMAL -> SpanStyle(
108-
fontWeight = FontWeight.Normal,
109-
fontStyle = FontStyle.Normal
110-
)
111-
112-
Typeface.BOLD -> SpanStyle(
113-
fontWeight = FontWeight.Bold,
114-
fontStyle = FontStyle.Normal
115-
)
106+
is StyleSpan if span.style == Typeface.NORMAL -> SpanStyle(
107+
fontWeight = FontWeight.Normal,
108+
fontStyle = FontStyle.Normal
109+
)
116110

117-
Typeface.ITALIC -> SpanStyle(
118-
fontWeight = FontWeight.Normal,
119-
fontStyle = FontStyle.Italic
120-
)
111+
is StyleSpan if span.style == Typeface.BOLD -> SpanStyle(
112+
fontWeight = FontWeight.Bold,
113+
fontStyle = FontStyle.Normal
114+
)
121115

122-
else -> SpanStyle()
123-
}
116+
is StyleSpan if span.style == Typeface.ITALIC -> SpanStyle(
117+
fontWeight = FontWeight.Normal,
118+
fontStyle = FontStyle.Italic
119+
)
124120

125121
is TypefaceSpan -> SpanStyle(
126122
fontFamily = when (span.family) {
@@ -135,9 +131,7 @@ private fun spannableStringToAnnotatedString(text: Spanned, density: Density?):
135131
is AbsoluteSizeSpan -> {
136132
density
137133
?.run { SpanStyle(fontSize = if (span.dip) span.size.dp.toSp() else span.size.toSp()) }
138-
?: throw IllegalArgumentException(
139-
"Found AbsoluteSizeSpan but passed density null. Pass a Density to convert."
140-
)
134+
?: error("Found AbsoluteSizeSpan but passed density null. Pass a Density to convert.")
141135
}
142136

143137
is RelativeSizeSpan -> SpanStyle(fontSize = span.sizeChange.em)

composed-core/src/test/kotlin/com/w2sv/composed/core/ResourcesKtTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ResourcesKtTest {
2626
val composeTestRule = createComposeRule()
2727

2828
@Test
29-
fun conversion() {
29+
fun `test rememberStyledTextResource`() {
3030
composeTestRule.setContent {
3131
testStyledText(
3232
id = R.string.bold,
@@ -87,7 +87,6 @@ private fun testStyledText(
8787
) {
8888
val styled = rememberStyledTextResource(id = id)
8989
assertEquals(stringResource(id = id), styled.toString())
90-
println(styled.spanStyles)
9190
assertEquals(expectedSpanStyleCount, styled.spanStyles.size)
9291
assertTrue(checkFirstSpanStyle(styled.spanStyles.first().item))
9392
}

composed-core/src/test/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010
<string name="underline">Sample text with <u>underline</u> to test.</string>
1111
<string name="colored">Sample text with <font color="#9900FF">colored</font> to test.</string>
1212
<string name="monospace">Sample text with <font face="monospace">monospace</font> to test.</string>
13-
</resources>
13+
</resources>

0 commit comments

Comments
 (0)