Skip to content

Commit

Permalink
Simplify creation of new screens (#1525)
Browse files Browse the repository at this point in the history
This also changes from an enum key to a string key so that we can switch to rememberSaveable soon.
  • Loading branch information
JakeWharton authored Sep 28, 2023
1 parent c0abb5a commit 89750b1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -53,7 +54,8 @@ fun main() {
response.text().await()
}

val context = TestContext(client)
composition.setContent {
TestApp(client)
TestApp(context)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,62 +31,60 @@ import app.cash.redwood.ui.dp
import com.example.redwood.testing.compose.Button
import com.example.redwood.testing.compose.Text

private val screens = buildMap<String, @Composable TestContext.() -> 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<Screen?>(null) }
val activeScreen = screen.value
if (activeScreen == null) {
HomeScreen(screen)
fun TestApp(context: TestContext) {
val screenKeyState = remember { mutableStateOf<String?>(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<Screen?>) {
private fun ScreenList(screen: MutableState<String?>) {
Column(
width = Fill,
height = Fill,
overflow = Overflow.Scroll,
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
})
}
}
Expand Down

0 comments on commit 89750b1

Please sign in to comment.