From 2e49e9bd12f69851dfe31dd42cc1cd3e5bcaac8e Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 14 Nov 2018 17:29:38 +0100 Subject: [PATCH] moving some functions from DBHelper to Events --- .../calendar/pro/helpers/DBHelper.kt | 22 +++++-------------- .../calendar/pro/models/Event.kt | 10 +++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt index bd6131ffe..89cdbf3c2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/DBHelper.kt @@ -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): 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 { val events = ArrayList() val ts = getNowSeconds() diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt index 65dd411ea..fa21e9185 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/models/Event.kt @@ -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): 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))