improve the way we are checking repeatable events
This commit is contained in:
parent
ef90a0c113
commit
11aa45fce6
|
@ -150,12 +150,7 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||
fun getEvents(fromTS: Int, toTS: Int, callback: GetEventsListener?) {
|
||||
Thread({
|
||||
val events = ArrayList<Event>()
|
||||
var ts = fromTS
|
||||
while (ts <= toTS) {
|
||||
events.addAll(getEventsFor(ts))
|
||||
ts += Constants.DAY
|
||||
}
|
||||
|
||||
events.addAll(getEventsFor(fromTS, toTS))
|
||||
val selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL"
|
||||
val selectionArgs = arrayOf(toTS.toString(), fromTS.toString())
|
||||
val cursor = getEventsCursor(selection, selectionArgs)
|
||||
|
@ -165,61 +160,36 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||
}).start()
|
||||
}
|
||||
|
||||
private fun getEventsFor(ts: Int): List<Event> {
|
||||
private fun getEventsFor(fromTS: Int, toTS: Int): List<Event> {
|
||||
val newEvents = ArrayList<Event>()
|
||||
val dayExclusive = Constants.DAY - 1
|
||||
val dayEnd = ts + dayExclusive
|
||||
val dateTime = Formatter.getDateTimeFromTS(ts)
|
||||
|
||||
// get daily, weekly and biweekly events
|
||||
var selection = "($COL_REPEAT_INTERVAL = ${Constants.DAY} OR $COL_REPEAT_INTERVAL = ${Constants.WEEK} OR " +
|
||||
"$COL_REPEAT_INTERVAL = ${Constants.BIWEEK}) AND ($dayEnd - $COL_REPEAT_START) % $COL_REPEAT_INTERVAL BETWEEN 0 AND $dayExclusive"
|
||||
newEvents.addAll(getEvents(selection, ts))
|
||||
|
||||
// get monthly events
|
||||
selection = "$COL_REPEAT_INTERVAL = ${Constants.MONTH} AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd"
|
||||
newEvents.addAll(getEvents(selection, ts))
|
||||
|
||||
// get yearly events
|
||||
selection = "$COL_REPEAT_INTERVAL = ${Constants.YEAR} AND $COL_REPEAT_MONTH = ${dateTime.monthOfYear}" +
|
||||
" AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd"
|
||||
newEvents.addAll(getEvents(selection, ts))
|
||||
// get repeatable events
|
||||
val selection = "$COL_REPEAT_INTERVAL != 0 AND $COL_START_TS < $toTS"
|
||||
val events = getEvents(selection)
|
||||
for (e in events) {
|
||||
while (e.startTS < toTS) {
|
||||
if (e.startTS > fromTS) {
|
||||
val newEvent = Event(e.id, e.startTS, e.endTS, e.title, e.description, e.reminderMinutes, e.repeatInterval)
|
||||
newEvents.add(newEvent)
|
||||
}
|
||||
e.addIntervalTime()
|
||||
}
|
||||
}
|
||||
|
||||
return newEvents
|
||||
}
|
||||
|
||||
private fun getEvents(selection: String, ts: Int): List<Event> {
|
||||
private fun getEvents(selection: String): List<Event> {
|
||||
val events = ArrayList<Event>()
|
||||
val cursor = getEventsCursor(selection, null)
|
||||
if (cursor != null) {
|
||||
val currEvents = fillEvents(cursor)
|
||||
for (e in currEvents) {
|
||||
updateEventTimes(e, ts)
|
||||
}
|
||||
events.addAll(currEvents)
|
||||
}
|
||||
|
||||
return events
|
||||
}
|
||||
|
||||
private fun updateEventTimes(e: Event, ts: Int) {
|
||||
val periods = (ts - e.startTS + Constants.DAY) / e.repeatInterval
|
||||
val currStart = Formatter.getDateTimeFromTS(e.startTS)
|
||||
val newStart: DateTime
|
||||
newStart = when (e.repeatInterval) {
|
||||
Constants.DAY -> currStart.plusDays(periods)
|
||||
Constants.WEEK -> currStart.plusWeeks(periods)
|
||||
Constants.BIWEEK -> currStart.plusWeeks(periods * 2)
|
||||
Constants.MONTH -> currStart.plusMonths(periods)
|
||||
else -> currStart.plusYears(periods)
|
||||
}
|
||||
|
||||
val newStartTS = (newStart.millis / 1000).toInt()
|
||||
val newEndTS = newStartTS + (e.endTS - e.startTS)
|
||||
e.startTS = newStartTS
|
||||
e.endTS = newEndTS
|
||||
}
|
||||
|
||||
fun getEventsAtReboot(): List<Event> {
|
||||
val selection = "$COL_REMINDER_MINUTES != -1 AND ($COL_START_TS > ? OR $COL_REPEAT_INTERVAL != 0)"
|
||||
val selectionArgs = arrayOf((DateTime.now().millis / 1000).toString())
|
||||
|
@ -271,8 +241,4 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
|||
interface GetEventsListener {
|
||||
fun gotEvents(events: MutableList<Event>)
|
||||
}
|
||||
|
||||
interface YearlyEventsListener {
|
||||
fun yearlyEvents(events: MutableList<String>)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
package com.simplemobiletools.calendar.models
|
||||
|
||||
import com.simplemobiletools.calendar.Constants
|
||||
import com.simplemobiletools.calendar.Formatter
|
||||
import org.joda.time.DateTime
|
||||
import java.io.Serializable
|
||||
|
||||
class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var title: String = "", var description: String = "",
|
||||
|
@ -12,4 +15,20 @@ class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var title
|
|||
companion object {
|
||||
private val serialVersionUID = -32456795132344616L
|
||||
}
|
||||
|
||||
fun addIntervalTime() {
|
||||
val currStart = Formatter.getDateTimeFromTS(startTS)
|
||||
val newStart: DateTime
|
||||
newStart = when (repeatInterval) {
|
||||
Constants.DAY -> currStart.plusDays(1)
|
||||
Constants.WEEK -> currStart.plusWeeks(1)
|
||||
Constants.BIWEEK -> currStart.plusWeeks(2)
|
||||
Constants.MONTH -> currStart.plusMonths(1)
|
||||
else -> currStart.plusYears(1)
|
||||
}
|
||||
val newStartTS = (newStart.millis / 1000).toInt()
|
||||
val newEndTS = newStartTS + (endTS - startTS)
|
||||
startTS = newStartTS
|
||||
endTS = newEndTS
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue