removing the redundant EventRepetitionExceptions table too

This commit is contained in:
tibbi 2018-11-16 17:01:49 +01:00
parent bd4687bfb3
commit 0b198a3c3a
12 changed files with 52 additions and 75 deletions

View file

@ -4,27 +4,26 @@ import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import androidx.sqlite.db.SupportSQLiteDatabase
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.helpers.Converters
import com.simplemobiletools.calendar.pro.helpers.REGULAR_EVENT_TYPE_ID
import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao
import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao
import com.simplemobiletools.calendar.pro.interfaces.EventsDao
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
import com.simplemobiletools.calendar.pro.models.EventType
import java.util.concurrent.Executors
@Database(entities = [Event::class, EventType::class, EventRepetitionException::class], version = 1)
@Database(entities = [Event::class, EventType::class], version = 1)
@TypeConverters(Converters::class)
abstract class EventsDatabase : RoomDatabase() {
abstract fun EventsDao(): EventsDao
abstract fun EventTypesDao(): EventTypesDao
abstract fun EventRepetitionExceptionsDao(): EventRepetitionExceptionsDao
companion object {
private var db: EventsDatabase? = null

View file

@ -28,7 +28,6 @@ import com.simplemobiletools.calendar.pro.activities.SnoozeReminderActivity
import com.simplemobiletools.calendar.pro.databases.EventsDatabase
import com.simplemobiletools.calendar.pro.helpers.*
import com.simplemobiletools.calendar.pro.helpers.Formatter
import com.simplemobiletools.calendar.pro.interfaces.EventRepetitionExceptionsDao
import com.simplemobiletools.calendar.pro.interfaces.EventTypesDao
import com.simplemobiletools.calendar.pro.interfaces.EventsDao
import com.simplemobiletools.calendar.pro.models.*
@ -48,7 +47,6 @@ import java.util.*
val Context.config: Config get() = Config.newInstance(applicationContext)
val Context.eventsDB: EventsDao get() = EventsDatabase.getInstance(applicationContext).EventsDao()
val Context.eventTypesDB: EventTypesDao get() = EventsDatabase.getInstance(applicationContext).EventTypesDao()
val Context.eventRepetitionExceptionsDB: EventRepetitionExceptionsDao get() = EventsDatabase.getInstance(applicationContext).EventRepetitionExceptionsDao()
val Context.eventsHelper: EventsHelper get() = EventsHelper(this)
val Context.calDAVHelper: CalDAVHelper get() = CalDAVHelper(this)

View file

@ -202,7 +202,7 @@ class CalDAVHelper(val context: Context) {
val repeatRule = Parser().parseRepeatInterval(rrule, startTS)
val event = Event(null, startTS, endTS, title, location, description, reminders.getOrElse(0) { -1 },
reminders.getOrElse(1) { -1 }, reminders.getOrElse(2) { -1 }, repeatRule.repeatInterval, repeatRule.repeatRule,
repeatRule.repeatLimit, importId, allDay, eventTypeId, source = source)
repeatRule.repeatLimit, ArrayList(), importId, allDay, eventTypeId, source = source)
if (event.getIsAllDay()) {
event.startTS = Formatter.getShiftedImportTimestamp(event.startTS)
@ -228,6 +228,11 @@ class CalDAVHelper(val context: Context) {
eventsHelper.updateEvent(null, event, false)
}
} else {
if (title.isNotEmpty()) {
importIdsMap[event.importId] = event
eventsHelper.insertEvent(null, event, false)
}
// if the event is an exception from another events repeat rule, find the original parent event
if (originalInstanceTime != 0L) {
val parentImportId = "$source-$originalId"
@ -237,11 +242,6 @@ class CalDAVHelper(val context: Context) {
eventsHelper.addEventRepeatException(parentEventId, originalInstanceTime / 1000L, false, event.importId)
}
}
if (title.isNotEmpty()) {
importIdsMap[event.importId] = event
eventsHelper.insertEvent(null, event, false)
}
}
} while (cursor.moveToNext())
}

View file

