Skip to content

Instantly share code, notes, and snippets.

@antonshilov
Last active July 1, 2024 18:49
Show Gist options
  • Save antonshilov/ef8cd0a360a5cc0f823b2a4e85084720 to your computer and use it in GitHub Desktop.
Save antonshilov/ef8cd0a360a5cc0f823b2a4e85084720 to your computer and use it in GitHub Desktop.
Simple tree hierarchy rendering with Jetpack Compose
package com.example.myapplication
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.foundation.Box
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.ui.tooling.preview.Preview
@ExperimentalAnimationApi
@Composable
fun Node(
modifier: Modifier = Modifier,
nodeModel: NodeModel
) {
Column(
horizontalGravity = Alignment.CenterHorizontally
) {
val isChildrenShown = remember { mutableStateOf(true) }
NodeBox(
modifier = modifier.clickable(onClick = {
isChildrenShown.value = !isChildrenShown.value
}),
isExpanded = isChildrenShown.value
)
Spacer(modifier = Modifier.size(8.dp))
AnimatedVisibility(visible = isChildrenShown.value) {
Row(
modifier = modifier,
horizontalArrangement = Arrangement.Center
) {
nodeModel.children.forEachIndexed { index, model ->
Node(nodeModel = model)
if (index != nodeModel.children.size - 1) {
Spacer(modifier = Modifier.size(4.dp))
}
}
}
}
}
}
@Composable
fun NodeBox(modifier: Modifier = Modifier, isExpanded: Boolean) {
Box(
modifier = modifier.size(8.dp),
backgroundColor = if (isExpanded) Color.Green else Color.Blue
)
}
@ExperimentalAnimationApi
@Preview
@Composable
fun TreePreview() {
Node(
nodeModel = rootModel()
)
}
data class NodeModel(val children: List<NodeModel> = emptyList())
private fun rootModel(): NodeModel {
return NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
)
)
),
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
),
NodeModel(
listOf(
NodeModel(),
NodeModel(),
NodeModel(),
)
)
)
),
)
)
}
@handstandsam
Copy link

This is simple and looks nice, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment