mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 04:10:45 +01:00
moving some delete functions into Room
This commit is contained in:
parent
96d290a635
commit
5b466ec841
@ -682,7 +682,7 @@ class EventActivity : SimpleActivity() {
|
|||||||
when (it) {
|
when (it) {
|
||||||
DELETE_SELECTED_OCCURRENCE -> dbHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
|
DELETE_SELECTED_OCCURRENCE -> dbHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
|
||||||
DELETE_FUTURE_OCCURRENCES -> dbHelper.addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS)
|
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 {
|
runOnUiThread {
|
||||||
finish()
|
finish()
|
||||||
@ -776,8 +776,8 @@ class EventActivity : SimpleActivity() {
|
|||||||
|
|
||||||
// recreate the event if it was moved in a different CalDAV calendar
|
// recreate the event if it was moved in a different CalDAV calendar
|
||||||
if (mEvent.id != 0L && oldSource != newSource) {
|
if (mEvent.id != 0L && oldSource != newSource) {
|
||||||
dbHelper.deleteEvents(arrayListOf(mEvent.id!!), true)
|
EventsHelper(this).deleteEvent(mEvent.id!!, true)
|
||||||
mEvent.id = 0
|
mEvent.id = null
|
||||||
}
|
}
|
||||||
|
|
||||||
storeEvent(wasRepeatable)
|
storeEvent(wasRepeatable)
|
||||||
|
@ -6,7 +6,10 @@ import android.media.AudioManager
|
|||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import com.simplemobiletools.calendar.pro.R
|
import com.simplemobiletools.calendar.pro.R
|
||||||
import com.simplemobiletools.calendar.pro.dialogs.SelectCalendarsDialog
|
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.helpers.*
|
||||||
import com.simplemobiletools.calendar.pro.models.EventType
|
import com.simplemobiletools.calendar.pro.models.EventType
|
||||||
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
|
||||||
@ -248,7 +251,7 @@ class SettingsActivity : SimpleActivity() {
|
|||||||
settings_delete_all_events_holder.setOnClickListener {
|
settings_delete_all_events_holder.setOnClickListener {
|
||||||
ConfirmationDialog(this, messageId = R.string.delete_all_events_confirmation) {
|
ConfirmationDialog(this, messageId = R.string.delete_all_events_confirmation) {
|
||||||
Thread {
|
Thread {
|
||||||
dbHelper.deleteAllEvents()
|
EventsHelper(applicationContext).deleteAllEvents()
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,9 @@ import com.simplemobiletools.calendar.pro.R
|
|||||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.pro.dialogs.DeleteEventDialog
|
import com.simplemobiletools.calendar.pro.dialogs.DeleteEventDialog
|
||||||
import com.simplemobiletools.calendar.pro.extensions.config
|
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.handleEventDeleting
|
||||||
import com.simplemobiletools.calendar.pro.extensions.shareEvents
|
import com.simplemobiletools.calendar.pro.extensions.shareEvents
|
||||||
import com.simplemobiletools.calendar.pro.helpers.Formatter
|
import com.simplemobiletools.calendar.pro.helpers.*
|
||||||
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.models.Event
|
import com.simplemobiletools.calendar.pro.models.Event
|
||||||
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter
|
||||||
import com.simplemobiletools.commons.extensions.adjustAlpha
|
import com.simplemobiletools.commons.extensions.adjustAlpha
|
||||||
@ -144,7 +140,7 @@ class DayEventsAdapter(activity: SimpleActivity, val events: ArrayList<Event>, r
|
|||||||
|
|
||||||
Thread {
|
Thread {
|
||||||
val nonRepeatingEventIDs = eventsToDelete.asSequence().filter { it.repeatInterval == 0 }.mapNotNull { it.id }.toMutableList()
|
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()
|
val repeatingEventIDs = eventsToDelete.asSequence().filter { it.repeatInterval != 0 }.mapNotNull { it.id }.toList()
|
||||||
activity.handleEventDeleting(repeatingEventIDs, timestamps, it)
|
activity.handleEventDeleting(repeatingEventIDs, timestamps, it)
|
||||||
|
@ -7,7 +7,6 @@ import com.simplemobiletools.calendar.pro.R
|
|||||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.pro.dialogs.DeleteEventDialog
|
import com.simplemobiletools.calendar.pro.dialogs.DeleteEventDialog
|
||||||
import com.simplemobiletools.calendar.pro.extensions.config
|
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.handleEventDeleting
|
||||||
import com.simplemobiletools.calendar.pro.extensions.shareEvents
|
import com.simplemobiletools.calendar.pro.extensions.shareEvents
|
||||||
import com.simplemobiletools.calendar.pro.helpers.*
|
import com.simplemobiletools.calendar.pro.helpers.*
|
||||||
@ -208,7 +207,7 @@ class EventListAdapter(activity: SimpleActivity, var listItems: ArrayList<ListIt
|
|||||||
|
|
||||||
Thread {
|
Thread {
|
||||||
val nonRepeatingEventIDs = eventsToDelete.filter { !it.isRepeatable }.mapNotNull { it.id }.toMutableList()
|
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 }
|
val repeatingEventIDs = eventsToDelete.filter { it.isRepeatable }.map { it.id }
|
||||||
activity.handleEventDeleting(repeatingEventIDs, timestamps, it)
|
activity.handleEventDeleting(repeatingEventIDs, timestamps, it)
|
||||||
|
@ -457,7 +457,7 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Int>, act
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
DELETE_ALL_OCCURRENCES -> {
|
DELETE_ALL_OCCURRENCES -> {
|
||||||
dbHelper.deleteEvents(eventIds.toMutableList(), true)
|
EventsHelper(this).deleteEvents(eventIds.toMutableList(), true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -259,7 +259,7 @@ class CalDAVHandler(val context: Context) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.dbHelper.deleteEvents(eventIdsToDelete.toMutableList(), false)
|
EventsHelper(context).deleteEvents(eventIdsToDelete.toMutableList(), false)
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressLint("MissingPermission")
|
@SuppressLint("MissingPermission")
|
||||||
@ -358,7 +358,7 @@ class CalDAVHandler(val context: Context) {
|
|||||||
fun deleteCalDAVCalendarEvents(calendarId: Long) {
|
fun deleteCalDAVCalendarEvents(calendarId: Long) {
|
||||||
val events = context.dbHelper.getCalDAVCalendarEvents(calendarId)
|
val events = context.dbHelper.getCalDAVCalendarEvents(calendarId)
|
||||||
val eventIds = events.mapNotNull { it.id }.toMutableList()
|
val eventIds = events.mapNotNull { it.id }.toMutableList()
|
||||||
context.dbHelper.deleteEvents(eventIds, false)
|
EventsHelper(context).deleteEvents(eventIds, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteCalDAVEvent(event: Event) {
|
fun deleteCalDAVEvent(event: Event) {
|
||||||
|
@ -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> {
|
fun getCalDAVCalendarEvents(calendarId: Long): List<Event> {
|
||||||
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = (?)"
|
val selection = "$MAIN_TABLE_NAME.$COL_EVENT_SOURCE = (?)"
|
||||||
val selectionArgs = arrayOf("$CALDAV-$calendarId")
|
val selectionArgs = arrayOf("$CALDAV-$calendarId")
|
||||||
@ -177,7 +126,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
|||||||
val cursor = getEventsCursor(selection, selectionArgs)
|
val cursor = getEventsCursor(selection, selectionArgs)
|
||||||
val events = fillEvents(cursor)
|
val events = fillEvents(cursor)
|
||||||
val eventIDs = events.mapNotNull { it.id }.toMutableList()
|
val eventIDs = events.mapNotNull { it.id }.toMutableList()
|
||||||
deleteEvents(eventIDs, true)
|
EventsHelper(context).deleteEvents(eventIDs, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun resetEventsWithType(eventTypeId: Long) {
|
fun resetEventsWithType(eventTypeId: Long) {
|
||||||
|
@ -135,4 +135,36 @@ class EventsHelper(val context: Context) {
|
|||||||
}
|
}
|
||||||
callback?.invoke()
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -13,15 +13,24 @@ interface EventsDao {
|
|||||||
@Query("SELECT * FROM events WHERE id = :id")
|
@Query("SELECT * FROM events WHERE id = :id")
|
||||||
fun getEventWithId(id: Long): Event?
|
fun getEventWithId(id: Long): Event?
|
||||||
|
|
||||||
|
@Query("SELECT id FROM events")
|
||||||
|
fun getEventIds(): List<Long>
|
||||||
|
|
||||||
@Query("SELECT id FROM events WHERE import_id = :importId")
|
@Query("SELECT id FROM events WHERE import_id = :importId")
|
||||||
fun getEventIdWithImportId(importId: String): Long?
|
fun getEventIdWithImportId(importId: String): Long?
|
||||||
|
|
||||||
@Query("SELECT id FROM events WHERE import_id LIKE :importId")
|
@Query("SELECT id FROM events WHERE import_id LIKE :importId")
|
||||||
fun getEventIdWithLastImportId(importId: String): Long?
|
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)")
|
@Query("SELECT * FROM events WHERE id IN (:ids)")
|
||||||
fun getEventsWithIds(ids: List<Long>): List<Event>
|
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\'")
|
@Query("SELECT * FROM events WHERE source = \'$SOURCE_CONTACT_BIRTHDAY\'")
|
||||||
fun getBirthdays(): List<Event>
|
fun getBirthdays(): List<Event>
|
||||||
|
|
||||||
@ -33,4 +42,7 @@ interface EventsDao {
|
|||||||
|
|
||||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
fun insertOrUpdate(event: Event): Long
|
fun insertOrUpdate(event: Event): Long
|
||||||
|
|
||||||
|
@Query("DELETE FROM events WHERE id IN (:ids)")
|
||||||
|
fun deleteEvents(ids: List<Long>)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user