@ -0,0 +1,16 @@
package com.simplemobiletools.calendar.pro.helpers
import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
class Converters {
private val gson = Gson()
private val stringType = object : TypeToken<List<String>>() {}.type
@TypeConverter
fun jsonToStringList(value: String) = gson.fromJson<ArrayList<String>>(value, stringType)
@TypeConverter
fun stringListToJson(list: ArrayList<String>) = gson.toJson(list)
}

View file

@ -6,7 +6,6 @@ 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
import com.simplemobiletools.calendar.pro.models.EventType
class EventsHelper(val context: Context) {
@ -69,14 +68,6 @@ class EventsHelper(val context: Context) {
eventTypesDB.deleteEventTypes(typesToDelete)
}
fun getEventRepetitionIgnoredOccurrences(event: Event): ArrayList<String> {
return if (event.id == null || event.repeatInterval == 0) {
ArrayList()
} else {
context.eventRepetitionExceptionsDB.getEventRepetitionExceptions(event.id!!).toMutableList() as ArrayList<String>
}
}
fun insertEvent(activity: SimpleActivity? = null, event: Event, addToCalDAV: Boolean, callback: ((id: Long) -> Unit)? = null) {
if (event.startTS > event.endTS) {
callback?.invoke(0)
@ -196,21 +187,14 @@ class EventsHelper(val context: Context) {
}
fun addEventRepeatException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String? = null) {
fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) {
context.eventRepetitionExceptionsDB.insert(it)
val parentEvent = eventsDB.getEventWithId(parentEventId)
if (parentEvent != null) {
context.scheduleNextEventReminder(parentEvent)
}
}
fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId)
}
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean, childImportId: String?) {
val childEvent = eventsDB.getEventWithId(parentEventId) ?: return
childEvent.apply {
id = 0
id = null
parentId = parentEventId
startTS = 0
endTS = 0
@ -221,17 +205,17 @@ class EventsHelper(val context: Context) {
insertEvent(null, childEvent, false) {
val childEventId = it
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
callback(eventRepetitionException)
Thread {
val parentEvent = eventsDB.getEventWithId(parentEventId) ?: return@Thread
val parentEventRepetitionExceptions = parentEvent.repetitionExceptions
parentEventRepetitionExceptions.add(Formatter.getDayCodeFromTS(occurrenceTS))
eventsDB.updateEventRepetitionExceptions(parentEventRepetitionExceptions, parentEventId)
context.scheduleNextEventReminder(parentEvent)
if (addToCalDAV && config.caldavSync) {
val parentEvent = eventsDB.getEventWithId(parentEventId)
if (parentEvent != null) {
val newId = context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS)
val newImportId = "${parentEvent.source}-$newId"
eventsDB.updateEventImportIdAndSource(newImportId, parentEvent.source, childEventId)
}
val newId = context.calDAVHelper.insertEventRepeatException(parentEvent, occurrenceTS)
val newImportId = "${parentEvent.source}-$newId"
eventsDB.updateEventImportIdAndSource(newImportId, parentEvent.source, childEventId)
}
}.start()
}
@ -265,7 +249,7 @@ class EventsHelper(val context: Context) {
events = events
.asSequence()
.distinct()
.filterNot { context.eventsHelper.getEventRepetitionIgnoredOccurrences(it).contains(Formatter.getDayCodeFromTS(it.startTS)) }
.filterNot { it.repetitionExceptions.contains(Formatter.getDayCodeFromTS(it.startTS)) }
.toMutableList() as ArrayList<Event>
val eventTypeColors = LongSparseArray<Int>()

View file

@ -2,7 +2,6 @@ package com.simplemobiletools.calendar.pro.helpers
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
import com.simplemobiletools.calendar.pro.extensions.eventsHelper
import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.commons.activities.BaseSimpleActivity
@ -59,7 +58,7 @@ class IcsExporter {
Parser().getRepeatCode(event).let { if (it.isNotEmpty()) out.writeLn("$RRULE$it") }
fillReminders(event, out)
fillIgnoredOccurrences(activity, event, out)
fillIgnoredOccurrences(event, out)
eventsExported++
out.writeLn(END_EVENT)
@ -92,8 +91,8 @@ class IcsExporter {
}
}
private fun fillIgnoredOccurrences(activity: BaseSimpleActivity, event: Event, out: BufferedWriter) {
activity.eventsHelper.getEventRepetitionIgnoredOccurrences(event).forEach {
private fun fillIgnoredOccurrences(event: Event, out: BufferedWriter) {
event.repetitionExceptions.forEach {
out.writeLn("$EXDATE:$it")
}
}

View file

@ -143,7 +143,7 @@ class IcsImporter(val activity: SimpleActivity) {
val source = if (calDAVCalendarId == 0 || eventType?.isSyncedEventType() == false) SOURCE_IMPORTED_ICS else "$CALDAV-$calDAVCalendarId"
val event = Event(0, curStart, curEnd, curTitle, curLocation, curDescription, curReminderMinutes.getOrElse(0) { -1 },
curReminderMinutes.getOrElse(1) { -1 }, curReminderMinutes.getOrElse(2) { -1 }, curRepeatInterval, curRepeatRule,
curRepeatLimit, curImportId, curFlags, curEventTypeId, 0, curLastModified, source)
curRepeatLimit, ArrayList(), curImportId, curFlags, curEventTypeId, 0, curLastModified, source)
if (event.getIsAllDay() && curEnd > curStart) {
event.endTS -= DAY

View file

@ -1,16 +0,0 @@
package com.simplemobiletools.calendar.pro.interfaces
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
@Dao
interface EventRepetitionExceptionsDao {
@Query("SELECT occurrence_daycode FROM event_repetition_exceptions WHERE event_id = :id")
fun getEventRepetitionExceptions(id: Long): List<String>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insert(eventRepetitionException: EventRepetitionException)
}

View file

@ -99,6 +99,9 @@ interface EventsDao {
@Query("UPDATE events SET repeat_limit = :repeatLimit WHERE id = :id")
fun updateEventRepetitionLimit(repeatLimit: Long, id: Long)
@Query("UPDATE events SET repetition_exceptions = :repetitionExceptions WHERE id = :id")
fun updateEventRepetitionExceptions(repetitionExceptions: ArrayList<String>, id: Long)
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(event: Event): Long

View file

@ -25,6 +25,7 @@ data class Event(
@ColumnInfo(name = "repeat_interval") var repeatInterval: Int = 0,
@ColumnInfo(name = "repeat_rule") var repeatRule: Int = 0,
@ColumnInfo(name = "repeat_limit") var repeatLimit: Long = 0L,
@ColumnInfo(name = "repetition_exceptions") var repetitionExceptions: ArrayList<String> = ArrayList(),
@ColumnInfo(name = "import_id") var importId: String = "",
@ColumnInfo(name = "flags") var flags: Int = 0,
@ColumnInfo(name = "event_type") var eventType: Long = REGULAR_EVENT_TYPE_ID,

View file

@ -1,10 +0,0 @@
package com.simplemobiletools.calendar.pro.models
import androidx.room.*
@Entity(tableName = "event_repetition_exceptions", indices = [(Index(value = ["id"], unique = true))],
foreignKeys = [ForeignKey(entity = Event::class, onDelete = ForeignKey.CASCADE, parentColumns = ["id"], childColumns = ["event_id"])])
data class EventRepetitionException(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "occurrence_daycode") val daycode: String,
@ColumnInfo(name = "event_id") val eventId: Long)

View file

@ -4,7 +4,10 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.PowerManager
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.extensions.eventsDB
import com.simplemobiletools.calendar.pro.extensions.notifyEvent
import com.simplemobiletools.calendar.pro.extensions.scheduleNextEventReminder
import com.simplemobiletools.calendar.pro.extensions.updateListWidget
import com.simplemobiletools.calendar.pro.helpers.EVENT_ID
import com.simplemobiletools.calendar.pro.helpers.Formatter
@ -31,7 +34,7 @@ class NotificationReceiver : BroadcastReceiver() {
return
}
if (!context.eventsHelper.getEventRepetitionIgnoredOccurrences(event).contains(Formatter.getDayCodeFromTS(event.startTS))) {
if (!event.repetitionExceptions.contains(Formatter.getDayCodeFromTS(event.startTS))) {
context.notifyEvent(event)
}
context.scheduleNextEventReminder(event)