properly delete alarms from the database

This commit is contained in:
tibbi 2018-03-08 21:27:47 +01:00
parent 05eae929d1
commit 660ebc90f0
2 changed files with 11 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import android.view.ViewGroup
import com.simplemobiletools.clock.R
import com.simplemobiletools.clock.activities.SimpleActivity
import com.simplemobiletools.clock.extensions.config
import com.simplemobiletools.clock.extensions.dbHelper
import com.simplemobiletools.clock.extensions.formatAlarmTime
import com.simplemobiletools.clock.interfaces.ToggleAlarmInterface
import com.simplemobiletools.clock.models.Alarm
@ -64,13 +65,16 @@ 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)
}
private fun setupView(view: View, alarm: Alarm) {

View File

@ -5,6 +5,7 @@ import android.content.Context
import android.database.Cursor
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import android.text.TextUtils
import com.simplemobiletools.clock.extensions.createNewAlarm
import com.simplemobiletools.clock.models.Alarm
import com.simplemobiletools.commons.extensions.getIntValue
@ -76,6 +77,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)
val selection = "$ALARMS_TABLE_NAME.$COL_ID IN ($args)"
mDb.delete(ALARMS_TABLE_NAME, selection, null)
}
fun getAlarmWithId(id: Int) = getAlarms().firstOrNull { it.id == id }
private fun fillAlarmContentValues(alarm: Alarm): ContentValues {