From 8cbbc2effeb965954a05b521c9292dbb36991f17 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 14 Nov 2018 18:51:57 +0100 Subject: [PATCH] moving event insert/update in Room --- .../calendar/pro/activities/EventActivity.kt | 4 +- .../calendar/pro/helpers/CalDAVHandler.kt | 2 +- .../calendar/pro/helpers/DBHelper.kt | 40 ------------------- .../calendar/pro/helpers/EventsHelper.kt | 21 +++++++++- .../calendar/pro/helpers/IcsImporter.kt | 2 +- .../calendar/pro/interfaces/EventsDao.kt | 2 +- 6 files changed, 24 insertions(+), 47 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 6cd143429..aa630728b 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 @@ -800,7 +800,7 @@ class EventActivity : SimpleActivity() { showEditRepeatingEventDialog() } } else { - dbHelper.update(mEvent, true, this) { + EventsHelper().updateEvent(applicationContext, this, mEvent, true) { finish() } } @@ -811,7 +811,7 @@ class EventActivity : SimpleActivity() { EditRepeatingEventDialog(this) { if (it) { Thread { - dbHelper.update(mEvent, true, this) { + EventsHelper().updateEvent(applicationContext, this, mEvent, true) { finish() } }.start() 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 7e8607609..c9a8668eb 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 @@ -226,7 +226,7 @@ class CalDAVHandler(val context: Context) { if (existingEvent.hashCode() != event.hashCode() && title.isNotEmpty()) { event.id = originalEventId - context.dbHelper.update(event, false) + EventsHelper().updateEvent(context, null, event, false) } } else { // if the event is an exception from another events repeat rule, find the original parent event 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 86c8b4828..31460ffb4 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 @@ -7,7 +7,6 @@ import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import android.text.TextUtils import androidx.collection.LongSparseArray -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 @@ -60,45 +59,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {} - fun update(event: Event, updateAtCalDAV: Boolean, activity: SimpleActivity? = null, callback: (() -> Unit)? = null) { - val values = fillEventValues(event) - val selection = "$COL_ID = ?" - val selectionArgs = arrayOf(event.id.toString()) - mDb.update(MAIN_TABLE_NAME, values, selection, selectionArgs) - - if (event.repeatInterval == 0) { - context.eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!) - } else { - context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition()) - } - - context.updateWidgets() - context.scheduleNextEventReminder(event, this, activity) - if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) { - CalDAVHandler(context).updateCalDAVEvent(event) - } - callback?.invoke() - } - - private fun fillEventValues(event: Event): ContentValues { - return ContentValues().apply { - put(COL_START_TS, event.startTS) - put(COL_END_TS, event.endTS) - put(COL_TITLE, event.title) - put(COL_DESCRIPTION, event.description) - put(COL_REMINDER_MINUTES, event.reminder1Minutes) - put(COL_REMINDER_MINUTES_2, event.reminder2Minutes) - put(COL_REMINDER_MINUTES_3, event.reminder3Minutes) - put(COL_IMPORT_ID, event.importId) - put(COL_FLAGS, event.flags) - put(COL_EVENT_TYPE, event.eventType) - put(COL_PARENT_EVENT_ID, event.parentId) - put(COL_LAST_UPDATED, event.lastUpdated) - put(COL_EVENT_SOURCE, event.source) - put(COL_LOCATION, event.location) - } - } - private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) { val childEvent = getEventWithId(parentEventId) ?: return 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 1c600ce31..2ba51bed3 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 @@ -78,7 +78,7 @@ class EventsHelper { return } - val id = context.eventsDB.insert(event) + val id = context.eventsDB.insertOrUpdate(event) event.id = id if (event.repeatInterval != 0 && event.parentId == 0L) { @@ -102,7 +102,7 @@ class EventsHelper { continue } - val id = activity.eventsDB.insert(event) + val id = activity.eventsDB.insertOrUpdate(event) event.id = id if (event.repeatInterval != 0 && event.parentId == 0L) { @@ -118,4 +118,21 @@ class EventsHelper { activity.updateWidgets() } } + + fun updateEvent(context: Context, activity: Activity? = null, event: Event, updateAtCalDAV: Boolean, callback: (() -> Unit)? = null) { + context.eventsDB.insertOrUpdate(event) + + if (event.repeatInterval == 0) { + context.eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!) + } else { + context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition()) + } + + context.updateWidgets() + //context.scheduleNextEventReminder(event, this, activity) + if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) { + CalDAVHandler(context).updateCalDAVEvent(event) + } + callback?.invoke() + } } 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 5a187db47..14e889fdf 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 @@ -167,7 +167,7 @@ class IcsImporter(val activity: SimpleActivity) { } } else { event.id = eventToUpdate.id - activity.dbHelper.update(event, true) + EventsHelper().updateEvent(activity, null, event, true) } eventsImported++ resetValues() 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 3e1b8c07b..9309736b7 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 @@ -8,5 +8,5 @@ import com.simplemobiletools.calendar.pro.models.Event @Dao interface EventsDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insert(event: Event): Long + fun insertOrUpdate(event: Event): Long }