From 9efe1d11fa525796915e1bccfc88417b6c13a0f4 Mon Sep 17 00:00:00 2001 From: sychic <47618543+Sychic@users.noreply.github.com> Date: Thu, 11 Jul 2024 13:53:51 -0700 Subject: [PATCH] layoutdsl: migrate `state.kt` to statev2 Source-Commit: 1a0d46aae2a774b0f9fea758ac20b51f735dfe9d Source-Commit: a4c22b3ca1c4072cc8ee4818fbea0c63f6e7ef2a --- .../kotlin/gg/essential/elementa/layoutdsl/state.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/unstable/layoutdsl/src/main/kotlin/gg/essential/elementa/layoutdsl/state.kt b/unstable/layoutdsl/src/main/kotlin/gg/essential/elementa/layoutdsl/state.kt index bdfe0726..93039702 100644 --- a/unstable/layoutdsl/src/main/kotlin/gg/essential/elementa/layoutdsl/state.kt +++ b/unstable/layoutdsl/src/main/kotlin/gg/essential/elementa/layoutdsl/state.kt @@ -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") @@ -24,12 +26,12 @@ fun Modifier.then(state: State): Modifier { } fun Modifier.then(state: StateV2): 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) }; { @@ -43,7 +45,7 @@ fun Modifier.then(state: StateV2): Modifier { @Suppress("DeprecatedCallableAddReplaceWith") @Deprecated("Using StateV1 is discouraged, use StateV2 instead") fun Modifier.whenTrue(state: State, 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, activeModifier: Modifier, inactiveModifier: Modifier = Modifier): Modifier = then(state.map { if (it) activeModifier else inactiveModifier }) \ No newline at end of file