pocmo / recompose

recompose is a tool for converting Android layouts in XML to Kotlin code using Jetpack Compose.
Apache License 2.0
783 stars 49 forks source link

KotlinWriterHelper #101

Open tieskedh opened 3 years ago

tieskedh commented 3 years ago

I saw you were looking for a higher level for KotlinWriter? Maybe something like this is an option?

The code below

internal class KotlinWriterHelper(
        val writer: KotlinWriter
) {
    operator fun String.invoke(
            vararg parameters: CallParameter?,
            block: (KotlinWriter.() -> Unit)? = null
    ) = writer.writeCall(
            name = this,
            parameters = parameters.toList(),
            block = block
    )
    fun write(writer: KotlinWriterHelper.() -> Unit) = writer()
}

would change this code

writer.writeCall(
       "Row",
       parameters = listOf(
           rowModifier.toCallParameter()
        )
) {
    writeCall(
        name = "RadioButton",
        parameters = listOf(
            CallParameter(
                name = "selected",
                value = ParameterValue.RawValue(node.checked)
            ),
            CallParameter(
                name = "onCheckedChange",
                value = ParameterValue.EmptyLambdaValue
            )
       )
   )
}

into

write{
    "Row"(
         rowModifier.toCallParameter()
    ){
    "RadioButton"(
        CallParameter(
             name = "selected",
             value = ParameterValue.RawValue(node.checked)
        ),
        CallParameter(
            name = "onCheckedChange",
            value = ParameterValue.EmptyLambdaValue
        )
    )
}

Adding functions like:

    internal infix fun String.withSizeValue(size: Size) = CallParameter(ParameterValue.SizeValue(size), this)
    internal infix fun String.withColorValue(color: Color) = CallParameter(ParameterValue.ColorValue(color), this)
    internal infix fun String?.withRawValue(raw: String) = CallParameter(ParameterValue.RawValue(raw), this)
    internal infix fun String.withRawValue(raw: Boolean) = CallParameter(ParameterValue.RawValue(raw), this)
    internal infix fun String.withRawValue(raw: Int) = CallParameter(ParameterValue.RawValue(raw), this)
    internal infix fun String?.withStringValue(raw: String) = CallParameter(ParameterValue.StringValue(raw), this)
    internal fun String.withEmptyLambda() = CallParameter(ParameterValue.EmptyLambdaValue, this)
    internal infix fun String.withKeyboardType(inputType: InputType) = CallParameter(ParameterValue.KeyboardTypeValue(inputType), this)

would simplify it to:

write{
    "Row"(
         rowModifier.toCallParameter()
    ){
    "RadioButton"(
        "selected" withRawValue node.checked,
        "onCheckedChange".withEmptyLambda()
    )
}

(these params could be written in an interal interface, which then would be implemented by the KotlinWriterHelper)

We then could make the names values or getters inside an interface:

    val Text = "Text"
    val Box = "Box"
    val Button = "Button"
    val Row = "Row"
    val Checkbox = "Checkbox"
    val RadioButton = "RadioButton"
    val Card = "Card"
    val Image = "Image"
    val TextField = "TextField"

Such that we can write:

write{
    Row(
         rowModifier.toCallParameter()
    ){
    RadioButton(
        "selected" withRawValue node.checked,
        "onCheckedChange".withEmptyLambda()
    )
}

And we could add a Text function:

fun KotlinWriterHelper.Text(
        vararg params : CallParameter?,
        text: String? = null,
    ) = "Text"(
        text?.let {
            null withStringValue it
        },
        *params
    )
)

(I placed the varargs at the start, as that would keep compiling all the old code)

pocmo commented 3 years ago

Also see: https://github.com/pocmo/recompose/pull/76