Fix bugs with notification schedules caused by 24xx end times.

This commit is contained in:
Cody Henthorne 2024-12-05 10:49:24 -05:00 committed by Greyson Parrelli
parent 5cd0062688
commit 30ad854381
5 changed files with 41 additions and 23 deletions

View file

@ -60,8 +60,7 @@ class EditNotificationProfileScheduleViewModel(
}
fun setEndTime(hour: Int, minute: Int) {
val adjustedEndHour = if (hour == 0) 24 else hour
scheduleSubject.onNext(schedule.copy(end = adjustedEndHour * 100 + minute))
scheduleSubject.onNext(schedule.copy(end = hour * 100 + minute))
}
fun setEnabled(enabled: Boolean) {

View file

@ -115,6 +115,7 @@ import org.thoughtcrime.securesms.database.helpers.migration.V255_AddCallTableLo
import org.thoughtcrime.securesms.database.helpers.migration.V256_FixIncrementalDigestColumns
import org.thoughtcrime.securesms.database.helpers.migration.V257_CreateBackupMediaSyncTable
import org.thoughtcrime.securesms.database.helpers.migration.V258_FixGroupRevokedInviteeUpdate
import org.thoughtcrime.securesms.database.helpers.migration.V259_AdjustNotificationProfileMidnightEndTimes
/**
* Contains all of the database migrations for [SignalDatabase]. Broken into a separate file for cleanliness.
@ -232,10 +233,11 @@ object SignalDatabaseMigrations {
255 to V255_AddCallTableLogIndex,
256 to V256_FixIncrementalDigestColumns,
257 to V257_CreateBackupMediaSyncTable,
258 to V258_FixGroupRevokedInviteeUpdate
258 to V258_FixGroupRevokedInviteeUpdate,
259 to V259_AdjustNotificationProfileMidnightEndTimes
)
const val DATABASE_VERSION = 258
const val DATABASE_VERSION = 259
@JvmStatic
fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {

View file

@ -0,0 +1,23 @@
package org.thoughtcrime.securesms.database.helpers.migration
import android.app.Application
import net.zetetic.database.sqlcipher.SQLiteDatabase
/**
* Adjust notification profile schedules with end times between midnight at 1am. These were originally
* stored as 24xx and will now use the same as start with 00xx.
*/
@Suppress("ClassName")
object V259_AdjustNotificationProfileMidnightEndTimes : SignalDatabaseMigration {
override fun migrate(context: Application, db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {
db.execSQL(
"""
UPDATE
notification_profile_schedule
SET 'end' = end - 2400
WHERE
end >= 2400
"""
)
}
}

View file

@ -39,9 +39,8 @@ data class NotificationProfileSchedule(
val localStart: LocalDateTime = start.toLocalDateTime(localNow)
val localEnd: LocalDateTime = end.toLocalDateTime(localNow)
return if (end < start) {
(daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd)) ||
(daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1)))
return if (end <= start) {
(daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd)) || (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1)))
} else {
daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd)
}
@ -55,7 +54,7 @@ data class NotificationProfileSchedule(
val localStart: LocalDateTime = start.toLocalDateTime(localNow)
val localEnd: LocalDateTime = end.toLocalDateTime(localNow)
return if (end < start && (daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd))) {
return if (end <= start && (daysEnabled.contains(localStart.dayOfWeek.minus(1)) && localNow.isBetween(localStart.minusDays(1), localEnd))) {
localStart.minusDays(1)
} else {
localStart
@ -63,15 +62,14 @@ data class NotificationProfileSchedule(
}
fun endTime(): LocalTime {
val adjustedEnd = if (end == 2400) 0 else end
return LocalTime.of(adjustedEnd / 100, adjustedEnd % 100)
return LocalTime.of(end / 100, end % 100)
}
fun endDateTime(localNow: LocalDateTime): LocalDateTime {
val localStart: LocalDateTime = start.toLocalDateTime(localNow)
val localEnd: LocalDateTime = end.toLocalDateTime(localNow)
return if (end < start && (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1)))) {
return if (end <= start && (daysEnabled.contains(localStart.dayOfWeek) && localNow.isBetween(localStart, localEnd.plusDays(1)))) {
localEnd.plusDays(1)
} else {
localEnd
@ -80,9 +78,5 @@ data class NotificationProfileSchedule(
}
fun Int.toLocalDateTime(now: LocalDateTime): LocalDateTime {
if (this == 2400) {
return now.plusDays(1).withHour(0).withMinute(0).withSecond(0)
}
return now.withHour(this / 100).withMinute(this % 100).withSecond(0)
}

View file

@ -78,7 +78,7 @@ class NotificationProfileScheduleTest {
}
@Test
fun `when time is inside enabled schedule 12am to 10am then return false`() {
fun `when time is inside enabled schedule 12am to 10am then return true`() {
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 1000, daysEnabled = setOf(DayOfWeek.SUNDAY))
assertTrue(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC)))
@ -87,8 +87,8 @@ class NotificationProfileScheduleTest {
}
@Test
fun `when time is inside enabled schedule 12am to 12am then return false`() {
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 2400, daysEnabled = setOf(DayOfWeek.SUNDAY))
fun `when time is inside enabled schedule 12am to 12am then return true`() {
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 0, daysEnabled = setOf(DayOfWeek.SUNDAY))
assertTrue(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC)))
assertTrue(schedule.isCurrentlyActive(sunday1am.toMillis(ZoneOffset.UTC)))
@ -98,9 +98,9 @@ class NotificationProfileScheduleTest {
@Test
fun `when time is outside enabled schedule 12am to 12am then return false`() {
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 2400, daysEnabled = setOf(DayOfWeek.SUNDAY))
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 0, daysEnabled = setOf(DayOfWeek.SUNDAY))
assertFalse(schedule.isCurrentlyActive(monday0am.toMillis(ZoneOffset.UTC)))
assertFalse(schedule.isCurrentlyActive(monday0am.plusMinutes(1).toMillis(ZoneOffset.UTC)))
assertFalse(schedule.isCurrentlyActive(monday1am.toMillis(ZoneOffset.UTC)))
assertFalse(schedule.isCurrentlyActive(monday9am.toMillis(ZoneOffset.UTC)))
assertFalse(schedule.isCurrentlyActive(monday10pm.toMillis(ZoneOffset.UTC)))
@ -111,7 +111,7 @@ class NotificationProfileScheduleTest {
@Test
fun `when enabled schedule 12am to 12am for all days then return true`() {
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 2400, daysEnabled = DayOfWeek.entries.toSet())
val schedule = NotificationProfileSchedule(id = 1L, enabled = true, start = 0, end = 0, daysEnabled = DayOfWeek.entries.toSet())
assertTrue(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC)))
assertTrue(schedule.isCurrentlyActive(sunday1am.toMillis(ZoneOffset.UTC)))
@ -128,7 +128,7 @@ class NotificationProfileScheduleTest {
@Test
fun `when disabled schedule 12am to 12am for all days then return false`() {
val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 2400, daysEnabled = DayOfWeek.entries.toSet())
val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 0, daysEnabled = DayOfWeek.entries.toSet())
assertFalse(schedule.isCurrentlyActive(sunday0am.toMillis(ZoneOffset.UTC)))
assertFalse(schedule.isCurrentlyActive(sunday1am.toMillis(ZoneOffset.UTC)))
@ -145,7 +145,7 @@ class NotificationProfileScheduleTest {
@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.entries.toSet())
val schedule = NotificationProfileSchedule(id = 1L, enabled = false, start = 0, end = 0, daysEnabled = DayOfWeek.entries.toSet())
assertThat(schedule.endDateTime(sunday930am), `is`(monday0am))
}
}