diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt index 4c0181bf3..da858038d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/activities/ManageEventTypesActivity.kt @@ -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 { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index 7c0fb5f06..c66cae669 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -311,7 +311,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont mDb.insert(EXCEPTIONS_TABLE_NAME, null, values) } - fun deleteEventTypes(ids: ArrayList, callback: (deletedCnt: Int) -> Unit) { + fun deleteEventTypes(ids: ArrayList, 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 @@ -323,7 +323,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont return for (eventTypeId in deleteIds) { - resetEventsWithType(eventTypeId) + if (deleteEvents) { + deleteEventsWithType(eventTypeId) + } else { + resetEventsWithType(eventTypeId) + } } val args = TextUtils.join(", ", deleteIds) @@ -331,6 +335,15 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont 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)