From b54cf59c3e78d6dd48124020cab8e8dfe7fa6831 Mon Sep 17 00:00:00 2001 From: tibbi Date: Tue, 6 Mar 2018 21:20:10 +0100 Subject: [PATCH] recheck alarm state after editing --- .../clock/fragments/AlarmFragment.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt index a168eaf..7a3a35b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/fragments/AlarmFragment.kt @@ -73,6 +73,7 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { private fun openEditAlarm(alarm: Alarm) { EditAlarmDialog(activity as SimpleActivity, alarm) { setupAlarms() + checkAlarmState(alarm) } } @@ -80,14 +81,20 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { if (context!!.dbHelper.updateAlarmEnabledState(id, isEnabled)) { val alarm = alarms.firstOrNull { it.id == id } ?: return alarm.isEnabled = isEnabled - if (isEnabled) { - getClosestTriggerTimestamp(alarm) - } + checkAlarmState(alarm) } else { activity!!.toast(R.string.unknown_error_occurred) } } + private fun checkAlarmState(alarm: Alarm) { + if (alarm.isEnabled) { + getClosestTriggerTimestamp(alarm) + } else { + cancelAlarmClock(alarm) + } + } + private fun getClosestTriggerTimestamp(alarm: Alarm) { val calendar = Calendar.getInstance() calendar.firstDayOfWeek = Calendar.MONDAY @@ -147,4 +154,9 @@ class AlarmFragment : Fragment(), ToggleAlarmInterface { intent.putExtra(ALARM_ID, alarm.id) return PendingIntent.getBroadcast(context, alarm.id, intent, PendingIntent.FLAG_UPDATE_CURRENT) } + + private fun cancelAlarmClock(alarm: Alarm) { + val alarmManager = context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager + alarmManager.cancel(getPendingIntent(alarm)) + } }