Skip to content

Commit

Permalink
Fix NPE exception because of schedule
Browse files Browse the repository at this point in the history
Signed-off-by: Yurii Surzhykov <yuriisurzhykov@gmail.com>
  • Loading branch information
yuriisurzhykov committed May 28, 2024
1 parent ef279ce commit 2148c54
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ sealed interface LocationStatus {
val closeTime: LocalTime
) : LocationStatus

data class Closing(
val closeTime: LocalTime
) : LocationStatus

data class ClosingSoon(
val closeTime: LocalTime,
val reopenTime: LocalTime
Expand All @@ -17,7 +21,7 @@ sealed interface LocationStatus {
val closeTime: LocalTime,
val reopenDay: String,
val reopenTime: LocalTime
): LocationStatus
) : LocationStatus

data class ClosedOpenSoon(
val reopenTime: LocalTime
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface BuildCurrentLocationStatusUseCase {
findNextWorkingDay(workingDays.toList(), currentDate, currentTime)

// Return location status for whether it is open or closed now
return findCurrentOpenStatus(currentSchedule, nextSchedule!!, currentTime)
return findCurrentOpenStatus(currentSchedule, nextSchedule, currentTime)
?: findClosedStatus(currentTime, nextSchedule)
}

Expand All @@ -40,8 +40,13 @@ interface BuildCurrentLocationStatusUseCase {
* */
private fun findClosedStatus(
currentTime: LocalTime,
nextSchedule: Pair<String, TimeSlot>
nextSchedule: Pair<String, TimeSlot>?
): LocationStatus {

if (nextSchedule == null) {
return LocationStatus.ClosedFully
}

val nextOpenTime = nextSchedule.second.startTime
val reopenTimeDifference = currentTime.until(nextOpenTime, ChronoUnit.HOURS)
return if (reopenTimeDifference > 24) {
Expand All @@ -57,7 +62,7 @@ interface BuildCurrentLocationStatusUseCase {
* */
private fun findCurrentOpenStatus(
workingDay: WorkingDay,
nextWorkingDay: Pair<String, TimeSlot>,
nextWorkingDay: Pair<String, TimeSlot>?,
currentTime: LocalTime
): LocationStatus? {
// Looking for schedule time slot that applied to the current time
Expand All @@ -70,10 +75,19 @@ interface BuildCurrentLocationStatusUseCase {
}
return if (currentOpenSchedule != null) {
val timeDifference =
currentTime.until(currentOpenSchedule.endTime, ChronoUnit.MINUTES)
if (currentOpenSchedule.endTime < currentOpenSchedule.startTime) {
currentTime.until(LocalTime.MAX, ChronoUnit.MINUTES) +
LocalTime.MIDNIGHT.until(currentOpenSchedule.endTime, ChronoUnit.MINUTES)
} else {
currentTime.until(currentOpenSchedule.endTime, ChronoUnit.MINUTES)
}
// If the location closes within 24 hours, return the location status
// that it closing soon. Otherwise, return the location status that it opens
return if (timeDifference <= 60) {
if (nextWorkingDay == null) {
return LocationStatus.Closing(currentOpenSchedule.endTime)
}

val nextOpenTime = nextWorkingDay.second.startTime
val reopenTimeDifference = currentTime.until(nextOpenTime, ChronoUnit.HOURS)
if (reopenTimeDifference > 24) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ fun LocationStatusBadge(status: LocationStatus, modifier: Modifier = Modifier) {
when (status) {
is LocationStatus.Open -> ColorBadge(color = R.color.color_green, modifier)

is LocationStatus.Closing,
is LocationStatus.ClosingSoon,
is LocationStatus.ClosingSoonLongReopen -> ColorBadge(
color = R.color.color_yellow,
Expand Down Expand Up @@ -285,6 +286,13 @@ fun LocationStatusText(status: LocationStatus, modifier: Modifier = Modifier) {
modifier = modifier
)

is LocationStatus.Closing -> LocationStatusTextView(
text = stringResource(id = R.string.label_location_closing).format(
status.closeTime.toFormattedString()
),
modifier = modifier
)

is LocationStatus.ClosingSoon -> LocationStatusTextView(
text = stringResource(id = R.string.label_location_closing_soon).format(
status.closeTime.toFormattedString(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@
<string name="label_location_closed_opens_soon">Opens again at %s</string>
<string name="label_location_closed_opens_then">Opens %s at %s</string>
<string name="label_location_closing_soon_reopen_then">Open until %s, reopens on %s %s</string>
<string name="label_location_closing">Closing at %s</string>
</resources>

0 comments on commit 2148c54

Please sign in to comment.