Skip to content

Commit

Permalink
layoutdsl: migrate state.kt to statev2
Browse files Browse the repository at this point in the history
Source-Commit: 1a0d46aae2a774b0f9fea758ac20b51f735dfe9d
Source-Commit: a4c22b3ca1c4072cc8ee4818fbea0c63f6e7ef2a
  • Loading branch information
Sychic authored and Johni0702 committed Aug 12, 2024
1 parent e076bf8 commit 9efe1d1
Showing 1 changed file with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package gg.essential.elementa.layoutdsl
import gg.essential.elementa.state.State
import gg.essential.elementa.common.onSetValueAndNow
import gg.essential.elementa.state.v2.combinators.map
import gg.essential.elementa.state.v2.effect
import gg.essential.elementa.state.v2.toV2
import gg.essential.elementa.state.v2.State as StateV2

@Deprecated("Using StateV1 is discouraged, use StateV2 instead")
Expand All @@ -24,12 +26,12 @@ fun Modifier.then(state: State<Modifier>): Modifier {
}

fun Modifier.then(state: StateV2<Modifier>): Modifier {
return this then {
var reverse: (() -> Unit)? = state.get().applyToComponent(this)
return this then component@{
var reverse: (() -> Unit)? = null

val cleanupState = state.onSetValue(this) {
val cleanupState = effect(this) {
reverse?.invoke()
reverse = it.applyToComponent(this)
reverse = state().applyToComponent(this@component)
};

{
Expand All @@ -43,7 +45,7 @@ fun Modifier.then(state: StateV2<Modifier>): Modifier {
@Suppress("DeprecatedCallableAddReplaceWith")
@Deprecated("Using StateV1 is discouraged, use StateV2 instead")
fun Modifier.whenTrue(state: State<Boolean>, activeModifier: Modifier, inactiveModifier: Modifier = Modifier): Modifier =
then(state.map { if (it) activeModifier else inactiveModifier })
then(state.toV2().map { if (it) activeModifier else inactiveModifier })

fun Modifier.whenTrue(state: StateV2<Boolean>, activeModifier: Modifier, inactiveModifier: Modifier = Modifier): Modifier =
then(state.map { if (it) activeModifier else inactiveModifier })

0 comments on commit 9efe1d1

Please sign in to comment.