diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt index b6ca3ed652..292f6ac8a8 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfiles.kt @@ -24,22 +24,22 @@ object NotificationProfiles { val storeValues: NotificationProfileValues = SignalStore.notificationProfileValues() val localNow: LocalDateTime = now.toLocalDateTime(zoneId) - val manualProfile: NotificationProfile? = profiles.firstOrNull { it.id == storeValues.manuallyEnabledProfile } + val manualProfile: NotificationProfile? = if (now < storeValues.manuallyEnabledUntil) { + profiles.firstOrNull { it.id == storeValues.manuallyEnabledProfile } + } else { + null + } val scheduledProfile: NotificationProfile? = profiles.sortedDescending().filter { it.schedule.isCurrentlyActive(now, zoneId) }.firstOrNull { profile -> profile.schedule.startDateTime(localNow).toMillis(zoneId.toOffset()) > storeValues.manuallyDisabledAt } if (manualProfile == null || scheduledProfile == null) { - return (if (now < storeValues.manuallyEnabledUntil) manualProfile else null) ?: scheduledProfile + return manualProfile ?: scheduledProfile } return if (manualProfile == scheduledProfile) { - if (storeValues.manuallyEnabledUntil == Long.MAX_VALUE || now < storeValues.manuallyEnabledUntil) { - manualProfile - } else { - null - } + manualProfile } else { scheduledProfile } diff --git a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt index 5dbb1f31d3..36916c096f 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfilesTest.kt @@ -24,6 +24,7 @@ class NotificationProfilesTest { private val sunday830am: LocalDateTime = LocalDateTime.of(2021, 7, 4, 8, 30, 0) private val sunday9am: LocalDateTime = LocalDateTime.of(2021, 7, 4, 9, 0, 0) private val sunday930am: LocalDateTime = LocalDateTime.of(2021, 7, 4, 9, 30, 0) + private val monday830am: LocalDateTime = sunday830am.plusDays(1) private val utc: ZoneId = ZoneId.of("UTC") private val first = NotificationProfile( @@ -139,4 +140,15 @@ class NotificationProfilesTest { val profiles = listOf(first.copy(schedule = schedule)) assertThat("active profile is null", NotificationProfiles.getActiveProfile(profiles, sunday930am.toMillis(ZoneOffset.UTC), utc), nullValue()) } + + @Test + fun `when profile is manually enabled yesterday and is scheduled also for today then return profile`() { + signalStore.dataSet.putLong(NotificationProfileValues.KEY_MANUALLY_ENABLED_PROFILE, first.id) + signalStore.dataSet.putLong(NotificationProfileValues.KEY_MANUALLY_ENABLED_UNTIL, sunday9am.toMillis(ZoneOffset.UTC)) + signalStore.dataSet.putLong(NotificationProfileValues.KEY_MANUALLY_DISABLED_AT, sunday830am.toMillis(ZoneOffset.UTC)) + + val schedule = NotificationProfileSchedule(id = 3L, enabled = true, start = 700, end = 900, daysEnabled = setOf(DayOfWeek.SUNDAY, DayOfWeek.MONDAY)) + val profiles = listOf(first.copy(schedule = schedule)) + assertThat("active profile is first", NotificationProfiles.getActiveProfile(profiles, monday830am.toMillis(ZoneOffset.UTC), utc), `is`(profiles[0])) + } }