From e8a8b6a0dad61883874ffaf8b50154f993bde6a8 Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Sat, 1 Apr 2023 07:21:29 +0530 Subject: [PATCH] Improve time check in NotificationWorker. --- .../libretube/workers/NotificationWorker.kt | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt index ee50523d3..aa751a573 100644 --- a/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt +++ b/app/src/main/java/com/github/libretube/workers/NotificationWorker.kt @@ -54,24 +54,19 @@ class NotificationWorker(appContext: Context, parameters: WorkerParameters) : * Determine whether the time is valid to notify */ private fun checkTime(): Boolean { - if (!PreferenceHelper.getBoolean( - PreferenceKeys.NOTIFICATION_TIME_ENABLED, - false - ) - ) { + if (!PreferenceHelper.getBoolean(PreferenceKeys.NOTIFICATION_TIME_ENABLED, false)) { return true } val start = getTimePickerPref(PreferenceKeys.NOTIFICATION_START_TIME) val end = getTimePickerPref(PreferenceKeys.NOTIFICATION_END_TIME) - val currentTime = LocalTime.now() - val isOverNight = start > end - val startValid = if (isOverNight) start > currentTime else start < currentTime - val endValid = if (isOverNight) end < currentTime else start > currentTime - - return (startValid && endValid) + return if (start > end) { + currentTime !in end..start + } else { + currentTime in start..end + } } private fun getTimePickerPref(key: String): LocalTime {