|
| 1 | +package com.sopt.core.designsystem.component.timetable |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.border |
| 5 | +import androidx.compose.foundation.layout.Box |
| 6 | +import androidx.compose.foundation.layout.defaultMinSize |
| 7 | +import androidx.compose.foundation.layout.padding |
| 8 | +import androidx.compose.foundation.lazy.grid.GridCells |
| 9 | +import androidx.compose.foundation.lazy.grid.LazyVerticalGrid |
| 10 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 11 | +import androidx.compose.material3.Text |
| 12 | +import androidx.compose.runtime.Composable |
| 13 | +import androidx.compose.runtime.mutableStateListOf |
| 14 | +import androidx.compose.runtime.remember |
| 15 | +import androidx.compose.ui.Alignment |
| 16 | +import androidx.compose.ui.Modifier |
| 17 | +import androidx.compose.ui.graphics.Color |
| 18 | +import androidx.compose.ui.text.style.TextAlign |
| 19 | +import androidx.compose.ui.unit.dp |
| 20 | +import com.sopt.core.designsystem.theme.NoostakTheme |
| 21 | +import com.sopt.core.extension.noRippleClickable |
| 22 | +import com.sopt.domain.entity.AvailableTimeEntity |
| 23 | +import com.sopt.domain.entity.TimeEntity |
| 24 | +import com.sopt.domain.entity.TimeTableEntity |
| 25 | + |
| 26 | +@Composable |
| 27 | +fun NoostakEditableTimeTable( |
| 28 | + data: TimeTableEntity, |
| 29 | + modifier: Modifier = Modifier, |
| 30 | + onSelectionChange: (List<TimeEntity>) -> Unit |
| 31 | +) { |
| 32 | + val days = data.timeEntity.size |
| 33 | + val timeSlots = calculateTimeSlots(data.startTime, data.endTime) |
| 34 | + val selectedCells = remember { mutableStateListOf<Pair<Int, Int>>() } // Row, Column 저장 |
| 35 | + |
| 36 | + LazyVerticalGrid( |
| 37 | + modifier = modifier |
| 38 | + .border( |
| 39 | + width = 1.dp, |
| 40 | + color = NoostakTheme.colors.gray200, |
| 41 | + shape = RoundedCornerShape(8.dp) |
| 42 | + ), |
| 43 | + columns = GridCells.Fixed(days + 1) |
| 44 | + ) { |
| 45 | + items((days + 1) * (timeSlots + 1)) { index -> |
| 46 | + val rowIndex = index / (days + 1) |
| 47 | + val columnIndex = index % (days + 1) |
| 48 | + |
| 49 | + val cellType = determineCellType(rowIndex, columnIndex) |
| 50 | + val isSelected = selectedCells.contains(rowIndex to columnIndex) |
| 51 | + val backgroundColor = |
| 52 | + getEditableBackgroundColor(cellType, rowIndex, columnIndex, isSelected) |
| 53 | + val text = getCellText(cellType, rowIndex, columnIndex, data) |
| 54 | + |
| 55 | + NoostakEditableTimeTableBox( |
| 56 | + index = index, |
| 57 | + days = days, |
| 58 | + timeSlots = timeSlots, |
| 59 | + backgroundColor = backgroundColor, |
| 60 | + text = text, |
| 61 | + onClick = { |
| 62 | + if (cellType == CellType.Data) { |
| 63 | + val cell = rowIndex to columnIndex |
| 64 | + if (selectedCells.contains(cell)) { |
| 65 | + selectedCells.remove(cell) |
| 66 | + } else { |
| 67 | + selectedCells.add(cell) |
| 68 | + } |
| 69 | + onSelectionChange( |
| 70 | + selectedCells.toTimeEntities( |
| 71 | + data.startTime, |
| 72 | + data.timeEntity |
| 73 | + ) |
| 74 | + ) |
| 75 | + } |
| 76 | + } |
| 77 | + ) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@Composable |
| 83 | +fun getEditableBackgroundColor( |
| 84 | + cellType: CellType, |
| 85 | + rowIndex: Int, |
| 86 | + columnIndex: Int, |
| 87 | + isSelected: Boolean |
| 88 | +): Color { |
| 89 | + return when (cellType) { |
| 90 | + CellType.Blank, CellType.DateHeader, CellType.TimeHeader -> Color.Transparent |
| 91 | + CellType.Data -> if (isSelected) NoostakTheme.colors.blue400 else Color.Transparent |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +@Composable |
| 96 | +fun NoostakEditableTimeTableBox( |
| 97 | + index: Int, |
| 98 | + days: Int, |
| 99 | + timeSlots: Int, |
| 100 | + backgroundColor: Color, |
| 101 | + text: String, |
| 102 | + onClick: () -> Unit |
| 103 | +) { |
| 104 | + val shape = when (index) { |
| 105 | + 0 -> RoundedCornerShape(topStart = 10.dp) |
| 106 | + days -> RoundedCornerShape(topEnd = 10.dp) |
| 107 | + (days + 1) * timeSlots -> RoundedCornerShape(bottomStart = 10.dp) |
| 108 | + (days + 1) * (timeSlots + 1) - 1 -> RoundedCornerShape(bottomEnd = 10.dp) |
| 109 | + else -> RoundedCornerShape(0.dp) |
| 110 | + } |
| 111 | + |
| 112 | + Box( |
| 113 | + modifier = Modifier |
| 114 | + .border( |
| 115 | + width = 0.5.dp, |
| 116 | + color = NoostakTheme.colors.gray200, |
| 117 | + shape = shape |
| 118 | + ) |
| 119 | + .background( |
| 120 | + color = backgroundColor, |
| 121 | + shape = shape |
| 122 | + ) |
| 123 | + .defaultMinSize(minWidth = 42.dp, minHeight = 36.dp) |
| 124 | + .noRippleClickable { onClick() }, |
| 125 | + contentAlignment = Alignment.Center |
| 126 | + ) { |
| 127 | + Text( |
| 128 | + modifier = Modifier.padding(horizontal = 5.dp, vertical = 3.dp), |
| 129 | + text = text, |
| 130 | + color = NoostakTheme.colors.gray600, |
| 131 | + style = NoostakTheme.typography.c4Regular, |
| 132 | + textAlign = TextAlign.Center, |
| 133 | + maxLines = 2 |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +fun List<Pair<Int, Int>>.toTimeEntities( |
| 139 | + startTime: String, |
| 140 | + timeEntityList: List<TimeEntity> |
| 141 | +): List<TimeEntity> { |
| 142 | + val startHour = startTime.split(":")[0].toInt() |
| 143 | + val groupedByColumn = this.groupBy { it.second } |
| 144 | + |
| 145 | + return groupedByColumn.mapNotNull { (columnIndex, cells) -> |
| 146 | + val date = timeEntityList.getOrNull(columnIndex - 1)?.date ?: return@mapNotNull null |
| 147 | + val times = cells.map { cell -> |
| 148 | + val hour = startHour + (cell.first - 1) |
| 149 | + AvailableTimeEntity( |
| 150 | + startTime = "${"%02d".format(hour)}:00", |
| 151 | + endTime = "${"%02d".format(hour + 1)}:00" |
| 152 | + ) |
| 153 | + } |
| 154 | + TimeEntity(date = date, times = times) |
| 155 | + } |
| 156 | +} |
0 commit comments