mirror of
https://github.com/SimpleMobileTools/Simple-Calendar.git
synced 2025-06-05 21:59:17 +02:00
handle event insertions into Room
This commit is contained in:
@@ -785,7 +785,7 @@ class EventActivity : SimpleActivity() {
|
|||||||
|
|
||||||
private fun storeEvent(wasRepeatable: Boolean) {
|
private fun storeEvent(wasRepeatable: Boolean) {
|
||||||
if (mEvent.id == 0L || mEvent.id == null) {
|
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 (DateTime.now().isAfter(mEventStartDateTime.millis)) {
|
||||||
if (mEvent.repeatInterval == 0 && mEvent.getReminders().isNotEmpty()) {
|
if (mEvent.repeatInterval == 0 && mEvent.getReminders().isNotEmpty()) {
|
||||||
notifyEvent(mEvent)
|
notifyEvent(mEvent)
|
||||||
@@ -826,7 +826,7 @@ class EventActivity : SimpleActivity() {
|
|||||||
repeatLimit = 0
|
repeatLimit = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
dbHelper.insert(mEvent, true, this) {
|
EventTypesHelper().insertEvent(applicationContext, this, mEvent, true) {
|
||||||
finish()
|
finish()
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
|
@@ -505,7 +505,7 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener {
|
|||||||
eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
|
eventType = eventTypeId, source = source, lastUpdated = lastUpdated)
|
||||||
|
|
||||||
if (!importIDs.contains(contactId)) {
|
if (!importIDs.contains(contactId)) {
|
||||||
dbHelper.insert(event, false) {
|
EventTypesHelper().insertEvent(applicationContext, null, event, false) {
|
||||||
eventsAdded++
|
eventsAdded++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -241,7 +241,7 @@ class CalDAVHandler(val context: Context) {
|
|||||||
|
|
||||||
if (title.isNotEmpty()) {
|
if (title.isNotEmpty()) {
|
||||||
importIdsMap[event.importId] = event
|
importIdsMap[event.importId] = event
|
||||||
context.dbHelper.insert(event, false) {}
|
EventTypesHelper().insertEvent(context, null, event, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (cursor.moveToNext())
|
} 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) {}
|
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) {
|
fun update(event: Event, updateAtCalDAV: Boolean, activity: SimpleActivity? = null, callback: (() -> Unit)? = null) {
|
||||||
val values = fillEventValues(event)
|
val values = fillEventValues(event)
|
||||||
val selection = "$COL_ID = ?"
|
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 childEventId = it
|
||||||
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
|
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
|
||||||
callback(eventRepetitionException)
|
callback(eventRepetitionException)
|
||||||
|
@@ -2,10 +2,8 @@ package com.simplemobiletools.calendar.pro.helpers
|
|||||||
|
|
||||||
import android.app.Activity
|
import android.app.Activity
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import com.simplemobiletools.calendar.pro.extensions.config
|
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.pro.extensions.dbHelper
|
import com.simplemobiletools.calendar.pro.extensions.*
|
||||||
import com.simplemobiletools.calendar.pro.extensions.eventRepetitionExceptionsDB
|
|
||||||
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
|
|
||||||
import com.simplemobiletools.calendar.pro.models.Event
|
import com.simplemobiletools.calendar.pro.models.Event
|
||||||
import com.simplemobiletools.calendar.pro.models.EventType
|
import com.simplemobiletools.calendar.pro.models.EventType
|
||||||
import java.util.*
|
import java.util.*
|
||||||
@@ -73,4 +71,51 @@ class EventTypesHelper {
|
|||||||
context.eventRepetitionExceptionsDB.getEventRepetitionExceptions(event.id!!).toMutableList() as ArrayList<String>
|
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()) {
|
if (curRepeatExceptions.isEmpty()) {
|
||||||
eventsToInsert.add(event)
|
eventsToInsert.add(event)
|
||||||
} else {
|
} else {
|
||||||
activity.dbHelper.insert(event, true) {
|
EventTypesHelper().insertEvent(activity, activity, event, true) {
|
||||||
for (exceptionTS in curRepeatExceptions) {
|
for (exceptionTS in curRepeatExceptions) {
|
||||||
activity.dbHelper.addEventRepeatException(it, exceptionTS, true)
|
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) {
|
} catch (e: Exception) {
|
||||||
activity.showErrorToast(e, Toast.LENGTH_LONG)
|
activity.showErrorToast(e, Toast.LENGTH_LONG)
|
||||||
eventsFailed++
|
eventsFailed++
|
||||||
|
@@ -1,8 +1,12 @@
|
|||||||
package com.simplemobiletools.calendar.pro.interfaces
|
package com.simplemobiletools.calendar.pro.interfaces
|
||||||
|
|
||||||
import androidx.room.Dao
|
import androidx.room.Dao
|
||||||
|
import androidx.room.Insert
|
||||||
|
import androidx.room.OnConflictStrategy
|
||||||
|
import com.simplemobiletools.calendar.pro.models.Event
|
||||||
|
|
||||||
@Dao
|
@Dao
|
||||||
interface EventsDao {
|
interface EventsDao {
|
||||||
|
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||||
|
fun insert(event: Event): Long
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user