delete events belonging to a particular event type if selected so

This commit is contained in:
tibbi 2017-06-07 23:07:18 +02:00
parent 67135b0122
commit c5070b8bc4
2 changed files with 16 additions and 3 deletions

View file

@ -58,7 +58,7 @@ class ManageEventTypesActivity : SimpleActivity(), DeleteEventTypesListener {
toast(R.string.cannot_delete_default_type)
}
dbHelper.deleteEventTypes(ids) {
dbHelper.deleteEventTypes(ids, deleteEvents) {
if (it > 0) {
getEventTypes()
} else {

View file

@ -311,7 +311,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mDb.insert(EXCEPTIONS_TABLE_NAME, null, values)
}
fun deleteEventTypes(ids: ArrayList<Int>, callback: (deletedCnt: Int) -> Unit) {
fun deleteEventTypes(ids: ArrayList<Int>, deleteEvents: Boolean, callback: (deletedCnt: Int) -> Unit) {
var deleteIds = ids
if (ids.contains(DBHelper.REGULAR_EVENT_TYPE_ID))
deleteIds = ids.filter { it != DBHelper.REGULAR_EVENT_TYPE_ID } as ArrayList<Int>
@ -323,14 +323,27 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return
for (eventTypeId in deleteIds) {
if (deleteEvents) {
deleteEventsWithType(eventTypeId)
} else {
resetEventsWithType(eventTypeId)
}
}
val args = TextUtils.join(", ", deleteIds)
val selection = "$COL_TYPE_ID IN ($args)"
callback.invoke(mDb.delete(TYPES_TABLE_NAME, selection, null))
}
private fun deleteEventsWithType(eventTypeId: Int) {
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_TYPE = ?"
val selectionArgs = arrayOf(eventTypeId.toString())
val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor)
val eventIDs = Array(events.size, { i -> (events[i].id.toString()) })
deleteEvents(eventIDs)
}
private fun resetEventsWithType(eventTypeId: Int) {
val values = ContentValues()
values.put(COL_EVENT_TYPE, REGULAR_EVENT_TYPE_ID)