moving some event repetition related things into Room

This commit is contained in:
tibbi 2018-11-14 23:08:59 +01:00
parent c91a32ccf4
commit 92862d1193
7 changed files with 56 additions and 58 deletions

View file

@ -680,7 +680,7 @@ class EventActivity : SimpleActivity() {
DeleteEventDialog(this, arrayListOf(mEvent.id!!), mEvent.repeatInterval > 0) {
Thread {
when (it) {
DELETE_SELECTED_OCCURRENCE -> dbHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
DELETE_SELECTED_OCCURRENCE -> EventsHelper(applicationContext).addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
DELETE_FUTURE_OCCURRENCES -> EventsHelper(applicationContext).addEventRepeatLimit(mEvent.id!!, mEventOccurrenceTS)
DELETE_ALL_OCCURRENCES -> EventsHelper(applicationContext).deleteEvent(mEvent.id!!, true)
}
@ -817,7 +817,7 @@ class EventActivity : SimpleActivity() {
}.start()
} else {
Thread {
dbHelper.addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
EventsHelper(applicationContext).addEventRepeatException(mEvent.id!!, mEventOccurrenceTS, true)
mEvent.apply {
parentId = id!!.toLong()
id = null

View file

@ -448,7 +448,7 @@ fun Context.handleEventDeleting(eventIds: List<Long>, timestamps: List<Int>, act
when (action) {
DELETE_SELECTED_OCCURRENCE -> {
eventIds.forEachIndexed { index, value ->
dbHelper.addEventRepeatException(value, timestamps[index], true)
EventsHelper(this).addEventRepeatException(value, timestamps[index], true)
}
}
DELETE_FUTURE_OCCURRENCES -> {

View file

@ -9,7 +9,10 @@ import android.provider.CalendarContract
import android.provider.CalendarContract.Reminders
import android.util.SparseIntArray
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.eventsDB
import com.simplemobiletools.calendar.pro.extensions.refreshCalDAVCalendars
import com.simplemobiletools.calendar.pro.extensions.scheduleCalDAVSync
import com.simplemobiletools.calendar.pro.models.CalDAVCalendar
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
@ -152,7 +155,7 @@ class CalDAVHandler(val context: Context) {
private fun fetchCalDAVCalendarEvents(calendarId: Int, eventTypeId: Long, activity: SimpleActivity?) {
val importIdsMap = HashMap<String, Event>()
val fetchedEventIds = ArrayList<String>()
val existingEvents = context.dbHelper.getEventsFromCalDAVCalendar(calendarId)
val existingEvents = context.eventsDB.getEventsFromCalDAVCalendar("$CALDAV-$calendarId")
existingEvents.forEach {
importIdsMap[it.importId] = it
}
@ -232,7 +235,7 @@ class CalDAVHandler(val context: Context) {
val parentEventId = context.eventsDB.getEventIdWithImportId(parentImportId)
if (parentEventId != null) {
event.parentId = parentEventId
context.dbHelper.addEventRepeatException(parentEventId, (originalInstanceTime / 1000).toInt(), false, event.importId)
EventsHelper(context).addEventRepeatException(parentEventId, (originalInstanceTime / 1000).toInt(), false, event.importId)
}
}

View file

@ -8,7 +8,6 @@ import android.text.TextUtils
import androidx.collection.LongSparseArray
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.commons.extensions.getStringValue
@ -56,48 +55,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
val childEvent = context.eventsDB.getEventWithId(parentEventId) ?: return
childEvent.apply {
id = 0
parentId = parentEventId
startTS = 0
endTS = 0
if (childImportId != null) {
importId = childImportId
}
}
EventsHelper(context).insertEvent(null, childEvent, false) {
val childEventId = it
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
callback(eventRepetitionException)
Thread {
if (addToCalDAV && context.config.caldavSync) {
val parentEvent = context.eventsDB.getEventWithId(parentEventId)
if (parentEvent != null) {
val newId = CalDAVHandler(context).insertEventRepeatException(parentEvent, occurrenceTS)
val newImportId = "${parentEvent.source}-$newId"
context.eventsDB.updateEventImportIdAndSource(newImportId, parentEvent.source, childEventId)
}
}
}.start()
}
}
fun addEventRepeatException(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String? = null) {
fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) {
context.eventRepetitionExceptionsDB.insert(it)
val parentEvent = context.eventsDB.getEventWithId(parentEventId)
if (parentEvent != null) {
context.scheduleNextEventReminder(parentEvent, this)
}
}
}
fun getEvents(fromTS: Int, toTS: Int, eventId: Long = -1L, applyTypeFilter: Boolean = true, callback: (events: ArrayList<Event>) -> Unit) {
Thread {
getEventsInBackground(fromTS, toTS, eventId, applyTypeFilter, callback)
@ -350,13 +307,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return events
}
fun getEventsFromCalDAVCalendar(calendarId: Int): List<Event> {
val selection = "$COL_EVENT_SOURCE = ?"
val selectionArgs = arrayOf("$CALDAV-$calendarId")
val cursor = getEventsCursor(selection, selectionArgs)
return fillEvents(cursor)
}
private fun getEventsCursor(selection: String = "", selectionArgs: Array<String>? = null): Cursor? {
return mDb.query(MAIN_TABLE_NAME, allColumns, selection, selectionArgs, null, null, COL_START_TS)
}

View file

@ -5,6 +5,7 @@ import android.content.Context
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
import com.simplemobiletools.calendar.pro.models.EventType
import java.util.*
@ -208,4 +209,46 @@ class EventsHelper(val context: Context) {
}
}.start()
}
fun addEventRepeatException(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String? = null) {
EventsHelper(context).fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) {
context.eventRepetitionExceptionsDB.insert(it)
val parentEvent = eventsDB.getEventWithId(parentEventId)
if (parentEvent != null) {
//context.scheduleNextEventReminder(parentEvent, this)
}
}
}
fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
val childEvent = eventsDB.getEventWithId(parentEventId) ?: return
childEvent.apply {
id = 0
parentId = parentEventId
startTS = 0
endTS = 0
if (childImportId != null) {
importId = childImportId
}
}
insertEvent(null, childEvent, false) {
val childEventId = it
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
callback(eventRepetitionException)
Thread {
if (addToCalDAV && config.caldavSync) {
val parentEvent = eventsDB.getEventWithId(parentEventId)
if (parentEvent != null) {
val newId = CalDAVHandler(context).insertEventRepeatException(parentEvent, occurrenceTS)
val newImportId = "${parentEvent.source}-$newId"
eventsDB.updateEventImportIdAndSource(newImportId, parentEvent.source, childEventId)
}
}
}.start()
}
}
}

View file

@ -3,7 +3,6 @@ package com.simplemobiletools.calendar.pro.helpers
import android.widget.Toast
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.extensions.eventsDB
import com.simplemobiletools.calendar.pro.helpers.IcsImporter.ImportResult.*
import com.simplemobiletools.calendar.pro.models.Event
@ -161,7 +160,7 @@ class IcsImporter(val activity: SimpleActivity) {
} else {
EventsHelper(activity).insertEvent(activity, event, true) {
for (exceptionTS in curRepeatExceptions) {
activity.dbHelper.addEventRepeatException(it, exceptionTS, true)
EventsHelper(activity).addEventRepeatException(it, exceptionTS, true)
}
existingEvents.add(event)
}

View file

@ -47,6 +47,9 @@ interface EventsDao {
@Query("SELECT * FROM events WHERE import_id != \"\"")
fun getEventsWithImportIds(): List<Event>
@Query("SELECT * FROM events WHERE source = :source")
fun getEventsFromCalDAVCalendar(source: String): List<Event>
@Query("SELECT id FROM events WHERE source = :source AND import_id != \"\"")
fun getCalDAVCalendarEvents(source: String): List<Long>