misc repeatable event getting rewrites

This commit is contained in:
tibbi 2017-05-08 18:43:10 +02:00
parent 9961f46751
commit 331d99ef57
2 changed files with 14 additions and 12 deletions

View File

@ -366,7 +366,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
fun getEventsInBackground(fromTS: Int, toTS: Int, callback: (events: MutableList<Event>) -> Unit) { fun getEventsInBackground(fromTS: Int, toTS: Int, callback: (events: MutableList<Event>) -> Unit) {
val events = ArrayList<Event>() val events = ArrayList<Event>()
val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL" val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS 0"
val selectionArgs = arrayOf(toTS.toString(), fromTS.toString()) val selectionArgs = arrayOf(toTS.toString(), fromTS.toString())
val cursor = getEventsCursor(selection, selectionArgs) val cursor = getEventsCursor(selection, selectionArgs)
events.addAll(fillEvents(cursor)) events.addAll(fillEvents(cursor))
@ -377,17 +377,17 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
callback(filtered) callback(filtered)
} }
private fun getRepeatableEventsFor(fromTS: Int, toTS: Int, getRunningEvents: Boolean = false): List<Event> { private fun getRepeatableEventsFor(fromTS: Int, toTS: Int): List<Event> {
val newEvents = ArrayList<Event>() val newEvents = ArrayList<Event>()
// get repeatable events // get repeatable events
val selection = "$COL_REPEAT_INTERVAL != 0 AND $COL_START_TS < $toTS" val selection = "$COL_REPEAT_INTERVAL != 0 AND $COL_START_TS <= $toTS"
val events = getEvents(selection) val events = getEvents(selection)
val startTimes = SparseIntArray(events.size) val startTimes = SparseIntArray(events.size)
events.forEach { events.forEach {
startTimes.put(it.id, it.startTS) startTimes.put(it.id, it.startTS)
if (it.repeatLimit >= 0) { if (it.repeatLimit >= 0) {
newEvents.addAll(getEventsRepeatingTillDateOrForever(fromTS, toTS, startTimes, getRunningEvents, it)) newEvents.addAll(getEventsRepeatingTillDateOrForever(fromTS, toTS, startTimes, it))
} else { } else {
newEvents.addAll(getEventsRepeatingXTimes(fromTS, toTS, startTimes, it)) newEvents.addAll(getEventsRepeatingXTimes(fromTS, toTS, startTimes, it))
} }
@ -396,11 +396,11 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return newEvents return newEvents
} }
private fun getEventsRepeatingTillDateOrForever(fromTS: Int, toTS: Int, startTimes: SparseIntArray, getRunningEvents: Boolean, event: Event): ArrayList<Event> { private fun getEventsRepeatingTillDateOrForever(fromTS: Int, toTS: Int, startTimes: SparseIntArray, event: Event): ArrayList<Event> {
val events = ArrayList<Event>() val events = ArrayList<Event>()
while (event.startTS <= toTS && (event.repeatLimit == 0 || event.repeatLimit >= event.startTS)) { while (event.startTS <= toTS && (event.repeatLimit == 0 || event.repeatLimit >= event.startTS)) {
if (event.endTS >= fromTS) { if (event.endTS >= fromTS) {
if (event.repeatInterval % WEEK == 0) { if (event.isRepeatingByXWeeks()) {
if (event.startTS.isTsOnProperDay(event)) { if (event.startTS.isTsOnProperDay(event)) {
if (isOnProperWeek(event, startTimes)) { if (isOnProperWeek(event, startTimes)) {
events.add(event.copy()) events.add(event.copy())
@ -409,8 +409,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} else { } else {
events.add(event.copy()) events.add(event.copy())
} }
} else if (getRunningEvents && (event.startTS <= fromTS)) {
events.add(event.copy())
} }
event.addIntervalTime() event.addIntervalTime()
} }
@ -420,7 +418,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
private fun getEventsRepeatingXTimes(fromTS: Int, toTS: Int, startTimes: SparseIntArray, event: Event): ArrayList<Event> { private fun getEventsRepeatingXTimes(fromTS: Int, toTS: Int, startTimes: SparseIntArray, event: Event): ArrayList<Event> {
val events = ArrayList<Event>() val events = ArrayList<Event>()
while (event.repeatLimit < 0 && event.startTS <= toTS) { while (event.repeatLimit < 0 && event.startTS <= toTS) {
if (event.repeatInterval % WEEK == 0) { if (event.isRepeatingByXWeeks()) {
if (event.startTS.isTsOnProperDay(event)) { if (event.startTS.isTsOnProperDay(event)) {
if (isOnProperWeek(event, startTimes)) { if (isOnProperWeek(event, startTimes)) {
if (event.endTS >= fromTS) { if (event.endTS >= fromTS) {
@ -430,7 +428,9 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
} }
} }
} else { } else {
if (event.endTS >= fromTS) {
events.add(event.copy()) events.add(event.copy())
}
event.repeatLimit++ event.repeatLimit++
} }
event.addIntervalTime() event.addIntervalTime()
@ -449,12 +449,12 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
val events = ArrayList<Event>() val events = ArrayList<Event>()
val ts = (System.currentTimeMillis() / 1000).toInt() val ts = (System.currentTimeMillis() / 1000).toInt()
val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ?" val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS 0"
val selectionArgs = arrayOf(ts.toString(), ts.toString()) val selectionArgs = arrayOf(ts.toString(), ts.toString())
val cursor = getEventsCursor(selection, selectionArgs) val cursor = getEventsCursor(selection, selectionArgs)
events.addAll(fillEvents(cursor)) events.addAll(fillEvents(cursor))
events.addAll(getRepeatableEventsFor(ts, ts, true)) events.addAll(getRepeatableEventsFor(ts, ts))
return events return events
} }

View File

@ -43,4 +43,6 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
val isAllDay = flags and FLAG_ALL_DAY != 0 val isAllDay = flags and FLAG_ALL_DAY != 0
fun getReminders() = setOf(reminder1Minutes, reminder2Minutes, reminder3Minutes).filter { it != REMINDER_OFF } fun getReminders() = setOf(reminder1Minutes, reminder2Minutes, reminder3Minutes).filter { it != REMINDER_OFF }
fun isRepeatingByXWeeks() = repeatInterval > 0 && repeatInterval % WEEK == 0
} }