properly delete just 1 event occurrence from event activity

This commit is contained in:
tibbi 2017-03-05 19:39:29 +01:00
parent b286c62873
commit a4ee9eee52
2 changed files with 19 additions and 2 deletions

View File

@ -260,10 +260,11 @@ class EventActivity : SimpleActivity(), DBHelper.EventUpdateListener {
private fun deleteEvent() { private fun deleteEvent() {
DeleteEventDialog(this, arrayListOf(mEvent.id)) { DeleteEventDialog(this, arrayListOf(mEvent.id)) {
val db = DBHelper.newInstance(applicationContext, null)
if (it) { if (it) {
DBHelper.newInstance(applicationContext, this).deleteEvents(arrayOf(mEvent.id.toString())) db.deleteEvents(arrayOf(mEvent.id.toString()))
} else { } else {
db.deleteEventOccurrence(mEvent.id, mEventOccurrenceTS)
} }
finish() finish()
} }

View File

@ -181,6 +181,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
put(COL_IMPORT_ID, event.importId) put(COL_IMPORT_ID, event.importId)
put(COL_FLAGS, event.flags) put(COL_FLAGS, event.flags)
put(COL_EVENT_TYPE, event.eventType) put(COL_EVENT_TYPE, event.eventType)
put(COL_PARENT_EVENT_ID, 0)
} }
} }
@ -229,6 +230,13 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
} }
private fun fillExceptionValues(parentEventId: Int, occurrenceTS: Int): ContentValues {
return ContentValues().apply {
put(COL_PARENT_EVENT_ID, parentEventId)
put(COL_OCCURRENCE_TIMESTAMP, occurrenceTS)
}
}
fun getEventTypeIdWithTitle(title: String): Int { fun getEventTypeIdWithTitle(title: String): Int {
val cols = arrayOf(COL_TYPE_ID) val cols = arrayOf(COL_TYPE_ID)
val selection = "$COL_TYPE_TITLE = ? COLLATE NOCASE" val selection = "$COL_TYPE_TITLE = ? COLLATE NOCASE"
@ -278,6 +286,14 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
mEventsListener?.eventsDeleted(ids.size) mEventsListener?.eventsDeleted(ids.size)
} }
fun deleteEventOccurrence(parentEventId: Int, occurrenceTS: Int) {
val exceptionSelection = "$COL_PARENT_EVENT_ID = $parentEventId AND $COL_OCCURRENCE_TIMESTAMP = $occurrenceTS"
mDb.delete(EXCEPTIONS_TABLE_NAME, exceptionSelection, null)
val values = fillExceptionValues(parentEventId, occurrenceTS)
mDb.insert(EXCEPTIONS_TABLE_NAME, null, values)
}
fun deleteEventTypes(ids: ArrayList<Int>, callback: (deletedCnt: Int) -> Unit) { fun deleteEventTypes(ids: ArrayList<Int>, callback: (deletedCnt: Int) -> Unit) {
var deleteIds = ids var deleteIds = ids
if (ids.contains(DBHelper.REGULAR_EVENT_TYPE_ID)) if (ids.contains(DBHelper.REGULAR_EVENT_TYPE_ID))