moving some functions from DBHelper to Events

This commit is contained in:
tibbi 2018-11-14 17:29:38 +01:00
parent cd655e734a
commit 2e49e9bd12
2 changed files with 16 additions and 16 deletions

View File

@ -10,7 +10,6 @@ import androidx.collection.LongSparseArray
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
import com.simplemobiletools.calendar.pro.extensions.*
import com.simplemobiletools.calendar.pro.models.Event
import com.simplemobiletools.calendar.pro.models.EventRepetition
import com.simplemobiletools.calendar.pro.models.EventRepetitionException
import com.simplemobiletools.calendar.pro.models.EventType
import com.simplemobiletools.commons.extensions.getIntValue
@ -72,7 +71,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
event.id = id
if (event.repeatInterval != 0 && event.parentId == 0L) {
context.eventRepetitionsDB.insertOrUpdate(getEventRepetition(event))
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
}
context.updateWidgets()
@ -98,7 +97,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
event.id = id
if (event.repeatInterval != 0 && event.parentId == 0L) {
context.eventRepetitionsDB.insertOrUpdate(getEventRepetition(event))
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
}
context.scheduleNextEventReminder(event, this)
@ -122,7 +121,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
if (event.repeatInterval == 0) {
context.eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!)
} else {
context.eventRepetitionsDB.insertOrUpdate(getEventRepetition(event))
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
}
context.updateWidgets()
@ -152,8 +151,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}
}
private fun getEventRepetition(event: Event) = EventRepetition(null, event.id!!, event.repeatInterval, event.repeatRule, event.repeatLimit)
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
val childEvent = getEventWithId(parentEventId) ?: return
@ -440,7 +437,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
if (event.endTS >= fromTS) {
if (event.repeatInterval.isXWeeklyRepetition()) {
if (event.startTS.isTsOnProperDay(event)) {
if (isOnProperWeek(event, startTimes)) {
if (event.isOnProperWeek(startTimes)) {
event.copy().apply {
updateIsPastEvent()
color = event.color
@ -460,7 +457,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
if (event.getIsAllDay()) {
if (event.repeatInterval.isXWeeklyRepetition()) {
if (event.endTS >= toTS && event.startTS.isTsOnProperDay(event)) {
if (isOnProperWeek(event, startTimes)) {
if (event.isOnProperWeek(startTimes)) {
event.copy().apply {
updateIsPastEvent()
color = event.color
@ -491,7 +488,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
while (event.repeatLimit < 0 && event.startTS <= toTS) {
if (event.repeatInterval.isXWeeklyRepetition()) {
if (event.startTS.isTsOnProperDay(event)) {
if (isOnProperWeek(event, startTimes)) {
if (event.isOnProperWeek(startTimes)) {
if (event.endTS >= fromTS) {
event.copy().apply {
updateIsPastEvent()
@ -549,13 +546,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return events
}
// check if its the proper week, for events repeating every x weeks
private fun isOnProperWeek(event: Event, startTimes: LongSparseArray<Int>): Boolean {
val initialWeekOfYear = Formatter.getDateTimeFromTS(startTimes[event.id!!]!!).weekOfWeekyear
val currentWeekOfYear = Formatter.getDateTimeFromTS(event.startTS).weekOfWeekyear
return (currentWeekOfYear - initialWeekOfYear) % (event.repeatInterval / WEEK) == 0
}
fun getRunningEvents(): List<Event> {
val events = ArrayList<Event>()
val ts = getNowSeconds()

View File

@ -1,5 +1,6 @@
package com.simplemobiletools.calendar.pro.models
import androidx.collection.LongSparseArray
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Index
@ -135,6 +136,15 @@ data class Event(
fun getCalDAVCalendarId() = if (source.startsWith(CALDAV)) (source.split("-").lastOrNull() ?: "0").toString().toInt() else 0
fun getEventRepetition() = EventRepetition(null, id!!, repeatInterval, repeatRule, repeatLimit)
// check if its the proper week, for events repeating every x weeks
fun isOnProperWeek(startTimes: LongSparseArray<Int>): Boolean {
val initialWeekOfYear = Formatter.getDateTimeFromTS(startTimes[id!!]!!).weekOfWeekyear
val currentWeekOfYear = Formatter.getDateTimeFromTS(startTS).weekOfWeekyear
return (currentWeekOfYear - initialWeekOfYear) % (repeatInterval / WEEK) == 0
}
fun updateIsPastEvent() {
val endTSToCheck = if (startTS < getNowSeconds() && getIsAllDay()) {
Formatter.getDayEndTS(Formatter.getDayCodeFromTS(endTS))