-
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
Simplify creation of new screens #1525
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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,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'!") | ||
Comment on lines
+58
to
+59
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. I thought about automatically falling back to home, but if we get |
||
} 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 | ||
}) | ||
} | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 will become a context receiver eliminating the need to explicitly pass any values in the lambdas above.