Elio Maroun
08/15/2023, 11:50 AMElio Maroun
08/15/2023, 11:50 AM@Composable
fun Schedule(
bookings: List<Booking>,
modifier: Modifier = Modifier,
bookingContent: @Composable (booking: Booking) -> Unit = { BookingCard(booking = it) },
onEmptyViewClicked: (Int, Int, Int) -> Unit
) {
val hourHeight = 64.dp
Layout(
content = {
bookings.sortedBy { it.start.localDateTime }
.forEach { booking ->
Box(modifier = Modifier.bookingData(booking)) {
bookingContent(booking)
}
}
},
modifier = modifier
.pointerInput(Unit) {
detectTapGestures {
val x = it.x.toDp()
val y = it.y.toDp()
val time = y.value.toInt() / hourHeight.value
val selectedDate = time.toInt()
println("X: $x, Y: $y")
println("selected date $selectedDate")
println("Time: $time")
val days = selectedDate / 24
val startHour = selectedDate - (24 * days)
onEmptyViewClicked(days, startHour, selectedDate)
}
}
.drawBehind {
repeat(167) {
drawLine(
color = Color.LightGray,
start = Offset(0f, (it + 1) * hourHeight.toPx()),
end = Offset(size.width, (it + 1) * hourHeight.toPx()),
strokeWidth = 1.dp.toPx()
)
}
},
) { measureables, constraints ->
val height = hourHeight.roundToPx() * 168
val placeablesWithBookings = measureables.map { measurable ->
val booking = measurable.parentData as Booking
val bookingDurationMinutes =
ChronoUnit.MINUTES.between(booking.start.localDateTime, booking.end.localDateTime)
val eventHeight = ((bookingDurationMinutes / 60f) * hourHeight.toPx()).roundToInt()
val placeable = measurable.measure(
constraints.copy(
minHeight = eventHeight,
maxHeight = eventHeight
)
)
Pair(placeable, booking)
}
layout(constraints.maxWidth, height) {
placeablesWithBookings.forEach { (placeable, booking) ->
val dayOffset = ChronoUnit.DAYS.between(
LocalDate.now(),
booking.start.localDateTime.toLocalDate()
)
val bookingOffsetMinutes = ChronoUnit.MINUTES.between(
LocalTime.MIN,
booking.start.localDateTime.toLocalTime()
)
val bookingY =
(((bookingOffsetMinutes / 60f) * hourHeight.toPx()) + (dayOffset * hourHeight.roundToPx() * 24)).roundToInt()
placeable.place(0, bookingY)
}
}
}
}
private class BookingDataModifier(
val booking: Booking,
) : ParentDataModifier {
override fun Density.modifyParentData(parentData: Any?) = booking
}
private fun Modifier.bookingData(booking: Booking) = this.then(BookingDataModifier(booking))
Elio Maroun
08/15/2023, 1:20 PM