From 2148c54684f1214752126e188076c91a919ab5fc Mon Sep 17 00:00:00 2001 From: Yurii Surzhykov Date: Tue, 28 May 2024 16:31:23 -0700 Subject: [PATCH] Fix NPE exception because of schedule Signed-off-by: Yurii Surzhykov --- .../purs/domain/model/LocationStatus.kt | 6 ++++- .../BuildCurrentLocationStatusUseCase.kt | 22 +++++++++++++++---- .../location/details/LocationDetailsScreen.kt | 8 +++++++ .../src/main/res/values/strings.xml | 1 + 4 files changed, 32 insertions(+), 5 deletions(-) diff --git a/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/model/LocationStatus.kt b/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/model/LocationStatus.kt index b8e5e92..29a7ce5 100644 --- a/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/model/LocationStatus.kt +++ b/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/model/LocationStatus.kt @@ -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 @@ -17,7 +21,7 @@ sealed interface LocationStatus { val closeTime: LocalTime, val reopenDay: String, val reopenTime: LocalTime - ): LocationStatus + ) : LocationStatus data class ClosedOpenSoon( val reopenTime: LocalTime diff --git a/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/usecase/BuildCurrentLocationStatusUseCase.kt b/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/usecase/BuildCurrentLocationStatusUseCase.kt index a8c7d87..ad4bb73 100644 --- a/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/usecase/BuildCurrentLocationStatusUseCase.kt +++ b/location-domain/src/main/java/com/github/yuriisurzhykov/purs/domain/usecase/BuildCurrentLocationStatusUseCase.kt @@ -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) } @@ -40,8 +40,13 @@ interface BuildCurrentLocationStatusUseCase { * */ private fun findClosedStatus( currentTime: LocalTime, - nextSchedule: Pair + nextSchedule: Pair? ): LocationStatus { + + if (nextSchedule == null) { + return LocationStatus.ClosedFully + } + val nextOpenTime = nextSchedule.second.startTime val reopenTimeDifference = currentTime.until(nextOpenTime, ChronoUnit.HOURS) return if (reopenTimeDifference > 24) { @@ -57,7 +62,7 @@ interface BuildCurrentLocationStatusUseCase { * */ private fun findCurrentOpenStatus( workingDay: WorkingDay, - nextWorkingDay: Pair, + nextWorkingDay: Pair?, currentTime: LocalTime ): LocationStatus? { // Looking for schedule time slot that applied to the current time @@ -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) { diff --git a/ui-features/location-details/src/main/java/com/github/yuriisurzhykov/purs/location/details/LocationDetailsScreen.kt b/ui-features/location-details/src/main/java/com/github/yuriisurzhykov/purs/location/details/LocationDetailsScreen.kt index cab4c5b..6fc8044 100644 --- a/ui-features/location-details/src/main/java/com/github/yuriisurzhykov/purs/location/details/LocationDetailsScreen.kt +++ b/ui-features/location-details/src/main/java/com/github/yuriisurzhykov/purs/location/details/LocationDetailsScreen.kt @@ -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, @@ -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(), diff --git a/ui-features/location-details/src/main/res/values/strings.xml b/ui-features/location-details/src/main/res/values/strings.xml index 69d6977..e0c8d49 100644 --- a/ui-features/location-details/src/main/res/values/strings.xml +++ b/ui-features/location-details/src/main/res/values/strings.xml @@ -10,4 +10,5 @@ Opens again at %s Opens %s at %s Open until %s, reopens on %s %s + Closing at %s \ No newline at end of file