recheck alarm state after editing

This commit is contained in:
tibbi 2018-03-06 21:20:10 +01:00
parent 258453d88c
commit b54cf59c3e
1 changed files with 15 additions and 3 deletions

View File

@ -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))
}
}