Rework Modifier.applyIf.

It was using `Modifier.composed` which is not good for performance and detekt is warning about this.
This commit is contained in:
Benoit Marty 2024-04-03 11:34:49 +02:00 committed by Benoit Marty
parent cea03085c3
commit 7b70cac7c5
2 changed files with 21 additions and 22 deletions

View file

@ -87,10 +87,13 @@ internal fun RoomListSearchView(
) { ) {
Column( Column(
modifier = modifier modifier = modifier
.applyIf(state.isSearchActive, ifTrue = { .applyIf(
// Disable input interaction to underlying views condition = state.isSearchActive,
pointerInput(Unit) {} ifTrue = {
}) // Disable input interaction to underlying views
pointerInput(Unit) {}
}
)
) { ) {
if (state.isSearchActive) { if (state.isSearchActive) {
RoomListSearchContent( RoomListSearchContent(

View file

@ -16,30 +16,26 @@
package io.element.android.libraries.designsystem.modifiers package io.element.android.libraries.designsystem.modifiers
import android.annotation.SuppressLint
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.composed
import androidx.compose.ui.platform.debugInspectorInfo import androidx.compose.ui.platform.debugInspectorInfo
import androidx.compose.ui.platform.inspectable
/** /**
* Applies the [ifTrue] modifier when the [condition] is true, [ifFalse] otherwise. * Applies the [ifTrue] modifier when the [condition] is true, [ifFalse] otherwise.
*/ */
@SuppressLint("UnnecessaryComposedModifier") // It's actually necessary due to the `@Composable` lambdas
fun Modifier.applyIf( fun Modifier.applyIf(
condition: Boolean, condition: Boolean,
ifTrue: @Composable Modifier.() -> Modifier, ifTrue: Modifier.() -> Modifier,
ifFalse: @Composable (Modifier.() -> Modifier)? = null ifFalse: (Modifier.() -> Modifier)? = null
): Modifier = ): Modifier = this then inspectable(
composed( inspectorInfo = debugInspectorInfo {
inspectorInfo = debugInspectorInfo { name = "applyIf"
name = "applyIf" value = condition
value = condition
}
) {
when {
condition -> then(ifTrue(Modifier))
ifFalse != null -> then(ifFalse(Modifier))
else -> this
}
} }
) {
this then when {
condition -> ifTrue(Modifier)
ifFalse != null -> ifFalse(Modifier)
else -> Modifier
}
}