Skip to content

Commit

Permalink
Merge pull request #52 from kosenda/fix/reverse-row-and-column
Browse files Browse the repository at this point in the history
fix: reverse row column and rename id to index
  • Loading branch information
kosenda authored Jan 6, 2025
2 parents ec020a2 + 76d99a5 commit 220184c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 91 deletions.
51 changes: 15 additions & 36 deletions app/src/main/java/ksnd/autosizetable/SampleScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import ksnd.autosizetable.ui.theme.AutoSizeTableTheme

private const val NUM_OF_ITEMS_IN_EACH_COLUMN = 30
private const val NUM_OF_ITEMS_IN_EACH_ROW = 20
private const val NUM_OF_ITEMS_IN_EACH_COLUMN = 20
private const val NUM_OF_ITEMS_IN_EACH_ROW = 30
private const val MAX_NUM_OF_MAIL_ICONS = 5

@Composable
Expand All @@ -36,7 +35,7 @@ fun SampleScreen() {
var numMailRowIcons by remember { mutableIntStateOf(1) }
var numMailColumnIcons by remember { mutableIntStateOf(1) }

// fixedTopSize to fixedStartSize
// fixedTopSize to fixedStartSize (Row to Column)
val type = listOf(
1 to 1,
1 to 0,
Expand All @@ -50,32 +49,23 @@ fun SampleScreen() {
color = MaterialTheme.colorScheme.background,
) {
Column {
Row(
modifier = Modifier
.horizontalScroll(rememberScrollState()),
) {
Row(modifier = Modifier.horizontalScroll(rememberScrollState())) {
Button(
onClick = {
typeIndex = (typeIndex + 1) % type.size
},
onClick = { typeIndex = (typeIndex + 1) % type.size },
modifier = Modifier.padding(start = 8.dp, top = 8.dp),
) {
Text("Switch fixed size")
}

Button(
onClick = {
numMailRowIcons = (numMailRowIcons) % MAX_NUM_OF_MAIL_ICONS + 1
},
onClick = { numMailRowIcons = (numMailRowIcons) % MAX_NUM_OF_MAIL_ICONS + 1 },
modifier = Modifier.padding(start = 8.dp, top = 8.dp),
) {
Text("Switch num of row mail icons")
}

Button(
onClick = {
numMailColumnIcons = (numMailColumnIcons) % MAX_NUM_OF_MAIL_ICONS + 1
},
onClick = { numMailColumnIcons = (numMailColumnIcons) % MAX_NUM_OF_MAIL_ICONS + 1 },
modifier = Modifier.padding(start = 8.dp, top = 8.dp, end = 8.dp),
) {
Text("Switch num of column mail icons")
Expand All @@ -85,10 +75,10 @@ fun SampleScreen() {
AutoSizeTable(
modifier = Modifier.padding(all = 8.dp),
outlineColor = colorScheme.outline,
content = List(NUM_OF_ITEMS_IN_EACH_COLUMN) { columnId ->
List(NUM_OF_ITEMS_IN_EACH_ROW) { rowId ->
content = List(NUM_OF_ITEMS_IN_EACH_ROW) { rowIndex ->
List(NUM_OF_ITEMS_IN_EACH_COLUMN) { columnIndex ->
{
if (rowId == 0 && columnId % 2 == 0) {
if (columnIndex == 0 && rowIndex % 2 == 1) {
Column {
repeat(numMailColumnIcons) {
Row {
Expand All @@ -106,29 +96,18 @@ fun SampleScreen() {
}
} else {
Text(
text = "rowId: $rowId \ncolumnId: $columnId",
text = "column: $columnIndex\nrow: $rowIndex",
modifier = Modifier.padding(8.dp),
fontWeight = if (columnId < type[typeIndex].first) {
FontWeight.Bold
} else {
FontWeight.Normal
},
)
}
}
}
},
backgroundColor = { columnId, rowId ->
backgroundColor = { rowIndex, columnIndex ->
when {
columnId in 0..<type[typeIndex].first -> {
colorScheme.primaryContainer
}

rowId in 0..<type[typeIndex].second -> {
colorScheme.tertiaryContainer
}

columnId % 2 == 0 -> colorScheme.surface
rowIndex in 0..<type[typeIndex].first -> colorScheme.primaryContainer
columnIndex in 0..<type[typeIndex].second -> colorScheme.tertiaryContainer
rowIndex % 2 == 0 -> colorScheme.surface
else -> colorScheme.inverseOnSurface
}
},
Expand Down
103 changes: 48 additions & 55 deletions autosizetable/src/main/java/ksnd/autosizetable/AutoSizeTable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ fun AutoSizeTable(
outlineColor: Color = Color.Black,
horizontalScrollState: ScrollState = rememberScrollState(),
verticalScrollState: ScrollState = rememberScrollState(),
backgroundColor: (columnId: Int, rowId: Int) -> Color = { _, _ -> Color.Unspecified },
contentAlignment: (columnId: Int, rowId: Int) -> Alignment = { _, _ -> Alignment.TopStart },
backgroundColor: (rowIndex: Int, columnIndex: Int) -> Color = { _, _ -> Color.Unspecified },
contentAlignment: (rowIndex: Int, columnIndex: Int) -> Alignment = { _, _ -> Alignment.TopStart },
content: List<List<@Composable () -> Unit>>,
) {
val isFixedTop by remember(fixedTopSize) { derivedStateOf { fixedTopSize > 0 } }
Expand Down Expand Up @@ -87,18 +87,18 @@ fun AutoSizeTable(

// Fixed top and left part
Column {
content.take(fixedTopSize).forEachIndexed { columnId, columnList ->
content.take(fixedTopSize).forEachIndexed { rowIndex, rowList ->
Row {
columnList.take(fixedStartSize).forEachIndexed { rowId, item ->
rowList.take(fixedStartSize).forEachIndexed { columnIndex, item ->
Box(
modifier = Modifier
.size(
width = tableItemSize.columnWidthSize[rowId],
height = tableItemSize.rowHeightSize[columnId],
width = tableItemSize.columnWidthSize[columnIndex],
height = tableItemSize.rowHeightSize[rowIndex],
)
.background(color = backgroundColor(columnId, rowId))
.background(color = backgroundColor(rowIndex, columnIndex))
.drawBehind(onDraw = outlineOnDraw),
contentAlignment = contentAlignment(columnId, rowId),
contentAlignment = contentAlignment(rowIndex, columnIndex),
) {
item()
}
Expand All @@ -120,20 +120,20 @@ fun AutoSizeTable(
Modifier
},
) {
content.take(fixedTopSize).forEachIndexed { columnId, columnList ->
content.take(fixedTopSize).forEachIndexed { rowIndex, rowList ->
Row {
columnList.takeLast(columnList.size - fixedStartSize).forEachIndexed { rowId, item ->
rowList.takeLast(rowList.size - fixedStartSize).forEachIndexed { columnIndex, item ->
Box(
modifier = Modifier
.size(
width = tableItemSize.columnWidthSize[rowId + fixedStartSize],
height = tableItemSize.rowHeightSize[columnId],
width = tableItemSize.columnWidthSize[columnIndex + fixedStartSize],
height = tableItemSize.rowHeightSize[rowIndex],
)
.background(
color = backgroundColor(columnId, rowId + fixedStartSize),
color = backgroundColor(rowIndex, columnIndex + fixedStartSize),
)
.drawBehind(onDraw = outlineOnDraw),
contentAlignment = contentAlignment(columnId, rowId + fixedStartSize),
contentAlignment = contentAlignment(rowIndex, columnIndex + fixedStartSize),
) {
item()
}
Expand All @@ -155,28 +155,27 @@ fun AutoSizeTable(

// Fixed left part
Column {
content.takeLast(content.size - fixedTopSize)
.forEachIndexed { columnId, columnList ->
Row {
columnList.take(fixedStartSize).forEachIndexed { rowId, item ->
Box(
modifier =
Modifier
.size(
width = tableItemSize.columnWidthSize[rowId],
height = tableItemSize.rowHeightSize[columnId + fixedTopSize],
)
.background(
color = backgroundColor(columnId + fixedTopSize, rowId),
)
.drawBehind(onDraw = outlineOnDraw),
contentAlignment = contentAlignment(columnId + fixedTopSize, rowId),
) {
item()
}
content.takeLast(content.size - fixedTopSize).forEachIndexed { rowIndex, rowList ->
Row {
rowList.take(fixedStartSize).forEachIndexed { columnIndex, item ->
Box(
modifier =
Modifier
.size(
width = tableItemSize.columnWidthSize[columnIndex],
height = tableItemSize.rowHeightSize[rowIndex + fixedTopSize],
)
.background(
color = backgroundColor(rowIndex + fixedTopSize, columnIndex),
)
.drawBehind(onDraw = outlineOnDraw),
contentAlignment = contentAlignment(rowIndex + fixedTopSize, columnIndex),
) {
item()
}
}
}
}
}

// Unfixed part
Expand All @@ -187,29 +186,23 @@ fun AutoSizeTable(
) {
Column(
modifier = if (isFixedTop || isFixedStart) {
Modifier
.horizontalScroll(horizontalScrollState)
Modifier.horizontalScroll(horizontalScrollState)
} else {
Modifier
},
) {
content.takeLast(content.size - fixedTopSize).forEachIndexed { columnId, columnList ->
content.takeLast(content.size - fixedTopSize).forEachIndexed { rowIndex, rowList ->
Row {
columnList.takeLast(content.first().size - fixedStartSize).forEachIndexed { rowId, item ->
rowList.takeLast(content.first().size - fixedStartSize).forEachIndexed { columnIndex, item ->
Box(
modifier = Modifier
.size(
width = tableItemSize.columnWidthSize[rowId + fixedStartSize],
height = tableItemSize.rowHeightSize[columnId + fixedTopSize],
)
.background(
color = backgroundColor(
columnId + fixedTopSize,
rowId + fixedStartSize,
),
width = tableItemSize.columnWidthSize[columnIndex + fixedStartSize],
height = tableItemSize.rowHeightSize[rowIndex + fixedTopSize],
)
.background(color = backgroundColor(rowIndex + fixedTopSize, columnIndex + fixedStartSize))
.drawBehind(onDraw = outlineOnDraw),
contentAlignment = contentAlignment(columnId + fixedTopSize, rowId + fixedStartSize),
contentAlignment = contentAlignment(rowIndex + fixedTopSize, columnIndex + fixedStartSize),
) {
item()
}
Expand Down Expand Up @@ -249,21 +242,21 @@ private fun MeasureTable(
val heightSizes = MutableList(items.size) { 0.dp }
val widthSizes = MutableList(items.first().size) { 0.dp }

val itemsMeasurable = items.mapIndexed { columnId, columnList ->
List(columnList.size) { rowId ->
subcompose("${columnId}_$rowId") {
items[columnId][rowId]()
val itemsMeasurable = items.mapIndexed { rowIndex, rowList ->
List(rowList.size) { columnIndex ->
subcompose("${rowIndex}_$columnIndex") {
items[rowIndex][columnIndex]()
}.first().measure(Constraints())
}
}

items.forEachIndexed { columnId, columnList ->
columnList.forEachIndexed { rowId, _ ->
val item = itemsMeasurable[columnId][rowId]
items.forEachIndexed { rowIndex, rowList ->
rowList.forEachIndexed { columnIndex, _ ->
val item = itemsMeasurable[rowIndex][columnIndex]
val width = item.width.toDp()
val height = item.height.toDp()
widthSizes[rowId] = maxOf(widthSizes[rowId], width)
heightSizes[columnId] = maxOf(heightSizes[columnId], height)
widthSizes[columnIndex] = maxOf(widthSizes[columnIndex], width)
heightSizes[rowIndex] = maxOf(heightSizes[rowIndex], height)
}
}

Expand Down

0 comments on commit 220184c

Please sign in to comment.