From 55569b97c83200f0b7f4601caa700f3d32a6faf6 Mon Sep 17 00:00:00 2001 From: Jake Wharton Date: Wed, 27 Sep 2023 20:29:59 -0400 Subject: [PATCH] Simplify creation of new screens This also changes from an enum key to a string key so that we can switch to rememberSaveable soon. --- .../example/redwood/testing/browser/main.kt | 4 +- .../testing/treehouse/TestAppTreehouseUi.kt | 7 +- .../redwood/testing/presenter/TestApp.kt | 73 +++++++++---------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/test-app/browser/src/commonMain/kotlin/com/example/redwood/testing/browser/main.kt b/test-app/browser/src/commonMain/kotlin/com/example/redwood/testing/browser/main.kt index 4b936f07b9..e13a94b673 100644 --- a/test-app/browser/src/commonMain/kotlin/com/example/redwood/testing/browser/main.kt +++ b/test-app/browser/src/commonMain/kotlin/com/example/redwood/testing/browser/main.kt @@ -22,6 +22,7 @@ import app.cash.redwood.lazylayout.dom.HTMLElementRedwoodLazyLayoutWidgetFactory import app.cash.redwood.widget.asRedwoodView import com.example.redwood.testing.presenter.HttpClient import com.example.redwood.testing.presenter.TestApp +import com.example.redwood.testing.presenter.TestContext import com.example.redwood.testing.widget.TestSchemaWidgetFactories import kotlin.js.json import kotlinx.browser.document @@ -53,7 +54,8 @@ fun main() { response.text().await() } + val context = TestContext(client) composition.setContent { - TestApp(client) + TestApp(context) } } diff --git a/test-app/presenter-treehouse/src/jsMain/kotlin/com/example/redwood/testing/treehouse/TestAppTreehouseUi.kt b/test-app/presenter-treehouse/src/jsMain/kotlin/com/example/redwood/testing/treehouse/TestAppTreehouseUi.kt index b1407f0e59..18adad4944 100644 --- a/test-app/presenter-treehouse/src/jsMain/kotlin/com/example/redwood/testing/treehouse/TestAppTreehouseUi.kt +++ b/test-app/presenter-treehouse/src/jsMain/kotlin/com/example/redwood/testing/treehouse/TestAppTreehouseUi.kt @@ -19,12 +19,15 @@ import androidx.compose.runtime.Composable import app.cash.redwood.treehouse.TreehouseUi import com.example.redwood.testing.presenter.HttpClient import com.example.redwood.testing.presenter.TestApp +import com.example.redwood.testing.presenter.TestContext class TestAppTreehouseUi( - private val httpClient: HttpClient, + httpClient: HttpClient, ) : TreehouseUi { + private val context = TestContext(httpClient) + @Composable override fun Show() { - TestApp(httpClient) + TestApp(context) } } diff --git a/test-app/presenter/src/commonMain/kotlin/com/example/redwood/testing/presenter/TestApp.kt b/test-app/presenter/src/commonMain/kotlin/com/example/redwood/testing/presenter/TestApp.kt index eea7b5b9d6..652d7d4073 100644 --- a/test-app/presenter/src/commonMain/kotlin/com/example/redwood/testing/presenter/TestApp.kt +++ b/test-app/presenter/src/commonMain/kotlin/com/example/redwood/testing/presenter/TestApp.kt @@ -17,6 +17,7 @@ package com.example.redwood.testing.presenter import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState +import androidx.compose.runtime.Stable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import app.cash.redwood.Modifier @@ -30,52 +31,50 @@ import app.cash.redwood.ui.dp import com.example.redwood.testing.compose.Button import com.example.redwood.testing.compose.Text +private val screens = buildMap Unit> { + put("Repo Search") { RepoSearch(httpClient) } + put("UI Configuration") { UiConfigurationValues() } +} + +@Stable +class TestContext( + val httpClient: HttpClient, +) + @Composable -fun TestApp(httpClient: HttpClient) { - val screen = remember { mutableStateOf(null) } - val activeScreen = screen.value - if (activeScreen == null) { - HomeScreen(screen) +fun TestApp(context: TestContext) { + val screenKeyState = remember { mutableStateOf(null) } + val screenKey = screenKeyState.value + if (screenKey == null) { + ScreenList(screenKeyState) } else { - val onBack = { screen.value = null } + val onBack = { screenKeyState.value = null } BackHandler(onBack = onBack) + Column(width = Fill, height = Fill) { Button("Back", onClick = onBack) - // TODO This should be a Box. - Column( - width = Fill, - horizontalAlignment = Stretch, - modifier = Modifier.grow(1.0).horizontalAlignment(Stretch), - ) { - activeScreen.Show(httpClient) + val content = screens[screenKey] + if (content == null) { + Text("No screen found with key '$screenKey'!") + } else { + // TODO This should be a Box. + Column( + width = Fill, + horizontalAlignment = Stretch, + modifier = Modifier.grow(1.0).horizontalAlignment(Stretch), + ) { + with(context) { + content() + } + } } } } } -@Suppress("unused") // Used via reflection. -enum class Screen { - RepoSearch { - @Composable - override fun Show(httpClient: HttpClient) { - RepoSearch(httpClient) - } - }, - UiConfiguration { - @Composable - override fun Show(httpClient: HttpClient) { - UiConfigurationValues() - } - }, - ; - - @Composable - abstract fun Show(httpClient: HttpClient) -} - @Composable -private fun HomeScreen(screen: MutableState) { +private fun ScreenList(screen: MutableState) { Column( width = Fill, height = Fill, @@ -83,9 +82,9 @@ private fun HomeScreen(screen: MutableState) { horizontalAlignment = Stretch, ) { Text("Test App Screens:", modifier = Modifier.margin(Margin(8.dp))) - Screen.entries.forEach { - Button(it.name, onClick = { - screen.value = it + for (key in screens.keys) { + Button(key, onClick = { + screen.value = key }) } }