[ad_1]
As we speak I’ve created a useful Kotlin extension perform that I want to share with you. The extension perform is used along with Jetpack Compose when constructing a modifier for a Composable view.
Right here is the perform, which I named applyIf:
enjoyable Modifier.applyIf(
situation: Boolean,
modifierFunction: Modifier.() -> Modifier
): Modifier {
return this.run {
if (situation) {
this.modifierFunction()
} else {
this
}
}
}
Now the use case.
Think about in case you have a Composable, let’s say a Textual content, that must be clickable. To make a Textual content clickable in Compose, the clickable modifier component is used (Let’s additionally assume that the alpha component wants to return earlier than the clickable component and that the padding component wants to return after it):
Textual content(
modifier = Modifier
.alpha(0.8F)
.clickable { onSubmit() }
.padding(16.dp),
textual content = "Submit"
)
Now let’s say that the Textual content must be clickable provided that some situation is fulfilled. Then we might change our Textual content Composable to one thing like:
var modifier = Modifier.alpha(0.8F)
if (isReady) {
modifier = modifier.clickable { onSubmit() }
}Textual content(
modifier = modifier.padding(16.dp),
textual content = "Submit"
)
We’re making a modifier variable and altering it if wanted. The Textual content Composable will probably be clickable solely when isReady is true.
The identical factor may very well be achieved by utilizing Kotlin’s run scope perform as under:
Textual content(
modifier = Modifier
.alpha(0.8F)
.run {
if (isReady) {
this.clickable { onSubmit() }
} else {
this
}
}
.padding(16.dp),
textual content = "Submit"
)
As you will have seen, a separate modifier variable is now not wanted. We use the run scope perform and inside it we now have an if assertion which can set the modifier as clickable or will merely return the modifier unchanged.
If in case you have a number of Composables that must do the identical factor because the Textual content in our instance above, then you definitely would wish to put this run..if…else in each Composable. Which may make the code much less readable. To deal with that I made a decision to create an extension perform on Modifier, that can be utilized by such Composables. The perform is named applyIf:
enjoyable Modifier.applyIf(
situation: Boolean,
modifierFunction: Modifier.() -> Modifier
): Modifier {
return this.run {
if (situation) {
this.modifierFunction()
} else {
this
}
}
}
For our Textual content Composable instance, applyIf would then be used like:
Textual content(
modifier = Modifier
.alpha(0.8F)
.applyIf(isReady) {
this.clickable { onSubmit() }
}
.padding(16.dp),
textual content = "Submit"
)
The applyIf perform makes use of a cool Kotlin function known as perform literals with receivers. You may see that within the perform’s second argument:
modifierFunction: Modifier.() -> Modifier
In our case the receiver is a Modifier object and we’re calling the lambda perform modifierFunction() on that receiver object after which returning the up to date modifier.
Thanks for studying and I hope you can also make use of this perform in your initiatives!
[ad_2]
Source_link