moving some delete functions into Room

This commit is contained in:
tibbi 2018-11-14 21:25:43 +01:00
parent 96d290a635
commit 5b466ec841
9 changed files with 59 additions and 68 deletions

View File

@ -682,7 +682,7 @@ class EventActivity : SimpleActivity() {
when (it) {
DELETE_SELECTED_OCCURRENCE -> dbHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
DELETE_FUTURE_OCCURRENCES -> dbHelper.addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS)
DELETE_ALL_OCCURRENCES -> dbHelper.deleteEvents(arrayListOf(mEvent.id!!), true)
DELETE_ALL_OCCURRENCES -> EventsHelper(applicationContext).deleteEvent(mEvent.id!!, true)
}
runOnUiThread {
finish()
@ -776,8 +776,8 @@ class EventActivity : SimpleActivity() {
// recreate the event if it was moved in a different CalDAV calendar
if (mEvent.id != 0L && oldSource != newSource) {
dbHelper.deleteEvents(arrayListOf(mEvent.id!!), true)
mEvent.id = 0
EventsHelper(this).deleteEvent(mEvent.id!!, true)
mEvent.id = null
}
storeEvent(wasRepeatable)

View File

@ -6,7 +6,10 @@ import android.media.AudioManager
import android.os.Bundle
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.dialogs.SelectCalendarsDialog
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
import com.simplemobiletools.calendar.pro.extensions.getSyncedCalDAVCalendars
import com.simplemobiletools.calendar.pro.extensions.updateWidgets
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.models.EventType
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
@ -248,7 +251,7 @@ class SettingsActivity : SimpleActivity() {
settings_delete_all_events_holder.setOnClickListener {
ConfirmationDialog(this, messageId = R.string.delete_all_events_confirmation) {
Thread {
dbHelper.deleteAllEvents()
EventsHelper(applicationContext).deleteAllEvents()
}.start()
}
}

View File

@ -7,13 +7,9 @@ import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.extensions.handleEventDeleting
import com.simplemobiletools.calendar.pro.extensions.shareEvents
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.helpers.ITEM_EVENT
import com.simplemobiletools.calendar.pro.helpers.ITEM_EVENT_SIMPLE
import com.simplemobiletools.calendar.pro.helpers.LOW_ALPHA
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
import com.simplemobiletools.commons.extensions.adjustAlpha
@ -144,7 +140,7 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList<Event>, r
Thread {
val nonRepeatingEventIDs = eventsToDelete.asSequence().filter { it.repeatInterval == 0 }.mapNotNull { it.id }.toMutableList()
activity.dbHelper.deleteEvents(nonRepeatingEventIDs, true)
EventsHelper(activity).deleteEvents(nonRepeatingEventIDs, true)
val repeatingEventIDs = eventsToDelete.asSequence().filter { it.repeatInterval != 0 }.mapNotNull { it.id }.toList()
activity.handleEventDeleting(repeatingEventIDs, timestamps, it)

View File

@ -7,7 +7,6 @@ import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.dialogs.DeleteEventDialog
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.extensions.handleEventDeleting
import com.simplemobiletools.calendar.pro.extensions.shareEvents
import com.simplemobiletools.calendar.pro.helpers.*
@ -208,7 +207,7 @@ class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList<ListIt
Thread {
val nonRepeatingEventIDs = eventsToDelete.filter { !it.isRepeatable }.mapNotNull { it.id }.toMutableList()
activity.dbHelper.deleteEvents(nonRepeatingEventIDs, true)
EventsHelper(activity).deleteEvents(nonRepeatingEventIDs, true)
val repeatingEventIDs = eventsToDelete.filter { it.isRepeatable }.map { it.id }
activity.handleEventDeleting(repeatingEventIDs, timestamps, it)

View File

@ -457,7 +457,7 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Int>, act
}
}
DELETE_ALL_OCCURRENCES -> {
dbHelper.deleteEvents(eventIds.toMutableList(), true)
EventsHelper(this).deleteEvents(eventIds.toMutableList(), true)
}
}
}

View File

@ -259,7 +259,7 @@ class CalDAVHandler(val context: Context) {
}
}
context.dbHelper.deleteEvents(eventIdsToDelete.toMutableList(), false)
EventsHelper(context).deleteEvents(eventIdsToDelete.toMutableList(), false)
}
@SuppressLint("MissingPermission")
@ -358,7 +358,7 @@ class CalDAVHandler(val context: Context) {
fun deleteCalDAVCalendarEvents(calendarId: Long) {
val events = context.dbHelper.getCalDAVCalendarEvents(calendarId)
val eventIds = events.mapNotNull { it.id }.toMutableList()
context.dbHelper.deleteEvents(eventIds, false)
EventsHelper(context).deleteEvents(eventIds, false)
}
fun deleteCalDAVEvent(event: Event) {

View File

@ -90,57 +90,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
fun deleteAllEvents() {
val cursor = getEventsCursor()
val events = fillEvents(cursor).mapNotNull { it.id }.toMutableList()
deleteEvents(events, true)
}
fun deleteEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
val eventIds = TextUtils.join(", ", ids)
val selection = "$MAIN_TABLE_NAME.$COL_ID IN ($eventIds)"
val cursor = getEventsCursor(selection)
val eventsWithImportId = fillEvents(cursor).filter { it.importId.isNotEmpty() }
mDb.delete(MAIN_TABLE_NAME, selection, null)
// temporary workaround, will be rewritten in Room
ids.filterNot { it == 0L }.forEach {
context.cancelNotification(it)
}
if (deleteFromCalDAV && context.config.caldavSync) {
eventsWithImportId.forEach {
CalDAVHandler(context).deleteCalDAVEvent(it)
}
}
deleteChildEvents(ids, deleteFromCalDAV)
context.updateWidgets()
}
private fun deleteChildEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
val projection = arrayOf(COL_ID)
val selection = "$COL_PARENT_EVENT_ID IN ($ids)"
val childIds = ArrayList<Long>()
var cursor: Cursor? = null
try {
cursor = mDb.query(MAIN_TABLE_NAME, projection, selection, null, null, null, null)
if (cursor?.moveToFirst() == true) {
do {
childIds.add(cursor.getLongValue(COL_ID))
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
if (childIds.isNotEmpty()) {
deleteEvents(childIds, deleteFromCalDAV)
}
}
fun getCalDAVCalendarEvents(calendarId: Long): List<Event> {
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = (?)"
val selectionArgs = arrayOf("$CALDAV-$calendarId")
@ -177,7 +126,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val cursor = getEventsCursor(selection, selectionArgs)
val events = fillEvents(cursor)
val eventIDs = events.mapNotNull { it.id }.toMutableList()
deleteEvents(eventIDs, true)
EventsHelper(context).deleteEvents(eventIDs, true)
}
fun resetEventsWithType(eventTypeId: Long) {

View File

@ -135,4 +135,36 @@ class EventsHelper(val context: Context) {
}
callback?.invoke()
}
fun deleteAllEvents() {
val eventIds = context.eventsDB.getEventIds().toMutableList()
EventsHelper(context).deleteEvents(eventIds, true)
}
fun deleteEvent(id: Long, deleteFromCalDAV: Boolean) = deleteEvents(arrayListOf(id), deleteFromCalDAV)
fun deleteEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
val eventsWithImportId = context.eventsDB.getEventsByIdsWithImportIds(ids)
context.eventsDB.deleteEvents(ids)
ids.forEach {
context.cancelNotification(it)
}
if (deleteFromCalDAV && context.config.caldavSync) {
eventsWithImportId.forEach {
CalDAVHandler(context).deleteCalDAVEvent(it)
}
}
deleteChildEvents(ids, deleteFromCalDAV)
context.updateWidgets()
}
fun deleteChildEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
val childIds = context.eventsDB.getEventIdsWithParentIds(ids).toMutableList()
if (childIds.isNotEmpty()) {
deleteEvents(childIds, deleteFromCalDAV)
}
}
}

