Created
June 29, 2024 04:28
-
-
Save anioutkazharkova/e1fa1ba094745d3f9fddbef0344a013d to your computer and use it in GitHub Desktop.
Giga Code
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
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.layout.fillMaxWidth | |
import androidx.compose.foundation.layout.height | |
import androidx.compose.material3.MaterialTheme | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.layout.ContentScale | |
import androidx.compose.ui.unit.dp | |
import coil.compose.rememberImagePainter | |
import kotlinx.serialization.Serializable | |
import kotlinx.serialization.json.Json | |
enum class ElementType { | |
TOP_BAR, | |
CHILDREN, | |
IMAGE, | |
TEXT, | |
VERTICAL_LIST, | |
ROW, | |
SCAFFOLD, | |
CENTER_ALIGNED_TOP_APP_BAR | |
} | |
@Serializable | |
data class ServerDrivenUi( | |
val data: List<Data> | |
) | |
@Serializable | |
data class Data( | |
val type: ElementType, | |
val children: List<Children> | |
) | |
@Serializable | |
data class Children( | |
val type: ElementType, | |
val value: String? = null, | |
val children: List<Children>? = null | |
) | |
@Composable | |
fun ImagePlaceholder( | |
modifier: Modifier = Modifier, | |
contentDescription: String? = null, | |
imageUrl: String? = null, | |
) { | |
val painter = rememberImagePainter( | |
data = imageUrl, | |
builder = { | |
crossfade(true) | |
} | |
) | |
Image( | |
painter = painter, | |
contentDescription = contentDescription, | |
contentScale = ContentScale.Crop, | |
modifier = modifier | |
) | |
} | |
@Composable | |
fun ServerDrivenUi(json: String) { | |
val ui = Json.decodeFromString<ServerDrivenUi>(json) | |
// ... | |
} | |
@Composable | |
fun ServerDrivenUi(json: String) { | |
val ui = Json.decodeFromString<ServerDrivenUi>(json) | |
Scaffold( | |
topBar = { | |
CenterAlignedTopAppBar( | |
title = { Text(text = ui.data[0].children[0].value) } | |
) | |
} | |
) { padding -> | |
LazyColumn( | |
modifier = Modifier.padding(padding) | |
) { | |
items(ui.data) { data -> | |
when (data.type) { | |
ElementType.TOP_BAR -> { | |
TopBar(data = data) | |
} | |
ElementType.CHILDREN -> { | |
Children(data = data) | |
} | |
ElementType.IMAGE -> { | |
ImagePlaceholder( | |
modifier = Modifier | |
.fillMaxWidth() | |
.height(200.dp), | |
contentDescription = "Placeholder image", | |
imageUrl = data.children[0].value | |
) | |
} | |
ElementType.TEXT -> { | |
Text(text = data.children[0].value) | |
} | |
ElementType.VERTICAL_LIST -> { | |
VerticalList(data = data) | |
} | |
ElementType.ROW -> { | |
Row { | |
Children(data = data) | |
} | |
} | |
ElementType.SCAFFOLD -> { | |
// Do nothing | |
} | |
ElementType.CENTER_ALIGNED_TOP_APP_BAR -> { | |
// Do nothing | |
} | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun TopBar(data: Data) { | |
when (data.type) { | |
ElementType.CENTER_ALIGNED_TOP_APP_BAR -> { | |
CenterAlignedTopAppBar( | |
title = { Text(text = data.children[0].value!!) } | |
) | |
} | |
else -> {} | |
} | |
} | |
@Composable | |
fun Children(data: Data) { | |
when (data.type) { | |
ElementType.TEXT -> { | |
Text(text = data.children[0].value!!) | |
} | |
ElementType.IMAGE -> { | |
ImagePlaceholder( | |
modifier = Modifier | |
.fillMaxWidth() | |
.height(200.dp), | |
contentDescription = "Placeholder image", | |
imageUrl = data.children[0].value!! | |
) | |
} | |
else -> {} | |
} | |
} | |
@Composable | |
fun VerticalList(data: Data) { | |
LazyColumn { | |
items(data.children) { child -> | |
Row { | |
Children(data = child) | |
} | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment