Skip to content

Commit

Permalink
Closes #3: Add support for color references
Browse files Browse the repository at this point in the history
Closes #78.
  • Loading branch information
pt2121 authored Oct 2, 2020
1 parent 4cae10a commit 5ab09a0
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
4 changes: 4 additions & 0 deletions recompose-ast/src/main/kotlin/recompose/ast/values/Color.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ sealed class Color {
* An absolute color like 0xFFFF0000.
*/
data class Absolute(val value: Long) : Color()
/**
* A resource reference color, e.g. @color/nice_color
*/
data class Resource(val name: String) : Color()
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ internal class ComposingVisitor : Visitor {
name = "Text",
parameters = listOf(
CallParameter(name = "text", value = ParameterValue.StringValue(node.text)),
node.textColor?.let { CallParameter(name = "color", value = ParameterValue.ColoValue(it)) },
node.textColor?.let { CallParameter(name = "color", value = ParameterValue.ColorValue(it)) },
node.textSize?.let { CallParameter(name = "fontSize", value = ParameterValue.SizeValue(it)) },
node.maxLines?.let { CallParameter(name = "maxLines", value = ParameterValue.RawValue(it)) },
modifier.toCallParameter()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ internal class KotlinWriter {
writer.continueLine("{}")
}

private fun writeColor(value: ParameterValue.ColoValue) {
private fun writeColor(value: ParameterValue.ColorValue) {
when (val color = value.color) {
is Color.Absolute -> {
writer.continueLine("Color(")
Expand All @@ -132,6 +132,9 @@ internal class KotlinWriter {
writer.continueLine(".toInt()")
writer.continueLine(")")
}
is Color.Resource -> {
writer.continueLine("Color(ContextCompat.getColor(ContextAmbient.current, R.color.${color.name}))")
}
}
}

Expand Down Expand Up @@ -202,7 +205,7 @@ internal class KotlinWriter {
is Drawable.ColorValue -> writeColor(
// Repackaging as ParameterValue.ColorValue. That's a bit hacky. We probably want to re-use the
// write code instead.
ParameterValue.ColoValue(value.drawable.color)
ParameterValue.ColorValue(value.drawable.color)
)
is Drawable.Resource -> {
writeCall(
Expand Down Expand Up @@ -244,7 +247,7 @@ internal class KotlinWriter {
when (value) {
is ParameterValue.StringValue -> writeString(value)
is ParameterValue.EmptyLambdaValue -> writeEmptyLambda()
is ParameterValue.ColoValue -> writeColor(value)
is ParameterValue.ColorValue -> writeColor(value)
is ParameterValue.ModifierValue -> writeModifierValue(value)
is ParameterValue.RawValue -> writer.continueLine(value.raw)
is ParameterValue.SizeValue -> writeSize(value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class ParameterValue {

object EmptyLambdaValue : ParameterValue()

class ColoValue(
class ColorValue(
val color: Color
) : ParameterValue()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ class ComposerTest {
)
}

@Test
fun `TextView with text color resource reference`() {
assertComposing(
fileName = "textview-color-resource-reference.xml",
"""
Text(text = "color ref", color = Color(ContextCompat.getColor(ContextAmbient.current, R.color.green)))
""".trimIndent()
)
}

@Test
fun `ConstraintLayout with Buttons`() {
assertComposing(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ internal fun XmlPullParser.color(name: String): Color? {
throw Parser.ParserException("Could not parse color: $raw", e)
}

raw.startsWith("@color/") -> Color.Resource(name = raw.removePrefix("@color/"))

// There are multiple more color formats that need to be supported.
else -> throw Parser.ParserException("Unknown color format: $raw")
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="color ref"
android:textColor="@color/green" />

0 comments on commit 5ab09a0

Please sign in to comment.