moving some functions from DBHelper to Events
This commit is contained in:
parent
cd655e734a
commit
2e49e9bd12
|
@ -10,7 +10,6 @@ import androidx.collection.LongSparseArray
|
||||||
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
import com.simplemobiletools.calendar.pro.activities.SimpleActivity
|
||||||
import com.simplemobiletools.calendar.pro.extensions.*
|
import com.simplemobiletools.calendar.pro.extensions.*
|
||||||
import com.simplemobiletools.calendar.pro.models.Event
|
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.EventRepetitionException
|
||||||
import com.simplemobiletools.calendar.pro.models.EventType
|
import com.simplemobiletools.calendar.pro.models.EventType
|
||||||
import com.simplemobiletools.commons.extensions.getIntValue
|
import com.simplemobiletools.commons.extensions.getIntValue
|
||||||
|
@ -72,7 +71,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
event.id = id
|
event.id = id
|
||||||
|
|
||||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||||
context.eventRepetitionsDB.insertOrUpdate(getEventRepetition(event))
|
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||||
}
|
}
|
||||||
|
|
||||||
context.updateWidgets()
|
context.updateWidgets()
|
||||||
|
@ -98,7 +97,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
event.id = id
|
event.id = id
|
||||||
|
|
||||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||||
context.eventRepetitionsDB.insertOrUpdate(getEventRepetition(event))
|
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||||
}
|
}
|
||||||
|
|
||||||
context.scheduleNextEventReminder(event, this)
|
context.scheduleNextEventReminder(event, this)
|
||||||
|
@ -122,7 +121,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
if (event.repeatInterval == 0) {
|
if (event.repeatInterval == 0) {
|
||||||
context.eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!)
|
context.eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!)
|
||||||
} else {
|
} else {
|
||||||
context.eventRepetitionsDB.insertOrUpdate(getEventRepetition(event))
|
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||||
}
|
}
|
||||||
|
|
||||||
context.updateWidgets()
|
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) {
|
private fun fillExceptionValues(parentEventId: Long, occurrenceTS: Int, addToCalDAV: Boolean, childImportId: String?, callback: (eventRepetitionException: EventRepetitionException) -> Unit) {
|
||||||
val childEvent = getEventWithId(parentEventId) ?: return
|
val childEvent = getEventWithId(parentEventId) ?: return
|
||||||
|
|
||||||
|
@ -440,7 +437,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
if (event.endTS >= fromTS) {
|
if (event.endTS >= fromTS) {
|
||||||
if (event.repeatInterval.isXWeeklyRepetition()) {
|
if (event.repeatInterval.isXWeeklyRepetition()) {
|
||||||
if (event.startTS.isTsOnProperDay(event)) {
|
if (event.startTS.isTsOnProperDay(event)) {
|
||||||
if (isOnProperWeek(event, startTimes)) {
|
if (event.isOnProperWeek(startTimes)) {
|
||||||
event.copy().apply {
|
event.copy().apply {
|
||||||
updateIsPastEvent()
|
updateIsPastEvent()
|
||||||
color = event.color
|
color = event.color
|
||||||
|
@ -460,7 +457,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
if (event.getIsAllDay()) {
|
if (event.getIsAllDay()) {
|
||||||
if (event.repeatInterval.isXWeeklyRepetition()) {
|
if (event.repeatInterval.isXWeeklyRepetition()) {
|
||||||
if (event.endTS >= toTS && event.startTS.isTsOnProperDay(event)) {
|
if (event.endTS >= toTS && event.startTS.isTsOnProperDay(event)) {
|
||||||
if (isOnProperWeek(event, startTimes)) {
|
if (event.isOnProperWeek(startTimes)) {
|
||||||
event.copy().apply {
|
event.copy().apply {
|
||||||
updateIsPastEvent()
|
updateIsPastEvent()
|
||||||
color = event.color
|
color = event.color
|
||||||
|
@ -491,7 +488,7 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
while (event.repeatLimit < 0 && event.startTS <= toTS) {
|
while (event.repeatLimit < 0 && event.startTS <= toTS) {
|
||||||
if (event.repeatInterval.isXWeeklyRepetition()) {
|
if (event.repeatInterval.isXWeeklyRepetition()) {
|
||||||
if (event.startTS.isTsOnProperDay(event)) {
|
if (event.startTS.isTsOnProperDay(event)) {
|
||||||
if (isOnProperWeek(event, startTimes)) {
|
if (event.isOnProperWeek(startTimes)) {
|
||||||
if (event.endTS >= fromTS) {
|
if (event.endTS >= fromTS) {
|
||||||
event.copy().apply {
|
event.copy().apply {
|
||||||
updateIsPastEvent()
|
updateIsPastEvent()
|
||||||
|
@ -549,13 +546,6 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
|
||||||
return events
|
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> {
|
fun getRunningEvents(): List<Event> {
|
||||||
val events = ArrayList<Event>()
|
val events = ArrayList<Event>()
|
||||||
val ts = getNowSeconds()
|
val ts = getNowSeconds()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package com.simplemobiletools.calendar.pro.models
|
package com.simplemobiletools.calendar.pro.models
|
||||||
|
|
||||||
|
import androidx.collection.LongSparseArray
|
||||||
import androidx.room.ColumnInfo
|
import androidx.room.ColumnInfo
|
||||||
import androidx.room.Entity
|
import androidx.room.Entity
|
||||||
import androidx.room.Index
|
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 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() {
|
fun updateIsPastEvent() {
|
||||||
val endTSToCheck = if (startTS < getNowSeconds() && getIsAllDay()) {
|
val endTSToCheck = if (startTS < getNowSeconds() && getIsAllDay()) {
|
||||||
Formatter.getDayEndTS(Formatter.getDayCodeFromTS(endTS))
|
Formatter.getDayEndTS(Formatter.getDayCodeFromTS(endTS))
|
||||||
|
|
Loading…
Reference in New Issue