Skip to content

Instantly share code, notes, and snippets.

@blakelee
Last active March 3, 2022 06:08
Show Gist options
  • Select an option

  • Save blakelee/ac3240b54ecf5b41d5f1ec0c123734fa to your computer and use it in GitHub Desktop.

Select an option

Save blakelee/ac3240b54ecf5b41d5f1ec0c123734fa to your computer and use it in GitHub Desktop.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>
@Composable
fun <T : Any> Table(
items: List<T>,
columns: Int,
content: @Composable (T, Modifier) -> Unit,
onClick: (T, Boolean) -> Unit
) {
val chunked = items.chunked(columns)
val remainder = items.size % columns
val selected = remember { List(items.size) { mutableStateOf(false) } }
Column(modifier = Modifier.fillMaxWidth()) {
chunked.forEachIndexed { columnIndex, list ->
Row(
modifier = Modifier
.fillMaxWidth()
.height(IntrinsicSize.Min)
.width(IntrinsicSize.Max)
) {
list.forEachIndexed { rowIndex, item ->
val cellIndex = (columns * columnIndex) + rowIndex
val curSelected = selected[cellIndex]
val isLast = curSelected == selected.last()
val isCellBelow = cellIndex + columns < items.size
content(
item,
Modifier
.weight(1 / columns.toFloat())
.selectableCell(
selected = curSelected.value,
left = rowIndex == 0,
top = columnIndex == 0,
right = rowIndex == columns - 1 || isLast,
bottom = !isCellBelow,
onClick = {
curSelected.value = !curSelected.value
onClick(item, curSelected.value)
}
)
)
if (isLast) {
Box(
modifier = Modifier
.weight((columns - remainder) / columns.toFloat())
)
}
}
}
}
}
}
fun Modifier.selectableCell(
selected: Boolean,
enabled: Boolean = true,
role: Role? = null,
left: Boolean = false,
top: Boolean = false,
right: Boolean = false,
bottom: Boolean = false,
onClick: () -> Unit
): Modifier = composed(
factory = {
val color = if (selected) Color.Green else Color.Red
fun ContentDrawScope.drawLine(startX: Float, startY: Float, endX: Float, endY: Float) {
drawLine(
SolidColor(color),
Offset(startX, startY),
Offset(endX, endY),
strokeWidth = 1.dp.toPx(),
cap = StrokeCap.Square
)
}
selectable(
selected = selected,
enabled = enabled,
role = role,
onClick = onClick
)
.graphicsLayer(clip = false)
.drawWithCache {
onDrawWithContent {
drawContent()
// Draw top
drawLine(0f, 1.dp.toPx(), size.width, 1.dp.toPx())
if (top) {
val leftMod = if (left) -1.dp.toPx() else 0f
drawLine(leftMod, 0f, size.width, 0f)
}
// Draw bottom
drawLine(0f, size.height, size.width, size.height)
if (bottom) {
val modLeft = if (left) -1.dp.toPx() else 0f
drawLine(modLeft, size.height + 1.dp.toPx(), size.width, size.height + 1.dp.toPx())
}
// Draw left
drawLine(0f, 1.dp.toPx(), 0f, size.height)
if (left) {
drawLine(-1.dp.toPx(), 1.dp.toPx(), -1.dp.toPx(), size.height)
}
// Draw right
drawLine(size.width - 1.dp.toPx(), 1.dp.toPx(), size.width -1.dp.toPx(), size.height)
if (right) {
drawLine(size.width, 1.dp.toPx(), size.width, size.height)
}
}
}
}
)
@Preview(backgroundColor = 0xF, showBackground = true, showSystemUi = true)
@Composable
fun TableTest() {
val items = listOf(1, 2, 3, 4, 5, 6, 7, 8)
Box (modifier = Modifier.padding(4.dp)) {
Table(items, 3, { item, modifier ->
Text(
item.toString(),
modifier.height(80.dp)
)
}, { item, selected -> })
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment