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?) {
|
fun getEvents(fromTS: Int, toTS: Int, callback: GetEventsListener?) {
|
||||||
Thread({
|
Thread({
|
||||||
val events = ArrayList<Event>()
|
val events = ArrayList<Event>()
|
||||||
var ts = fromTS
|
events.addAll(getEventsFor(fromTS, toTS))
|
||||||
while (ts <= toTS) {
|
|
||||||
events.addAll(getEventsFor(ts))
|
|
||||||
ts += Constants.DAY
|
|
||||||
}
|
|
||||||
|
|
||||||
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 NULL"
|
||||||
val selectionArgs = arrayOf(toTS.toString(), fromTS.toString())
|
val selectionArgs = arrayOf(toTS.toString(), fromTS.toString())
|
||||||
val cursor = getEventsCursor(selection, selectionArgs)
|
val cursor = getEventsCursor(selection, selectionArgs)
|
||||||
|
@ -165,61 +160,36 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
||||||
}).start()
|
}).start()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEventsFor(ts: Int): List<Event> {
|
private fun getEventsFor(fromTS: Int, toTS: Int): List<Event> {
|
||||||
val newEvents = ArrayList<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
|
// get repeatable events
|
||||||
var selection = "($COL_REPEAT_INTERVAL = ${Constants.DAY} OR $COL_REPEAT_INTERVAL = ${Constants.WEEK} OR " +
|
val selection = "$COL_REPEAT_INTERVAL != 0 AND $COL_START_TS < $toTS"
|
||||||
"$COL_REPEAT_INTERVAL = ${Constants.BIWEEK}) AND ($dayEnd - $COL_REPEAT_START) % $COL_REPEAT_INTERVAL BETWEEN 0 AND $dayExclusive"
|
val events = getEvents(selection)
|
||||||
newEvents.addAll(getEvents(selection, ts))
|
for (e in events) {
|
||||||
|
while (e.startTS < toTS) {
|
||||||
// get monthly events
|
if (e.startTS > fromTS) {
|
||||||
selection = "$COL_REPEAT_INTERVAL = ${Constants.MONTH} AND $COL_REPEAT_DAY = ${dateTime.dayOfMonth} AND $COL_REPEAT_START <= $dayEnd"
|
val newEvent = Event(e.id, e.startTS, e.endTS, e.title, e.description, e.reminderMinutes, e.repeatInterval)
|
||||||
newEvents.addAll(getEvents(selection, ts))
|
newEvents.add(newEvent)
|
||||||
|
}
|
||||||
// get yearly events
|
e.addIntervalTime()
|
||||||
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))
|
|
||||||
|
|
||||||
return newEvents
|
return newEvents
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getEvents(selection: String, ts: Int): List<Event> {
|
private fun getEvents(selection: String): List<Event> {
|
||||||
val events = ArrayList<Event>()
|
val events = ArrayList<Event>()
|
||||||
val cursor = getEventsCursor(selection, null)
|
val cursor = getEventsCursor(selection, null)
|
||||||
if (cursor != null) {
|
if (cursor != null) {
|
||||||
val currEvents = fillEvents(cursor)
|
val currEvents = fillEvents(cursor)
|
||||||
for (e in currEvents) {
|
|
||||||
updateEventTimes(e, ts)
|
|
||||||
}
|
|
||||||
events.addAll(currEvents)
|
events.addAll(currEvents)
|
||||||
}
|
}
|
||||||
|
|
||||||
return events
|
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> {
|
fun getEventsAtReboot(): List<Event> {
|
||||||
val selection = "$COL_REMINDER_MINUTES != -1 AND ($COL_START_TS > ? OR $COL_REPEAT_INTERVAL != 0)"
|
val selection = "$COL_REMINDER_MINUTES != -1 AND ($COL_START_TS > ? OR $COL_REPEAT_INTERVAL != 0)"
|
||||||
val selectionArgs = arrayOf((DateTime.now().millis / 1000).toString())
|
val selectionArgs = arrayOf((DateTime.now().millis / 1000).toString())
|
||||||
|
@ -271,8 +241,4 @@ class DBHelper(context: Context) : SQLiteOpenHelper(context, DBHelper.DB_NAME, n
|
||||||
interface GetEventsListener {
|
interface GetEventsListener {
|
||||||
fun gotEvents(events: MutableList<Event>)
|
fun gotEvents(events: MutableList<Event>)
|
||||||
}
|
}
|
||||||
|
|
||||||
interface YearlyEventsListener {
|
|
||||||
fun yearlyEvents(events: MutableList<String>)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,8 @@
|
||||||
package com.simplemobiletools.calendar.models
|
package com.simplemobiletools.calendar.models
|
||||||
|
|
||||||
|
import com.simplemobiletools.calendar.Constants
|
||||||
|
import com.simplemobiletools.calendar.Formatter
|
||||||
|
import org.joda.time.DateTime
|
||||||
import java.io.Serializable
|
import java.io.Serializable
|
||||||
|
|
||||||
class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var title: String = "", var description: String = "",
|
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 {
|
companion object {
|
||||||
private val serialVersionUID = -32456795132344616L
|
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