From a20e1ce8f953a2f30506cbef70b047953abf9264 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 28 Mar 2018 19:13:20 +0200 Subject: [PATCH] cancel active alarms before deleting --- .../simplemobiletools/clock/adapters/AlarmsAdapter.kt | 4 +--- .../com/simplemobiletools/clock/helpers/DBHelper.kt | 9 +++++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/adapters/AlarmsAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/clock/adapters/AlarmsAdapter.kt index 7e393c69..19a05a9e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/adapters/AlarmsAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/adapters/AlarmsAdapter.kt @@ -64,16 +64,14 @@ class AlarmsAdapter(activity: SimpleActivity, var alarms: ArrayList, val private fun deleteItems() { val alarmsToRemove = ArrayList() - val alarmIDs = ArrayList() selectedPositions.sortedDescending().forEach { val alarm = alarms[it] - alarmIDs.add(alarm.id.toString()) alarmsToRemove.add(alarm) } alarms.removeAll(alarmsToRemove) removeSelectedItems() - activity.dbHelper.deleteAlarms(alarmIDs) + activity.dbHelper.deleteAlarms(alarmsToRemove) } private fun setupView(view: View, alarm: Alarm) { diff --git a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt index 1fe25e99..9e6be86a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/clock/helpers/DBHelper.kt @@ -6,6 +6,7 @@ import android.database.Cursor import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.text.TextUtils +import com.simplemobiletools.clock.extensions.cancelAlarmClock import com.simplemobiletools.clock.extensions.createNewAlarm import com.simplemobiletools.clock.models.Alarm import com.simplemobiletools.commons.extensions.getIntValue @@ -77,8 +78,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return mDb.update(ALARMS_TABLE_NAME, values, selection, selectionArgs) == 1 } - fun deleteAlarms(ids: ArrayList) { - val args = TextUtils.join(", ", ids) + fun deleteAlarms(alarms: ArrayList) { + alarms.filter { it.isEnabled }.forEach { + context.cancelAlarmClock(it) + } + + val args = TextUtils.join(", ", alarms.map { it.id.toString() }) val selection = "$ALARMS_TABLE_NAME.$COL_ID IN ($args)" mDb.delete(ALARMS_TABLE_NAME, selection, null) }