Skip to content

Commit

Permalink
clean up & add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jingwei99 committed Sep 26, 2023
1 parent 859c946 commit c22dd46
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 36 deletions.
1 change: 0 additions & 1 deletion redwood-lazylayout-compose/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ 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,18 +15,17 @@
*/
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 rememberSaveable(saver = LazyListState.Saver) {
return remember {
LazyListState(
initialFirstVisibleItemIndex,
)
Expand All @@ -47,17 +46,4 @@ 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,
)
}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,7 @@ private class RedwoodZiplineTreehouseUi(
)
}

override fun snapshotState(): StateSnapshot? {
// performSave is not picking up other values, why?
override fun snapshotState(): StateSnapshot {
val savedState = saveableStateRegistry.performSave()
return savedState.toStateSnapshot()
}
Expand Down
7 changes: 7 additions & 0 deletions redwood-treehouse/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ kotlin {
api libs.zipline
}
}

commonTest {
dependencies {
implementation libs.kotlin.test
implementation libs.assertk
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import kotlinx.serialization.json.intOrNull

@Serializable
public class StateSnapshot(
public val content: Map<String, List<@Contextual Saveable>>
public val content: Map<String, List<@Contextual Saveable>>,
) {
public fun toValuesMap(): Map<String, List<Any?>> {
return content.mapValues { entry ->
Expand All @@ -54,8 +54,8 @@ public class StateSnapshot(
*/
public fun Map<String, List<Any?>>.toStateSnapshot(): StateSnapshot = StateSnapshot(
mapValues { entry ->
entry.value.map {element ->
when(element) {
entry.value.map { element ->
when (element) {
is MutableState<*> -> Saveable(true, element.value.toJsonElement())
else -> Saveable(false, element.toJsonElement())
}
Expand Down Expand Up @@ -89,7 +89,7 @@ private fun JsonElement?.fromJsonElement(): Any {
}
}

public data class Saveable (
public data class Saveable(
val isMutableState: Boolean,
val value: JsonElement
val value: JsonElement,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package app.cash.redwood.treehouse

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import assertk.assertThat
import assertk.assertions.isEqualTo
import kotlin.test.Test
import kotlin.test.assertTrue
import kotlinx.serialization.json.JsonPrimitive

class StateSnapshotTest {

@Test
fun toValueMapWorksAsExpected() {
val stateSnapshot = stateSnapshot()
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["key2"]).isEqualTo(listOf(1.0))

assertThat(valuesMap["key3"]!![0] is MutableState<*>)
assertThat((valuesMap["key3"]!![0] as MutableState<*>).value).isEqualTo(JsonPrimitive("str"))

assertThat(valuesMap["key4"]).isEqualTo(listOf("str"))
}

@Test
fun toStateSnapshotWorksAsExpected() {
val storedStateSnapshot = storedStateSnapshot()
val stateSnapshot = storedStateSnapshot.toStateSnapshot()
assertThat(stateSnapshot.content["key1"]!![0]).isEqualTo(Saveable(true, JsonPrimitive(1)))
assertThat(stateSnapshot.content["key2"]!![0]).isEqualTo(Saveable(false, JsonPrimitive(1)))
assertThat(stateSnapshot.content["key3"]!![0]).isEqualTo(Saveable(true, JsonPrimitive("str")))
assertThat(stateSnapshot.content["key4"]!![0]).isEqualTo(Saveable(false, JsonPrimitive("str")))
}
private fun stateSnapshot() = StateSnapshot(
mapOf(
"key1" to listOf(Saveable(true, JsonPrimitive(1))),
"key2" to listOf(Saveable(false, JsonPrimitive(1))),
"key3" to listOf(Saveable(true, JsonPrimitive("str"))),
"key4" to listOf(Saveable(false, JsonPrimitive("str"))),
),
)

private fun storedStateSnapshot() = mapOf(
"key1" to listOf(mutableStateOf(1)),
"key2" to listOf(1),
"key3" to listOf(mutableStateOf("str")),
"key4" to listOf("str"),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ 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 @@ -97,9 +96,7 @@ 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 @@ -176,14 +173,7 @@ private fun NestedFlexBoxContainers(httpClient: HttpClient, navigator: Navigator
override fun SaverScope.save(value: TextFieldState) = value.text
}

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

LaunchedEffect(Unit) {
try {
Expand Down

0 comments on commit c22dd46

Please sign in to comment.