jwstegemann / fritz2

Easily build reactive web-apps in Kotlin based on flows and coroutines.
https://www.fritz2.dev
MIT License
627 stars 25 forks source link

Add `renderTrue` and `renderFalse` convenience functions #857

Closed haukesomm closed 3 months ago

haukesomm commented 3 months ago

Motivation

With fritz1.0-RC8 we added a bunch of convenience-render-functions to reduce boilerplate-code within the ui-sections, such as renderIf({/*some boolean expression */}) { ... }. In many cases however, we have to work with boolean Flows which lead to code like this:

someBooleanFlow.renderIf({ it }) { 
    /* render UI */ 
}

This looks not as clean as it could.

Solution

This PR adds two convenience functions Flow<Boolean>.renderIfTrue(...) and Flow<Boolean>.renderIfFalse(...) that render a given content if a boolean Flow's value is true or false respectively.

Examples:

val store = storeOf(true)

store.data.renderIfTrue {
    // content is rendered if the Store's value is true.
    // If the Store's value is false, nothing is rendered.
}
val store = storeOf(false)

store.data.renderIfFalse {
    // content is rendered if the Store's value is false.
    // If the Store's value is true, nothing is rendered.
}