From 17915ef5b55ec6dde9a7756c4ed9b8da30194afc Mon Sep 17 00:00:00 2001 From: Veyndan Stuart Date: Thu, 28 Sep 2023 14:08:48 +0200 Subject: [PATCH] Fix saving of `rememberSaveable` values (#1531) * Make `Saveable` serializable It was marked with `@Contextual`, but a serializer was never being passed in, therefore it crashes as it can't find a serializer. The class is trivial to serialize, so we can just annotate it with `@Serializable` for a serialization adapter to be generated for us. * When deserializing a `MutableState`, also deserialize its value --- .../kotlin/app/cash/redwood/treehouse/StateSnapshot.kt | 6 +++--- .../kotlin/app/cash/redwood/treehouse/StateSnapshotTest.kt | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/redwood-treehouse/src/commonMain/kotlin/app/cash/redwood/treehouse/StateSnapshot.kt b/redwood-treehouse/src/commonMain/kotlin/app/cash/redwood/treehouse/StateSnapshot.kt index fa0b2393a2..6faaefed49 100644 --- a/redwood-treehouse/src/commonMain/kotlin/app/cash/redwood/treehouse/StateSnapshot.kt +++ b/redwood-treehouse/src/commonMain/kotlin/app/cash/redwood/treehouse/StateSnapshot.kt @@ -18,7 +18,6 @@ package app.cash.redwood.treehouse import androidx.compose.runtime.MutableState import androidx.compose.runtime.mutableStateOf import kotlin.jvm.JvmInline -import kotlinx.serialization.Contextual import kotlinx.serialization.Serializable import kotlinx.serialization.json.JsonArray import kotlinx.serialization.json.JsonElement @@ -29,13 +28,13 @@ import kotlinx.serialization.json.intOrNull @Serializable public class StateSnapshot( - public val content: Map>, + public val content: Map>, ) { public fun toValuesMap(): Map> { return content.mapValues { entry -> entry.value.map { if (it.isMutableState) { - mutableStateOf(it.value) + mutableStateOf(it.value.fromJsonElement()) } else { it.value.fromJsonElement() } @@ -89,6 +88,7 @@ private fun JsonElement?.fromJsonElement(): Any { } } +@Serializable public data class Saveable( val isMutableState: Boolean, val value: JsonElement, diff --git a/redwood-treehouse/src/commonTest/kotlin/app/cash/redwood/treehouse/StateSnapshotTest.kt b/redwood-treehouse/src/commonTest/kotlin/app/cash/redwood/treehouse/StateSnapshotTest.kt index c25c93f534..8d1bfc3987 100644 --- a/redwood-treehouse/src/commonTest/kotlin/app/cash/redwood/treehouse/StateSnapshotTest.kt +++ b/redwood-treehouse/src/commonTest/kotlin/app/cash/redwood/treehouse/StateSnapshotTest.kt @@ -32,12 +32,12 @@ class StateSnapshotTest { val valuesMap = stateSnapshot.toValuesMap() assertThat(valuesMap.entries.size).isEqualTo(4) assertTrue(valuesMap["key1"]!![0] is MutableState<*>) - assertThat((valuesMap["key1"]!![0] as MutableState<*>).value).isEqualTo(JsonPrimitive(1)) + assertThat((valuesMap["key1"]!![0] as MutableState<*>).value).isEqualTo(1.0) assertThat(valuesMap["key2"]).isEqualTo(listOf(1.0)) assertThat(valuesMap["key3"]!![0] is MutableState<*>) - assertThat((valuesMap["key3"]!![0] as MutableState<*>).value).isEqualTo(JsonPrimitive("str")) + assertThat((valuesMap["key3"]!![0] as MutableState<*>).value).isEqualTo("str") assertThat(valuesMap["key4"]).isEqualTo(listOf("str")) }