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.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
import com.simplemobiletools.calendar.pro.models.EventType import com.simplemobiletools.calendar.pro.models.EventType
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue 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_EVENT_TYPE = "event_type"
private val COL_LAST_UPDATED = "last_updated" private val COL_LAST_UPDATED = "last_updated"
private val COL_EVENT_SOURCE = "event_source" 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 REPETITIONS_TABLE_NAME = "event_repetitions"
private val COL_EVENT_ID = "event_id" 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_RULE = "repeat_rule"
private val COL_REPEAT_LIMIT = "repeat_limit" 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 private val mDb = writableDatabase
companion object { companion object {
private const val DB_VERSION = 19 private const val DB_VERSION = 1
const val DB_NAME = "events_old.db" const val DB_NAME = "events_old.db"
const val REGULAR_EVENT_TYPE_ID = 1L const val REGULAR_EVENT_TYPE_ID = 1L
var dbInstance: DBHelper? = null 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)") "$COL_PARENT_EVENT_ID INTEGER, $COL_LAST_UPDATED INTEGER, $COL_EVENT_SOURCE TEXT, $COL_LOCATION TEXT)")
createRepetitionsTable(db) createRepetitionsTable(db)
createExceptionsTable(db)
} }
override fun onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int) {} 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)") "$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) { fun insert(event: Event, addToCalDAV: Boolean, activity: SimpleActivity? = null, callback: (id: Long) -> Unit) {
if (event.startTS > event.endTS) { if (event.startTS > event.endTS) {
callback(0) 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) { private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
val childEvent = getEventWithId(parentEventId) val childEvent = getEventWithId(parentEventId) ?: return
if (childEvent == null) {
callback(ContentValues())
return
}
childEvent.apply { childEvent.apply {
id = 0 id = 0
@ -205,11 +193,8 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
insert(childEvent, false) { insert(childEvent, false) {
val childEventId = it val childEventId = it
val exceptionValues = ContentValues().apply { val eventRepetitionException = EventRepetitionException(null, Formatter.getDayCodeFromTS(occurrenceTS), parentEventId)
put(COL_PARENT_EVENT_ID, parentEventId) callback(eventRepetitionException)
put(COL_OCCURRENCE_DAYCODE, Formatter.getDayCodeFromTS(occurrenceTS))
}
callback(exceptionValues)
Thread { Thread {
if (addToCalDAV && context.config.caldavSync) { 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)" val metaSelection = "$COL_EVENT_ID IN ($args)"
mDb.delete(REPETITIONS_TABLE_NAME, metaSelection, null) 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() context.updateWidgets()
// temporary workaround, will be rewritten in Room // 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) { fun addEventRepeatException(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String? = null) {
fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) { fillExceptionValues(parentEventId, occurrenceTS, addToCalDAV, childImportId) {
mDb.insert(REPEAT_EXCEPTIONS_TABLE_NAME, null, it) context.eventRepetitionExceptionsDB.insert(it)
val parentEvent = getEventWithId(parentEventId) val parentEvent = getEventWithId(parentEventId)
if (parentEvent != null) { if (parentEvent != null) {
@ -446,7 +428,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
events = events events = events
.asSequence() .asSequence()
.distinct() .distinct()
.filterNot { getIgnoredOccurrences(it).contains(Formatter.getDayCodeFromTS(it.startTS).toInt()) } .filterNot { EventTypesHelper().getEventRepetitionIgnoredOccurrences(context, it).contains(Formatter.getDayCodeFromTS(it.startTS)) }
.toMutableList() as ArrayList<Event> .toMutableList() as ArrayList<Event>
callback(events) callback(events)
} }
@ -750,28 +732,4 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
}.start() }.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 android.content.Context
import com.simplemobiletools.calendar.pro.extensions.config import com.simplemobiletools.calendar.pro.extensions.config
import com.simplemobiletools.calendar.pro.extensions.dbHelper 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.extensions.eventTypesDB
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.*
@ -63,4 +65,12 @@ class EventTypesHelper {
context.eventTypesDB.deleteEventTypes(typesToDelete) 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 package com.simplemobiletools.calendar.pro.helpers
import com.simplemobiletools.calendar.pro.R 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.extensions.eventTypesDB
import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult.* import com.simplemobiletools.calendar.pro.helpers.IcsExporter.ExportResult.*
import com.simplemobiletools.calendar.pro.models.Event import com.simplemobiletools.calendar.pro.models.Event
@ -93,7 +92,7 @@ class IcsExporter {
} }
private fun fillIgnoredOccurrences(activity: BaseSimpleActivity, event: Event, out: BufferedWriter) { private fun fillIgnoredOccurrences(activity: BaseSimpleActivity, event: Event, out: BufferedWriter) {
activity.dbHelper.getIgnoredOccurrences(event).forEach { EventTypesHelper().getEventRepetitionIgnoredOccurrences(activity, event).forEach {
out.writeLn("$EXDATE:$it") out.writeLn("$EXDATE:$it")
} }
} }

View File

@ -1,8 +1,16 @@
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 androidx.room.Query
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
@Dao @Dao
interface EventRepetitionExceptionsDao { 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))]) @Entity(tableName = "event_repetition_exceptions", indices = [(Index(value = ["id"], unique = true))])
data class EventRepetitionException( data class EventRepetitionException(
@PrimaryKey(autoGenerate = true) var id: Long?, @PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "occurrence_daycode") val daycode: Int, @ColumnInfo(name = "occurrence_daycode") val daycode: String,
@ColumnInfo(name = "parent_id") val parentId: Int) @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.scheduleNextEventReminder
import com.simplemobiletools.calendar.pro.extensions.updateListWidget import com.simplemobiletools.calendar.pro.extensions.updateListWidget
import com.simplemobiletools.calendar.pro.helpers.EVENT_ID import com.simplemobiletools.calendar.pro.helpers.EVENT_ID
import com.simplemobiletools.calendar.pro.helpers.EventTypesHelper
import com.simplemobiletools.calendar.pro.helpers.Formatter import com.simplemobiletools.calendar.pro.helpers.Formatter
class NotificationReceiver : BroadcastReceiver() { class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
val powerManager = context.getSystemService(Context.POWER_SERVICE) as PowerManager 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) wakelock.acquire(3000)
Thread { Thread {
@ -34,7 +35,7 @@ class NotificationReceiver : BroadcastReceiver() {
return 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.notifyEvent(event)
} }
context.scheduleNextEventReminder(event, context.dbHelper) context.scheduleNextEventReminder(event, context.dbHelper)