simplify daily repeatable event fetching for better performance

This commit is contained in:
tibbi 2018-05-06 10:31:07 +02:00
parent 857854632b
commit c44abf2957

View File

@ -3,6 +3,7 @@ package com.simplemobiletools.calendar.models
import com.simplemobiletools.calendar.extensions.seconds import com.simplemobiletools.calendar.extensions.seconds
import com.simplemobiletools.calendar.helpers.* import com.simplemobiletools.calendar.helpers.*
import com.simplemobiletools.calendar.helpers.Formatter import com.simplemobiletools.calendar.helpers.Formatter
import com.simplemobiletools.commons.helpers.DAY_SECONDS
import org.joda.time.DateTime import org.joda.time.DateTime
import java.io.Serializable import java.io.Serializable
import java.util.* import java.util.*
@ -20,12 +21,14 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
} }
fun addIntervalTime(original: Event) { fun addIntervalTime(original: Event) {
val currStart = Formatter.getDateTimeFromTS(startTS) when (repeatInterval) {
val newStart: DateTime DAY -> {
newStart = when (repeatInterval) { startTS += DAY_SECONDS
DAY -> currStart.plusDays(1) endTS += DAY_SECONDS
}
else -> { else -> {
when { val currStart = Formatter.getDateTimeFromTS(startTS)
val newStart = when {
repeatInterval % YEAR == 0 -> currStart.plusYears(repeatInterval / YEAR) repeatInterval % YEAR == 0 -> currStart.plusYears(repeatInterval / YEAR)
repeatInterval % MONTH == 0 -> when (repeatRule) { repeatInterval % MONTH == 0 -> when (repeatRule) {
REPEAT_MONTH_SAME_DAY -> addMonthsWithSameDay(currStart, original) REPEAT_MONTH_SAME_DAY -> addMonthsWithSameDay(currStart, original)
@ -39,13 +42,13 @@ data class Event(var id: Int = 0, var startTS: Int = 0, var endTS: Int = 0, var
} }
else -> currStart.plusSeconds(repeatInterval) else -> currStart.plusSeconds(repeatInterval)
} }
}
}
val newStartTS = newStart.seconds() val newStartTS = newStart.seconds()
val newEndTS = newStartTS + (endTS - startTS) val newEndTS = newStartTS + (endTS - startTS)
startTS = newStartTS startTS = newStartTS
endTS = newEndTS endTS = newEndTS
} }
}
}
// if an event should happen on 31st with Same Day monthly repetition, dont show it at all at months with 30 or less days // if an event should happen on 31st with Same Day monthly repetition, dont show it at all at months with 30 or less days
private fun addMonthsWithSameDay(currStart: DateTime, original: Event): DateTime { private fun addMonthsWithSameDay(currStart: DateTime, original: Event): DateTime {