moving the event repetition exceptions into a new table

This commit is contained in:
tibbi 2018-11-14 11:56:56 +01:00
parent aa6ae0811a
commit 4bdd09bea9
6 changed files with 33 additions and 57 deletions

View file

@ -11,6 +11,7 @@ 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
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
@ -33,6 +34,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val COL_EVENT_TYPE = "event_type"
private val COL_LAST_UPDATED = "last_updated"
private val COL_EVENT_SOURCE = "event_source"
private val COL_PARENT_EVENT_ID = "event_parent_id"
private val REPETITIONS_TABLE_NAME = "event_repetitions"
private val COL_EVENT_ID = "event_id"
@ -40,14 +42,10 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private val COL_REPEAT_RULE = "repeat_rule"
private val COL_REPEAT_LIMIT = "repeat_limit"
private val REPEAT_EXCEPTIONS_TABLE_NAME = "event_repeat_exceptions"
private val COL_OCCURRENCE_DAYCODE = "event_occurrence_daycode"
private val COL_PARENT_EVENT_ID = "event_parent_id"
private val mDb = writableDatabase
companion object {
private const val DB_VERSION = 19
private const val DB_VERSION = 1
const val DB_NAME = "events_old.db"
const val REGULAR_EVENT_TYPE_ID = 1L
var dbInstance: DBHelper? = null
@ -67,7 +65,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
"$COL_PARENT_EVENT_ID INTEGER, $COL_LAST_UPDATED INTEGER, $COL_EVENT_SOURCE TEXT, $COL_LOCATION TEXT)")
createRepetitionsTable(db)
createExceptionsTable(db)
}
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {}
@ -77,11 +74,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
"$COL_REPEAT_INTERVAL INTEGER, $COL_REPEAT_LIMIT INTEGER, $COL_REPEAT_RULE INTEGER)")
}
private fun createExceptionsTable(db: SQLiteDatabase) {
db.execSQL("CREATE TABLE $REPEAT_EXCEPTIONS_TABLE_NAME ($COL_ID INTEGER PRIMARY KEY AUTOINCREMENT, $COL_PARENT_EVENT_ID INTEGER, " +
"$COL_OCCURRENCE_DAYCODE INTEGER)")
}
fun insert(event: Event, addToCalDAV: Boolean, activity: SimpleActivity? = null, callback: (id: Long) -> Unit) {
if (event.startTS > event.endTS) {
callback(0)
@ -186,12 +178,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (values: ContentValues) -> Unit) {
val childEvent = getEventWithId(parentEventId)
if (childEvent == null) {
callback(ContentValues())
return
}
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
val childEvent = getEventWithId(parentEventId) ?: return
childEvent.apply {
id = 0
@ -205,11 +193,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
insert(childEvent, false) {
val childEventId = it
val exceptionValues = ContentValues().apply {
put(COL_PARENT_EVENT_ID, parentEventId)
put(COL_OCCURRENCE_DAYCODE, Formatter.getDayCodeFromTS(occurrenceTS))
}
callback(exceptionValues)
val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
callback(eventRepetitionException)
Thread {
if (addToCalDAV && context.config.caldavSync) {
@ -255,9 +240,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val metaSelection = "$COL_EVENT_ID IN ($args)"
mDb.delete(REPETITIONS_TABLE_NAME, metaSelection, null)
val exceptionSelection = "$COL_PARENT_EVENT_ID IN ($args)"
mDb.delete(REPEAT_EXCEPTIONS_TABLE_NAME, exceptionSelection, null)
context.updateWidgets()
// temporary workaround, will be rewritten in Room
@ -304,7 +286,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun addEventRepeatException(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String? = null) {
fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) {
mDb.insert(REPEAT_EXCEPTIONS_TABLE_NAME, null, it)
context.eventRepetitionExceptionsDB.insert(it)
val parentEvent = getEventWithId(parentEventId)
if (parentEvent != null) {
@ -446,7 +428,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
events = events
.asSequence()
.distinct()
.filterNot { getIgnoredOccurrences(it).contains(Formatter.getDayCodeFromTS(it.startTS).toInt()) }
.filterNot { EventTypesHelper().getEventRepetitionIgnoredOccurrences(context, it).contains(Formatter.getDayCodeFromTS(it.startTS)) }
.toMutableList() as ArrayList<Event>
callback(events)
}
@ -750,28 +732,4 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}.start()
}
fun getIgnoredOccurrences(event: Event): ArrayList<Int> {
if (event.repeatInterval == 0) {
return ArrayList()
}
val projection = arrayOf(COL_OCCURRENCE_DAYCODE)
val selection = "$COL_PARENT_EVENT_ID = ?"
val selectionArgs = arrayOf(event.id.toString())
val daycodes = ArrayList<Int>()
var cursor: Cursor? = null
try {
cursor = mDb.query(REPEAT_EXCEPTIONS_TABLE_NAME, projection, selection, selectionArgs, null, null, COL_OCCURRENCE_DAYCODE)
if (cursor?.moveToFirst() == true) {
do {
daycodes.add(cursor.getIntValue(COL_OCCURRENCE_DAYCODE))
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
return daycodes
}
}

View file

@ -4,7 +4,9 @@ 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.models.Event
import com.simplemobiletools.calendar.pro.models.EventType
import java.util.*
@ -63,4 +65,12 @@ class EventTypesHelper {
context.eventTypesDB.deleteEventTypes(typesToDelete)
}
fun getEventRepetitionIgnoredOccurrences(context: Context, event: Event): ArrayList<String> {
return if (event.id == null || event.repeatInterval == 0) {
ArrayList()
} else {
context.eventRepetitionExceptionsDB.getEventRepetitionExceptions(event.id!!).toMutableList() as ArrayList<String>
}
}
}

View file

@ -1,7 +1,6 @@
package com.simplemobiletools.calendar.pro.helpers
import com.simplemobiletools.calendar.pro.R
import com.simplemobiletools.calendar.pro.extensions.dbHelper
import com.simplemobiletools.calendar.pro.extensions.eventTypesDB
import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult.*
import com.simplemobiletools.calendar.pro.models.Event
@ -93,7 +92,7 @@ class IcsExporter {
}
private fun fillIgnoredOccurrences(activity: BaseSimpleActivity, event: Event, out: BufferedWriter) {
activity.dbHelper.getIgnoredOccurrences(event).forEach {
EventTypesHelper().getEventRepetitionIgnoredOccurrences(activity, event).forEach {
out.writeLn("$EXDATE:$it")
}
}

View file

@ -1,8 +1,16 @@
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

@ -8,5 +8,5 @@ import androidx.room.PrimaryKey
@Entity(tableName = "event_repetition_exceptions", indices = [(Index(value = ["id"], unique = true))])
data class EventRepetitionException(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "occurrence_daycode") val daycode: Int,
@ColumnInfo(name = "parent_id") val parentId: Int)
@ColumnInfo(name = "occurrence_daycode") val daycode: String,
@ColumnInfo(name = "event_id") val eventId: Long)

View file

@ -9,12 +9,13 @@ 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.EventTypesHelper
import com.simplemobiletools.calendar.pro.helpers.Formatter
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager
val wakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Simple Calendar")
val wakelock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "simplecalendar:notificationreceiver")
wakelock.acquire(3000)
Thread {
@ -34,7 +35,7 @@ class NotificationReceiver : BroadcastReceiver() {
return
}
if (!context.dbHelper.getIgnoredOccurrences(event).contains(Formatter.getDayCodeFromTS(event.startTS).toInt())) {
if (!EventTypesHelper().getEventRepetitionIgnoredOccurrences(context, event).contains(Formatter.getDayCodeFromTS(event.startTS))) {
context.notifyEvent(event)
}
context.scheduleNextEventReminder(event, context.dbHelper)