Skip to content
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

Merged
merged 3 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions redwood-lazylayout-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kotlin {
commonMain {
dependencies {
api projects.redwoodLazylayoutWidget
implementation libs.jetbrains.compose.runtime.saveable
}
}
commonTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@
*/
package app.cash.redwood.lazylayout.compose

import androidx.compose.runtime.saveable.Saver
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue

@Composable
public fun rememberLazyListState(
initialFirstVisibleItemIndex: Int = 0,
): LazyListState {
return remember {
return rememberSaveable(saver = LazyListState.Saver) {
LazyListState(
initialFirstVisibleItemIndex,
)
Expand All @@ -46,4 +47,17 @@ public class LazyListState(
firstVisibleItemIndex = index
scrollToItemTriggeredId++
}
public companion object {
/**
* The default [Saver] implementation for [LazyListState].
*/
public val Saver: Saver<LazyListState, *> = Saver(
save = { it.firstVisibleItemIndex },
restore = {
LazyListState(
firstVisibleItemIndex = it,
)
}
)
}
Copy link
Author

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ private class RedwoodZiplineTreehouseUi(
}

override fun snapshotState(): StateSnapshot? {
// performSave is not picking up other values, why?
val savedState = saveableStateRegistry.performSave()
return savedState.toStateSnapshot()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could remove the need for the Saveable wrapper class if we wrote an adapter to convert between MutableState and JsonElement. Perhaps it isn't worth the effort though, considering we'll change up this mechanism in #1384.

}
}
}

Expand All @@ -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())
}
}
},
Expand All @@ -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")
Expand All @@ -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 }
Copy link
Contributor

Choose a reason for hiding this comment

The 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 if (this.isString) return content?

return booleanOrNull ?: doubleOrNull ?: intOrNull ?: error("unexpected type: $this")
// TODO add other primitive types (float, long) when needed
}

is JsonArray -> listOf({ this.forEach { it.toJsonElement() } })
Expand All @@ -81,3 +88,8 @@ private fun JsonElement?.fromJsonElement(): Any {
else -> error("unexpected type: $this")
}
}

public data class Saveable (
val isMutableState: Boolean,
val value: JsonElement
)
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import app.cash.redwood.layout.compose.Column
import app.cash.redwood.layout.compose.Row
import app.cash.redwood.lazylayout.compose.ExperimentalRedwoodLazyLayoutApi
import app.cash.redwood.lazylayout.compose.LazyColumn
import app.cash.redwood.lazylayout.compose.LazyListState
import app.cash.redwood.lazylayout.compose.items
import app.cash.redwood.lazylayout.compose.rememberLazyListState
import app.cash.redwood.ui.Margin
Expand Down Expand Up @@ -96,7 +97,9 @@ private fun LazyColumn(
override fun SaverScope.save(value: TextFieldState) = value.text
}

var searchTerm by rememberSaveable(stateSaver = searchTermSaver) { mutableStateOf(TextFieldState("")) }
var searchTerm by rememberSaveable(stateSaver = searchTermSaver) {
mutableStateOf(TextFieldState(""))
}

LaunchedEffect(refreshSignal) {
try {
Expand Down Expand Up @@ -173,7 +176,14 @@ private fun NestedFlexBoxContainers(httpClient: HttpClient, navigator: Navigator
override fun SaverScope.save(value: TextFieldState) = value.text
}

var searchTerm by rememberSaveable(stateSaver = searchTermSaver) { mutableStateOf(TextFieldState("")) }
var searchTerm by rememberSaveable(stateSaver = searchTermSaver) {
error("boom")
mutableStateOf(TextFieldState(""))
}
var searchTerm1 by rememberSaveable(stateSaver = searchTermSaver) {
error("boom")
mutableStateOf(TextFieldState("hhh"))
}

LaunchedEffect(Unit) {
try {
Expand Down
Loading