moving event insert/update in Room

This commit is contained in:
tibbi 2018-11-14 18:51:57 +01:00
parent 977b137a9b
commit 8cbbc2effe
6 changed files with 24 additions and 47 deletions

View File

@ -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()

View File

@ -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

View File

@ -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

View File

@ -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()
}
}

View File

@ -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()

View File

@ -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
}