cancel active alarms before deleting

This commit is contained in:
tibbi 2018-03-28 19:13:20 +02:00
parent 02ea628762
commit a20e1ce8f9
2 changed files with 8 additions and 5 deletions

View File

@ -64,16 +64,14 @@ class AlarmsAdapter(activity: SimpleActivity, var alarms: ArrayList<Alarm>, val
private fun deleteItems() {
val alarmsToRemove = ArrayList<Alarm>()
val alarmIDs = ArrayList<String>()
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) {

View File

@ -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<String>) {
val args = TextUtils.join(", ", ids)
fun deleteAlarms(alarms: ArrayList<Alarm>) {
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)
}