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:
committed by
Benoit Marty
parent
cea03085c3
commit
7b70cac7c5
@@ -87,10 +87,13 @@ internal fun RoomListSearchView(
|
||||
) {
|
||||
Column(
|
||||
modifier = modifier
|
||||
.applyIf(state.isSearchActive, ifTrue = {
|
||||
// Disable input interaction to underlying views
|
||||
pointerInput(Unit) {}
|
||||
})
|
||||
.applyIf(
|
||||
condition = state.isSearchActive,
|
||||
ifTrue = {
|
||||
// Disable input interaction to underlying views
|
||||
pointerInput(Unit) {}
|
||||
}
|
||||
)
|
||||
) {
|
||||
if (state.isSearchActive) {
|
||||
RoomListSearchContent(
|
||||
|
||||
@@ -16,30 +16,26 @@
|
||||
|
||||
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.composed
|
||||
import androidx.compose.ui.platform.debugInspectorInfo
|
||||
import androidx.compose.ui.platform.inspectable
|
||||
|
||||
/**
|
||||
* 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(
|
||||
condition: Boolean,
|
||||
ifTrue: @Composable Modifier.() -> Modifier,
|
||||
ifFalse: @Composable (Modifier.() -> Modifier)? = null
|
||||
): Modifier =
|
||||
composed(
|
||||
inspectorInfo = debugInspectorInfo {
|
||||
name = "applyIf"
|
||||
value = condition
|
||||
}
|
||||
) {
|
||||
when {
|
||||
condition -> then(ifTrue(Modifier))
|
||||
ifFalse != null -> then(ifFalse(Modifier))
|
||||
else -> this
|
||||
}
|
||||
ifTrue: Modifier.() -> Modifier,
|
||||
ifFalse: (Modifier.() -> Modifier)? = null
|
||||
): Modifier = this then inspectable(
|
||||
inspectorInfo = debugInspectorInfo {
|
||||
name = "applyIf"
|
||||
value = condition
|
||||
}
|
||||
) {
|
||||
this then when {
|
||||
condition -> ifTrue(Modifier)
|
||||
ifFalse != null -> ifFalse(Modifier)
|
||||
else -> Modifier
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user