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

Move debugging layouts to their own files #1593

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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 com.example.redwood.emojisearch.presenter
import androidx.compose.runtime.Composable
import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
import app.cash.redwood.layout.api.MainAxisAlignment
import app.cash.redwood.layout.compose.Column

@Composable
fun BuggyNestedColumns() {
// Full-screen parent column
Column(
width = Constraint.Fill,
height = Constraint.Fill,
horizontalAlignment = CrossAxisAlignment.Center,
) {
// Header
Column(
width = Constraint.Fill,
horizontalAlignment = CrossAxisAlignment.Center,
) {
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
}
// Body (sibling of Header)
// Given a height with a Fill constraint, and a Center alignment,
// the Item should be vertically cenetered in the remaining space, but isn't.
// Instead, the content is effectively pushed down by the height of the Header.
Column(
horizontalAlignment = CrossAxisAlignment.Center,
width = Constraint.Fill,
height = Constraint.Fill,
verticalAlignment = MainAxisAlignment.Center,
) {
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import app.cash.redwood.compose.LocalUiConfiguration
import app.cash.redwood.layout.api.Constraint
import app.cash.redwood.layout.api.CrossAxisAlignment
import app.cash.redwood.layout.api.MainAxisAlignment
import app.cash.redwood.layout.api.Overflow
import app.cash.redwood.layout.compose.Column
import app.cash.redwood.layout.compose.Row
import app.cash.redwood.layout.compose.flex
Expand All @@ -47,7 +46,7 @@ import com.example.redwood.emojisearch.compose.TextInput
import example.values.TextFieldState
import kotlinx.serialization.json.Json

private data class EmojiImage(
data class EmojiImage(
val label: String,
val url: String,
)
Expand Down Expand Up @@ -75,8 +74,8 @@ fun EmojiSearch(
) {
when (variant) {
Variant.LAZY_COLUMN -> LazyColumn(httpClient, navigator)
Variant.SCROLLABLE_FLEXBOX -> NestedFlexBoxContainers(httpClient, navigator)
Variant.BUGGY_COLUMNS -> BuggyNestedColumns(httpClient, navigator)
Variant.SCROLLABLE_FLEXBOX -> NestedFlexBoxContainers(httpClient)
Variant.BUGGY_COLUMNS -> BuggyNestedColumns()
Comment on lines +77 to +78
Copy link
Author

Choose a reason for hiding this comment

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

moved these two to their own files

I was working on this file (EmojiSearch.kt) and realized there are too many things going on and I got distracted 😥

}
}

Expand Down Expand Up @@ -165,159 +164,7 @@ private fun LazyColumn(
}

@Composable
private fun NestedFlexBoxContainers(httpClient: HttpClient, navigator: Navigator) {
val allEmojis = remember { mutableStateListOf<EmojiImage>() }

val searchTermSaver = object : Saver<TextFieldState, String> {
override fun restore(value: String) = TextFieldState(value)
override fun SaverScope.save(value: TextFieldState) = value.text
}

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

LaunchedEffect(Unit) {
try {
val emojisJson = httpClient.call(
url = "https://api.github.com/emojis",
headers = mapOf("Accept" to "application/vnd.github.v3+json"),
)
val labelToUrl = Json.decodeFromString<Map<String, String>>(emojisJson)

allEmojis.clear()
allEmojis.addAll(labelToUrl.map { (key, value) -> EmojiImage(key, value) })
} catch (e: Exception) {
println("Failed to load https://api.github.com/emojis $e")
}
}

val filteredEmojis by derivedStateOf {
val searchTerms = searchTerm.text.split(" ")
allEmojis.filter { image ->
searchTerms.all { image.label.contains(it, ignoreCase = true) }
}
}

Column(
width = Constraint.Fill,
height = Constraint.Fill,
overflow = Overflow.Clip,
horizontalAlignment = CrossAxisAlignment.Stretch,
margin = LocalUiConfiguration.current.safeAreaInsets,
verticalAlignment = MainAxisAlignment.Start,
) {
TextInput(
state = TextFieldState(searchTerm.text),
hint = "Search",
onChange = { searchTerm = it },
modifier = Modifier.shrink(0.0),
)

if (filteredEmojis.count() > 0) {
Text(
text = "Scroll Column - Nested Scroll Row + B Emojis",
modifier = Modifier.margin(Margin(12.dp)),
)
Column(
width = Constraint.Fill,
height = Constraint.Fill,
overflow = Overflow.Scroll,
horizontalAlignment = CrossAxisAlignment.Stretch,
modifier = Modifier.shrink(1.0),
) {
Text(
text = "Scroll Row - A Emojis",
modifier = Modifier.margin(Margin(12.dp)),
)
Row(
width = Constraint.Wrap,
height = Constraint.Wrap,
overflow = Overflow.Scroll,
verticalAlignment = CrossAxisAlignment.Center,
) {
val filtered = filteredEmojis.filter { it.label.startsWith("a") }.take(30)
filtered.forEach { image ->
Item(image)
}
}
filteredEmojis.filter { it.label.startsWith("b") }.take(30).forEach { image ->
Item(image)
}
}
Text(
text = "Scroll FlexRow - People Emojis",
modifier = Modifier.margin(Margin(12.dp)),
)
Row(
width = Constraint.Wrap,
height = Constraint.Wrap,
overflow = Overflow.Scroll,
verticalAlignment = CrossAxisAlignment.Center,
) {
val filtered =
filteredEmojis.filter { it.label.contains(Regex("man|woman|person")) }.take(30)
filtered.forEach { image ->
Item(image)
}
}
} else {
Text(
text = "Empty",
modifier = Modifier.margin(Margin(12.dp)),
)
}
}
}

@Composable
private fun BuggyNestedColumns(httpClient: HttpClient, navigator: Navigator) {
// Full-screen parent column
Column(
width = Constraint.Fill,
height = Constraint.Fill,
horizontalAlignment = CrossAxisAlignment.Center,
) {
// Header
Column(
width = Constraint.Fill,
horizontalAlignment = CrossAxisAlignment.Center,
) {
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
}
// Body (sibling of Header)
// Given a height with a Fill constraint, and a Center alignment,
// the Item should be vertically cenetered in the remaining space, but isn't.
// Instead, the content is effectively pushed down by the height of the Header.
Column(
horizontalAlignment = CrossAxisAlignment.Center,
width = Constraint.Fill,
height = Constraint.Fill,
verticalAlignment = MainAxisAlignment.Center,
) {
Item(
emojiImage = loadingEmojiImage,
onClick = {},
)
}
}
}

@Composable
private fun Item(
fun Item(
emojiImage: EmojiImage,
onClick: () -> Unit = {},
) {
Expand All @@ -337,7 +184,7 @@ private fun Item(
}
}

private val loadingEmojiImage = EmojiImage(
val loadingEmojiImage = EmojiImage(
label = "loading…",
url = "https://github.githubassets.com/images/icons/emoji/unicode/231a.png?v8",
)
Loading