mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-02-17 04:10:45 +01:00
handle event insertions into Room
This commit is contained in:
parent
2e49e9bd12
commit
7ed9a42575
@ -785,7 +785,7 @@ class EventActivity : SimpleActivity() {
|
||||
|
||||
private fun storeEvent(wasRepeatable: Boolean) {
|
||||
if (mEvent.id == 0L || mEvent.id == null) {
|
||||
dbHelper.insert(mEvent, true, this) {
|
||||
EventTypesHelper().insertEvent(applicationContext, this, mEvent, true) {
|
||||
if (DateTime.now().isAfter(mEventStartDateTime.millis)) {
|
||||
if (mEvent.repeatInterval == 0 && mEvent.getReminders().isNotEmpty()) {
|
||||
notifyEvent(mEvent)
|
||||
@ -826,7 +826,7 @@ class EventActivity : SimpleActivity() {
|
||||
repeatLimit = 0
|
||||
}
|
||||
|
||||
dbHelper.insert(mEvent, true, this) {
|
||||
EventTypesHelper().insertEvent(applicationContext, this, mEvent, true) {
|
||||
finish()
|
||||
}
|
||||
}.start()
|
||||
|
@ -505,7 +505,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
||||
eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
|
||||
|
||||
if (!importIDs.contains(contactId)) {
|
||||
dbHelper.insert(event, false) {
|
||||
EventTypesHelper().insertEvent(applicationContext, null, event, false) {
|
||||
eventsAdded++
|
||||
}
|
||||
}
|
||||
|
@ -241,7 +241,7 @@ class CalDAVHandler(val context: Context) {
|
||||
|
||||
if (title.isNotEmpty()) {
|
||||
importIdsMap[event.importId] = event
|
||||
context.dbHelper.insert(event, false) {}
|
||||
EventTypesHelper().insertEvent(context, null, event, false)
|
||||
}
|
||||
}
|
||||
} while (cursor.moveToNext())
|
||||
|
@ -60,58 +60,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
|
||||
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
|
||||
|
||||
fun insert(event: Event, addToCalDAV: Boolean, activity: SimpleActivity? = null, callback: (id: Long) -> Unit) {
|
||||
if (event.startTS > event.endTS) {
|
||||
callback(0)
|
||||
return
|
||||
}
|
||||
|
||||
val eventValues = fillEventValues(event)
|
||||
val id = mDb.insert(MAIN_TABLE_NAME, null, eventValues)
|
||||
event.id = id
|
||||
|
||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||
}
|
||||
|
||||
context.updateWidgets()
|
||||
context.scheduleNextEventReminder(event, this, activity)
|
||||
|
||||
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
|
||||
CalDAVHandler(context).insertCalDAVEvent(event)
|
||||
}
|
||||
|
||||
callback(event.id!!)
|
||||
}
|
||||
|
||||
fun insertEvents(events: ArrayList<Event>, addToCalDAV: Boolean) {
|
||||
mDb.beginTransaction()
|
||||
try {
|
||||
for (event in events) {
|
||||
if (event.startTS > event.endTS) {
|
||||
continue
|
||||
}
|
||||
|
||||
val eventValues = fillEventValues(event)
|
||||
val id = mDb.insert(MAIN_TABLE_NAME, null, eventValues)
|
||||
event.id = id
|
||||
|
||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||
}
|
||||
|
||||
context.scheduleNextEventReminder(event, this)
|
||||
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && context.config.caldavSync) {
|
||||
CalDAVHandler(context).insertCalDAVEvent(event)
|
||||
}
|
||||
}
|
||||
mDb.setTransactionSuccessful()
|
||||
} finally {
|
||||
mDb.endTransaction()
|
||||
context.updateWidgets()
|
||||
}
|
||||
}
|
||||
|
||||
fun update(event: Event, updateAtCalDAV: Boolean, activity: SimpleActivity? = null, callback: (() -> Unit)? = null) {
|
||||
val values = fillEventValues(event)
|
||||
val selection = "$COL_ID = ?"
|
||||
@ -164,7 +112,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||
}
|
||||
}
|
||||
|
||||
insert(childEvent, false) {
|
||||
EventTypesHelper().insertEvent(context, null, childEvent, false) {
|
||||
val childEventId = it
|
||||
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
|
||||
callback(eventRepetitionException)
|
||||
|
@ -2,10 +2,8 @@ package com.simplemobiletools.calendar.pro.helpers
|
||||
|
||||
import android.app.Activity
|
||||
import android.content.Context
|
||||
import com.simplemobiletools.calendar.pro.extensions.config
|
||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
||||
import com.simplemobiletools.calendar.pro.extensions.eventRepetitionExceptionsDB
|
||||
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
|
||||
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.EventType
|
||||
import java.util.*
|
||||
@ -73,4 +71,51 @@ class EventTypesHelper {
|
||||
context.eventRepetitionExceptionsDB.getEventRepetitionExceptions(event.id!!).toMutableList() as ArrayList<String>
|
||||
}
|
||||
}
|
||||
|
||||
fun insertEvent(context: Context, activity: SimpleActivity? = null, event: Event, addToCalDAV: Boolean, callback: ((id: Long) -> Unit)? = null) {
|
||||
if (event.startTS > event.endTS) {
|
||||
callback?.invoke(0)
|
||||
return
|
||||
}
|
||||
|
||||
val id = context.eventsDB.insert(event)
|
||||
event.id = id
|
||||
|
||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||
}
|
||||
|
||||
context.updateWidgets()
|
||||
//context.scheduleNextEventReminder(event, this, activity)
|
||||
|
||||
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
|
||||
CalDAVHandler(context).insertCalDAVEvent(event)
|
||||
}
|
||||
|
||||
callback?.invoke(event.id!!)
|
||||
}
|
||||
|
||||
fun insertEvents(activity: Activity, events: ArrayList<Event>, addToCalDAV: Boolean) {
|
||||
try {
|
||||
for (event in events) {
|
||||
if (event.startTS > event.endTS) {
|
||||
continue
|
||||
}
|
||||
|
||||
val id = activity.eventsDB.insert(event)
|
||||
event.id = id
|
||||
|
||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||
activity.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||
}
|
||||
|
||||
//context.scheduleNextEventReminder(event, this)
|
||||
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && activity.config.caldavSync) {
|
||||
CalDAVHandler(activity).insertCalDAVEvent(event)
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
activity.updateWidgets()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -158,7 +158,7 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
if (curRepeatExceptions.isEmpty()) {
|
||||
eventsToInsert.add(event)
|
||||
} else {
|
||||
activity.dbHelper.insert(event, true) {
|
||||
EventTypesHelper().insertEvent(activity, activity, event, true) {
|
||||
for (exceptionTS in curRepeatExceptions) {
|
||||
activity.dbHelper.addEventRepeatException(it, exceptionTS, true)
|
||||
}
|
||||
@ -176,7 +176,7 @@ class IcsImporter(val activity: SimpleActivity) {
|
||||
}
|
||||
}
|
||||
|
||||
activity.dbHelper.insertEvents(eventsToInsert, true)
|
||||
EventTypesHelper().insertEvents(activity, eventsToInsert, true)
|
||||
} catch (e: Exception) {
|
||||
activity.showErrorToast(e, Toast.LENGTH_LONG)
|
||||
eventsFailed++
|
||||
|
@ -1,8 +1,12 @@
|
||||
package com.simplemobiletools.calendar.pro.interfaces
|
||||
|
||||
import androidx.room.Dao
|
||||
import androidx.room.Insert
|
||||
import androidx.room.OnConflictStrategy
|
||||
import com.simplemobiletools.calendar.pro.models.Event
|
||||
|
||||
@Dao
|
||||
interface EventsDao {
|
||||
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
fun insert(event: Event): Long
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user