https://kotlinlang.org logo
#codereview
Title
# codereview
c

chanjungskim

03/24/2023, 2:25 AM
Is there anybody who can review my code? it's about modifier and I want to simplify it when it's complicated. https://codereview.stackexchange.com/questions/284140/how-can-i-simplify-this-modifier
m

Marcello Galhardo

03/25/2023, 4:57 PM
I don’t have context to give you a meaningful answer, but if and only if the only thing you want is an alternative way to write that bit of code, you likely can do a conditional modifier extension:
Copy code
fun Modifier.thenIf(
  predicate: Boolean,
  block: () -> Modifier,
) = then(if (predicate) block() else this)
And now you can rewrite that code as such:
Copy code
Modifier
    .thenIf(label != null) {
        Modifier
            .padding(8.dp)
            .semantics(mergeDescendants = true) {
                // no-op
            }
    }
    .background(colors.backgroundColor(enabled).value, shape)
    .defaultMinSize(
        minWidth = 120.dp,
        minHeight = 24.dp
    )
That isn’t much better than the
apply
+
if
you did - if you are willing to give more details, maybe we can find an alternative solution.
c

chanjungskim

03/27/2023, 2:14 AM
@Marcello Galhardo I updated my question.
j

Jan

04/12/2023, 9:42 PM
Be aware that conditions like that make it really hard for compose to optimize modifier recompositions. Tbf most of the time this shouldn't matter.
c

chanjungskim

04/13/2023, 5:13 AM
Well,.. I don't get it.. do you mean, should I take the condition out and make them remember?
j

Jan

04/13/2023, 8:47 AM
5 Views