-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support to save non-mutable int in StateSnapshot #1508
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,20 +18,28 @@ 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 | ||
import kotlinx.serialization.json.JsonPrimitive | ||
import kotlinx.serialization.json.booleanOrNull | ||
import kotlinx.serialization.json.doubleOrNull | ||
import kotlinx.serialization.json.intOrNull | ||
|
||
@Serializable | ||
public class StateSnapshot( | ||
public val content: Map<String, List<JsonElement?>>, | ||
public val content: Map<String, List<@Contextual Saveable>> | ||
) { | ||
public fun toValuesMap(): Map<String, List<Any?>>? { | ||
public fun toValuesMap(): Map<String, List<Any?>> { | ||
return content.mapValues { entry -> | ||
entry.value.map { mutableStateOf(it.fromJsonElement()) } | ||
entry.value.map { | ||
if (it.isMutableState) { | ||
mutableStateOf(it.value) | ||
} else { | ||
it.value.fromJsonElement() | ||
} | ||
Comment on lines
+37
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could remove the need for the |
||
} | ||
} | ||
} | ||
|
||
|
@@ -46,10 +54,10 @@ public class StateSnapshot( | |
*/ | ||
public fun Map<String, List<Any?>>.toStateSnapshot(): StateSnapshot = StateSnapshot( | ||
mapValues { entry -> | ||
entry.value.map { element -> | ||
when (element) { | ||
is MutableState<*> -> element.value.toJsonElement() | ||
else -> error("unexpected type: $this") | ||
entry.value.map {element -> | ||
when(element) { | ||
is MutableState<*> -> Saveable(true, element.value.toJsonElement()) | ||
else -> Saveable(false, element.toJsonElement()) | ||
} | ||
} | ||
}, | ||
|
@@ -58,6 +66,7 @@ public fun Map<String, List<Any?>>.toStateSnapshot(): StateSnapshot = StateSnaps | |
private fun Any?.toJsonElement(): JsonElement { | ||
return when (this) { | ||
is String -> JsonPrimitive(this) | ||
is Int -> JsonPrimitive(this) | ||
is List<*> -> JsonArray(map { it.toJsonElement() }) | ||
is JsonElement -> this | ||
else -> error("unexpected type: $this") | ||
|
@@ -68,11 +77,9 @@ private fun Any?.toJsonElement(): JsonElement { | |
private fun JsonElement?.fromJsonElement(): Any { | ||
return when (this) { | ||
is JsonPrimitive -> { | ||
if (this.isString) { | ||
return content | ||
} | ||
return booleanOrNull ?: doubleOrNull ?: error("unexpected type: $this") | ||
// TODO add other primitive types (double, float, long) when needed | ||
if (this.isString) { return content } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks a bit odd having the braces on a single line. Can we revert it, or do |
||
return booleanOrNull ?: doubleOrNull ?: intOrNull ?: error("unexpected type: $this") | ||
// TODO add other primitive types (float, long) when needed | ||
} | ||
|
||
is JsonArray -> listOf({ this.forEach { it.toJsonElement() } }) | ||
|
@@ -81,3 +88,8 @@ private fun JsonElement?.fromJsonElement(): Any { | |
else -> error("unexpected type: $this") | ||
} | ||
} | ||
|
||
public data class Saveable ( | ||
val isMutableState: Boolean, | ||
val value: JsonElement | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is copy-paste from #1494, and will be reverted once this PR goes from draft -> ready for review