https://kotlinlang.org logo
#compose
Title
# compose
h

Håkon Pettersen

05/30/2022, 10:50 AM
Copy code
@Composable
fun List<SecuritySensor>.asListOfRowImageText() = asList {
    RowImageText(
        leadingIconRes = it.icon,
        text = { it.description },
        textStyle = MaterialTheme.typography.body1
    )
}

@Composable
private fun <T> List<T>.asList(
    withDividers: Boolean = false,
    itemBuilder: @Composable (T) -> Unit,
) = Column {
    forEachIndexed { index, item ->
        itemBuilder(item)
        if (withDividers && index < lastIndex) {
            EvaDivider()
        }
    }
}
I am currently getting warning message:
Composable functions that return Unit should start with an uppercase letter
. Is there a better way of creating compose functions to avoid getting this message?
m

Mark Murphy

05/30/2022, 11:40 AM
Presumably the warning is because
Column()
returns
Unit
, so your
asList()
extension function returns
Unit
. Your use of
=
masks that fact.
Is there a better way of creating compose functions to avoid getting this message?
Off the cuff, FWIW, and by eyeball, I would try:
Copy code
@Composable
private fun <T> CollectionColumn(
    collection: Collection<T>,
    divider: @Composable (() -> Unit)? = { EvaDivider() },
    itemBuilder: @Composable (T) -> Unit,
) {
    Column {
        collection.forEachIndexed { index, item ->
            itemBuilder(item)

            if (divider != null && index < lastIndex) {
                divider()
            }
        }
    }
}
AFAIK, there is nothing stopping you from using an
UpperCamelCase
function name on an extension function. However, I am not comfortable having an extension function of a model object type generate a composable UI. This implementation also pulls out
EvaDivider()
into a composable slot parameter, so your function is more flexible. Pass in
null
to say that you do not want a divider.
👆 3
h

Håkon Pettersen

05/30/2022, 2:19 PM
Thank you for the response, @Mark Murphy. Good point about the divider, will implement 🙂
2 Views