Skip to content

Commit

Permalink
Merge pull request #11 from ReneeVandervelde/conditional-clear
Browse files Browse the repository at this point in the history
Only clear alarms if setting is disabled.
  • Loading branch information
ReneeVandervelde authored Feb 18, 2024
2 parents aa371b8 + ad71e44 commit 0daa1d1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,20 @@ internal class AlarmScheduler(

private fun scheduleAlarm(alarmParameters: AlarmParameters) {
logger.trace("Removing all alarms")
alarmAccess.removeAlarm(wakeAlarm)
alarmAccess.removeAlarm(sleepAlarm)

if (alarmParameters.settings.wakeAlarm) {
logger.trace("Scheduling Wake alarm for ${alarmParameters.schedule.wake}")
alarmAccess.addAlarm(wakeAlarm, alarmParameters.schedule.wake.instant)
} else {
logger.trace("Wake alarm disabled")
alarmAccess.removeAlarm(wakeAlarm)
}

when {
!alarmParameters.settings.sleepNotifications -> logger.trace("Sleep alarm disabled")
!alarmParameters.settings.sleepNotifications -> {
logger.trace("Sleep alarm disabled")
alarmAccess.removeAlarm(sleepAlarm)
}
clock.now() > alarmParameters.schedule.sleep.instant -> logger.trace("Sleep alarm time has passed")
else -> alarmAccess.addAlarm(sleepAlarm, alarmParameters.schedule.sleep.instant)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,7 @@ class AlarmSchedulerTest {
fakeScheduleAccess.schedule.emit(fakeSchedule)
runCurrent()

assertEquals(2, alarmAccess.clearCalls.size, "Should clear existing alarms")
assertNotNull(alarmAccess.clearCalls.find { it.value == "wake" }, " Wake alarm should be cleared")
assertNotNull(alarmAccess.clearCalls.find { it.value == "sleep" }, " Sleep alarm should be cleared")
assertEquals(0, alarmAccess.clearCalls.size, "Should not clear alarms")
assertEquals(2, alarmAccess.addCalls.size)

val wakeAlarm = alarmAccess.addCalls.find { it.first.value == "wake" }?.second
Expand Down Expand Up @@ -111,6 +109,8 @@ class AlarmSchedulerTest {
runCurrent()

assertEquals(2, alarmAccess.clearCalls.size, "Should clear existing alarms when disabled")
assertNotNull(alarmAccess.clearCalls.find { it.value == "wake" }, " Wake alarm should be cleared")
assertNotNull(alarmAccess.clearCalls.find { it.value == "sleep" }, " Sleep alarm should be cleared")
assertEquals(0, alarmAccess.addCalls.size, "Should not set any alarms when disabled")

job.cancel()
Expand Down

0 comments on commit 0daa1d1

Please sign in to comment.