Skip to content

Commit

Permalink
Improve docs
Browse files Browse the repository at this point in the history
  • Loading branch information
cristan committed Sep 9, 2024
1 parent 858e537 commit e5639cc
Showing 1 changed file with 7 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ import java.util.Locale

object OpenStateMapper {
fun getOpenState(openingHours: List<OpeningHours>, dateTime: LocalDateTime): OpenState {
// Check for open 24/7
if (
openingHours.all { it.startTime == "00:00" && it.endTime == "24:00" } ||
openingHours.all { it.startTime == "00:00" && it.endTime == "00:01" } ||
openingHours.all { it.startTime == "00:00" && it.endTime == "23:59" }
) {
return OpenState.Open247
}

// Monday is 1, Sunday is 7, same as what is returned from the API
val today = dateTime.dayOfWeek.value

// Find the opening hours of yesterday in case it's still open
val yesterdayOpeningHours = openingHours.find {
val dayYesterday = if (today == 1) 7 else today - 1
if (it.dayOfWeek == dayYesterday && it.closesNextDay && it.endTime != "24:00") {
Expand All @@ -36,6 +39,7 @@ object OpenStateMapper {
}
}

// Check if it's open now
val todayOpeningHours = openingHours.find {
if (it.dayOfWeek == today) {
val afterStartTime = it.startTime == "00:00" || dateTime.toLocalTime() >= LocalTime.parse(it.startTime)
Expand All @@ -60,12 +64,15 @@ object OpenStateMapper {
}
}

// Check if it will open today
val todayOpen = openingHours.find {
it.dayOfWeek == today && dateTime.toLocalTime() < LocalTime.parse(it.startTime)
}
if (todayOpen != null) {
return OpenState.Closed(openDay = null, todayOpen.startTime)
}

// It isn't open and won't open today as well. Find when the next time is that it opens.
val nextDayInWeek = (today + 1..7).firstNotNullOfOrNull { day ->
openingHours.find { it.dayOfWeek == day }
}
Expand Down

0 comments on commit e5639cc

Please sign in to comment.