From 11aa45fce69774ab6bcd940c24b04962ef8795d0 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 22 Oct 2016 19:46:37 +0200 Subject: [PATCH] improve the way we are checking repeatable events --- .../simplemobiletools/calendar/DBHelper.kt | 64 +++++-------------- .../calendar/models/Event.kt | 19 ++++++ 2 files changed, 34 insertions(+), 49 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt index e39263964..c1b05bf1b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/DBHelper.kt @@ -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() - 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 { + private fun getEventsFor(fromTS: Int, toTS: Int): List { val newEvents = ArrayList() - 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 { + private fun getEvents(selection: String): List { val events = ArrayList() 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 { 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) } - - interface YearlyEventsListener { - fun yearlyEvents(events: MutableList) - } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt index c76680b63..ef65ba795 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/models/Event.kt @@ -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 + } }