From 92862d1193883c1722fb84b3e6392dfa33854045 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 14 Nov 2018 23:08:59 +0100 Subject: [PATCH] moving some event repetition related things into Room --- .../calendar/pro/activities/EventActivity.kt | 4 +- .../calendar/pro/extensions/Context.kt | 2 +- .../calendar/pro/helpers/CalDAVHandler.kt | 9 ++-- .../calendar/pro/helpers/DBHelper.kt | 50 ------------------- .../calendar/pro/helpers/EventsHelper.kt | 43 ++++++++++++++++ .../calendar/pro/helpers/IcsImporter.kt | 3 +- .../calendar/pro/interfaces/EventsDao.kt | 3 ++ 7 files changed, 56 insertions(+), 58 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt index 2ded3cc41..45f82fb2a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/EventActivity.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 0840f0c4f..f8bfec4f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -448,7 +448,7 @@ fun Context.handleEventDeleting(eventIds: List, timestamps: List, 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 -> { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHandler.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHandler.kt index 6c1076e78..d6049affb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHandler.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/CalDAVHandler.kt @@ -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() val fetchedEventIds = ArrayList() - 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) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt index 851f69995..5ecabd2f0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt @@ -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) -> 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 { - 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? = null): Cursor? { return mDb.query(MAIN_TABLE_NAME, allColumns, selection, selectionArgs, null, null, COL_START_TS) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index 7cb53c44f..5b271fc39 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -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() + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt index 59fcca212..1ade71755 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/IcsImporter.kt @@ -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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt index 24bcabb8e..bc6b950fa 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -47,6 +47,9 @@ interface EventsDao { @Query("SELECT * FROM events WHERE import_id != \"\"") fun getEventsWithImportIds(): List + @Query("SELECT * FROM events WHERE source = :source") + fun getEventsFromCalDAVCalendar(source: String): List + @Query("SELECT id FROM events WHERE source = :source AND import_id != \"\"") fun getCalDAVCalendarEvents(source: String): List