View File

@ -13,15 +13,24 @@ interface EventsDao {
@Query("SELECT * FROM events WHERE id = :id")
fun getEventWithId(id: Long): Event?
@Query("SELECT id FROM events")
fun getEventIds(): List<Long>
@Query("SELECT id FROM events WHERE import_id = :importId")
fun getEventIdWithImportId(importId: String): Long?
@Query("SELECT id FROM events WHERE import_id LIKE :importId")
fun getEventIdWithLastImportId(importId: String): Long?
@Query("SELECT * FROM events WHERE id IN (:ids) AND import_id != \"\"")
fun getEventsByIdsWithImportIds(ids: List<Long>): List<Event>
@Query("SELECT * FROM events WHERE id IN (:ids)")
fun getEventsWithIds(ids: List<Long>): List<Event>
@Query("SELECT id FROM events WHERE parent_id IN (:parentIds)")
fun getEventIdsWithParentIds(parentIds: List<Long>): List<Long>
@Query("SELECT * FROM events WHERE source = \'$SOURCE_CONTACT_BIRTHDAY\'")
fun getBirthdays(): List<Event>
@ -33,4 +42,7 @@ interface EventsDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(event: Event): Long
@Query("DELETE FROM events WHERE id IN (:ids)")
fun deleteEvents(ids: List<Long>)
}