diff --git a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt index f947a08647..11960e76dd 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/components/settings/app/notifications/profiles/EditNotificationProfileScheduleViewModel.kt @@ -60,7 +60,8 @@ class EditNotificationProfileScheduleViewModel( } fun setEndTime(hour: Int, minute: Int) { - scheduleSubject.onNext(schedule.copy(end = hour * 100 + minute)) + val adjustedEndHour = if (hour == 0) 24 else hour + scheduleSubject.onNext(schedule.copy(end = adjustedEndHour * 100 + minute)) } fun setEnabled(enabled: Boolean) { diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt index d4b827c33b..fb777b4033 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/helpers/SignalDatabaseMigrations.kt @@ -180,8 +180,9 @@ object SignalDatabaseMigrations { private const val REACTION_REFACTOR = 121 private const val PNI = 122 private const val NOTIFICATION_PROFILES = 123 + private const val NOTIFICATION_PROFILES_END_FIX = 124 - const val DATABASE_VERSION = 123 + const val DATABASE_VERSION = 124 @JvmStatic fun migrate(context: Context, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) { @@ -2222,6 +2223,15 @@ object SignalDatabaseMigrations { db.execSQL("CREATE INDEX notification_profile_schedule_profile_index ON notification_profile_schedule (notification_profile_id)") db.execSQL("CREATE INDEX notification_profile_allowed_members_profile_index ON notification_profile_allowed_members (notification_profile_id)") } + + if (oldVersion < NOTIFICATION_PROFILES_END_FIX) { + db.execSQL( + // language=sql + """ + UPDATE notification_profile_schedule SET end = 2400 WHERE end = 0 + """.trimIndent() + ) + } } @JvmStatic diff --git a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt index 76725f6419..949905482d 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileSchedule.kt @@ -56,7 +56,8 @@ data class NotificationProfileSchedule( } fun endTime(): LocalTime { - return LocalTime.of(end / 100, end % 100) + val adjustedEnd = if (end == 2400) 0 else end + return LocalTime.of(adjustedEnd / 100, adjustedEnd % 100) } fun endDateTime(localNow: LocalDateTime): LocalDateTime { @@ -73,8 +74,8 @@ data class NotificationProfileSchedule( fun Int.toLocalDateTime(now: LocalDateTime): LocalDateTime { if (this == 2400) { - return now.plusDays(1).withHour(0) + return now.plusDays(1).withHour(0).withMinute(0).withSecond(0) } - return now.withHour(this / 100).withMinute(this % 100) + return now.withHour(this / 100).withMinute(this % 100).withSecond(0) } diff --git a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt index d8ab050531..6039c5c4fa 100644 --- a/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt +++ b/app/src/test/java/org/thoughtcrime/securesms/notifications/profiles/NotificationProfileScheduleTest.kt @@ -1,5 +1,7 @@ package org.thoughtcrime.securesms.notifications.profiles +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.`is` import org.junit.Assert.assertFalse import org.junit.Assert.assertTrue import org.junit.BeforeClass @@ -15,6 +17,7 @@ class NotificationProfileScheduleTest { private val sunday0am: LocalDateTime = LocalDateTime.of(2021, 7, 4, 0, 0, 0) private val sunday1am: LocalDateTime = LocalDateTime.of(2021, 7, 4, 1, 0, 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 sunday10pm: LocalDateTime = LocalDateTime.of(2021, 7, 4, 22, 0, 0) private val monday0am: LocalDateTime = sunday0am.plusDays(1) @@ -139,4 +142,10 @@ class NotificationProfileScheduleTest { assertFalse(schedule.isCurrentlyActive(tuesday9am.toMillis(ZoneOffset.UTC))) assertFalse(schedule.isCurrentlyActive(tuesday10pm.toMillis(ZoneOffset.UTC))) } + + @Test + fun `when end time is midnight return midnight of next day from now`() { + val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 2400, daysEnabled = DayOfWeek.values().toSet()) + assertThat(schedule.endDateTime(sunday930am), `is`(monday0am)) + } }