Home > Mobile >  make list full of timeslots avoiding specific timeframe
make list full of timeslots avoiding specific timeframe

Time:12-21

I am trying to make a list that by default contains a list of available timeslots from one timepoint to another timepoint. It has to avoid making timeslots in a specific timeframe.

For example: I have a first timepoint being 09:00 and the end timepoint being 17:00. The duration of the timeslots is 15. Only sometimes I can't have any time timeslots in a specific timeframe. Which,for example, can be from 12:00 to 12:30.

So eventually the slots should be filled with TimeSlots from 09:00 to 12:00. And from 12:30 to 17:00. This is my code so far, I am using java.time.LocalTime. So far, it's not going that well... Thanks for looking!

data class TimeSlot(
    val startTime: LocalTime,
    val endTime: LocalTime,
)
private fun initializeSlots(
    slots: ArrayList<TimeSlot>,
    startTimeShift: LocalTime,
    appointmentDuration: Long,
    amountOfWorkingHours: Long,
    breakTime: LocalTime,
    breakDuration: Long
) {
    slots.add(TimeSlot(startTime = startTimeShift, endTime = startTimeShift.plusMinutes(appointmentDuration)))
    val possibleTotalAppointments = (amountOfWorkingHours * appointmentDuration) - 2 // -2 because index starts at 0 and first timeslot is already added.
    for (i in 0..(amountOfWorkingHours * appointmentDuration).toInt()) {
        if (slots[i].endTime == breakTime) {
            val endTimeOfBreak = breakTime.plusMinutes(breakDuration)
            val isTargetWithinTimeFrame = (!breakTime.isBefore(slots[i].startTime) && breakTime.isBefore(endTimeOfBreak))
            if (isTargetWithinTimeFrame) {
                slots.remove(slots[i])
                continue
            }
        } else {
            slots.add(TimeSlot(startTime = slots[i].endTime, endTime = slots[i].endTime.plusMinutes(appointmentDuration)))
        }
    }
}

CodePudding user response:

If your timeslots are static(Always from 09:00 till 17:00 and always 15 min duration) you can actually hardcode them into an array, and operate through indexes further. So for example if every hour have 4 indexes starting from 0 your specific timeframe 12:00 to 12:30. will have indexes of 12, 13, 14. Such code won't be too beautiful, but still a lot more readable.

Any way, LocalTime is overall not the best idea. I would just use Long, with System.currentTimeMillis() This is the best solution for handling timezones(Formatting them only for UI, not for logic). And also it will make your time calculations much easier, because you can actually divide and subtract millis.

CodePudding user response:

I took another approach to finding the answer, so if anyone comes across the same problem, here it is. You can also give multiple timeframes you want to avoid. Feel free to alter it any way. It can also return Pairs instead of 'TimeSlot's.

fun generateTimeSlots(startTime: LocalTime, endTime: LocalTime, duration: Long,avoid: List<Pair<LocalTime, LocalTime>>): List<TimeSlot> {
    val timeSlots = mutableListOf<TimeSlot>()
    var currentTime = startTime

    while (currentTime < endTime) {
        val nextTime = currentTime.plusMinutes(duration)
        val timeSlot = Pair(currentTime, nextTime)
        val timeSlot2 = TimeSlot(currentTime, nextTime)

        // Pair has some
        if (avoid.none { timeSlot.first in it.first..it.second.minusMinutes(1) }) {
            timeSlots.add(timeSlot2)
        }

        currentTime = nextTime
    }

    return timeSlots
}
  • Related