adding a few local helper variables to EventsHelper to remove some boilerplace code
This commit is contained in:
parent
15600571a1
commit
c91a32ccf4
|
@ -9,16 +9,21 @@ import com.simplemobiletools.calendar.pro.models.EventType
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
|
||||||
class EventsHelper(val context: Context) {
|
class EventsHelper(val context: Context) {
|
||||||
|
private val config = context.config
|
||||||
|
private val eventsDB = context.eventsDB
|
||||||
|
private val eventTypesDB = context.eventTypesDB
|
||||||
|
private val eventRepetitionsDB = context.eventRepetitionsDB
|
||||||
|
|
||||||
fun getEventTypes(activity: Activity, callback: (notes: ArrayList<EventType>) -> Unit) {
|
fun getEventTypes(activity: Activity, callback: (notes: ArrayList<EventType>) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
val eventTypes = activity.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
|
val eventTypes = eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
|
||||||
activity.runOnUiThread {
|
activity.runOnUiThread {
|
||||||
callback(eventTypes)
|
callback(eventTypes)
|
||||||
}
|
}
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEventTypesSync() = context.eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
|
fun getEventTypesSync() = eventTypesDB.getEventTypes().toMutableList() as ArrayList<EventType>
|
||||||
|
|
||||||
fun insertOrUpdateEventType(activity: Activity, eventType: EventType, callback: ((newEventTypeId: Long) -> Unit)? = null) {
|
fun insertOrUpdateEventType(activity: Activity, eventType: EventType, callback: ((newEventTypeId: Long) -> Unit)? = null) {
|
||||||
Thread {
|
Thread {
|
||||||
|
@ -34,20 +39,20 @@ class EventsHelper(val context: Context) {
|
||||||
CalDAVHandler(context).updateCalDAVCalendar(eventType)
|
CalDAVHandler(context).updateCalDAVCalendar(eventType)
|
||||||
}
|
}
|
||||||
|
|
||||||
val newId = context.eventTypesDB.insertOrUpdate(eventType)
|
val newId = eventTypesDB.insertOrUpdate(eventType)
|
||||||
context.config.addDisplayEventType(newId.toString())
|
config.addDisplayEventType(newId.toString())
|
||||||
return newId
|
return newId
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEventTypeIdWithTitle(title: String) = context.eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L
|
fun getEventTypeIdWithTitle(title: String) = eventTypesDB.getEventTypeIdWithTitle(title) ?: -1L
|
||||||
|
|
||||||
fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = context.eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
|
fun getEventTypeWithCalDAVCalendarId(calendarId: Int) = eventTypesDB.getEventTypeWithCalDAVCalendarId(calendarId)
|
||||||
|
|
||||||
fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
|
fun deleteEventTypes(eventTypes: ArrayList<EventType>, deleteEvents: Boolean) {
|
||||||
val typesToDelete = eventTypes.asSequence().filter { it.caldavCalendarId == 0 && it.id != REGULAR_EVENT_TYPE_ID }.toMutableList()
|
val typesToDelete = eventTypes.asSequence().filter { it.caldavCalendarId == 0 && it.id != REGULAR_EVENT_TYPE_ID }.toMutableList()
|
||||||
val deleteIds = typesToDelete.map { it.id }.toMutableList()
|
val deleteIds = typesToDelete.map { it.id }.toMutableList()
|
||||||
val deletedSet = deleteIds.map { it.toString() }.toHashSet()
|
val deletedSet = deleteIds.map { it.toString() }.toHashSet()
|
||||||
context.config.removeDisplayEventTypes(deletedSet)
|
config.removeDisplayEventTypes(deletedSet)
|
||||||
|
|
||||||
if (deleteIds.isEmpty()) {
|
if (deleteIds.isEmpty()) {
|
||||||
return
|
return
|
||||||
|
@ -57,11 +62,11 @@ class EventsHelper(val context: Context) {
|
||||||
if (deleteEvents) {
|
if (deleteEvents) {
|
||||||
deleteEventsWithType(eventTypeId!!)
|
deleteEventsWithType(eventTypeId!!)
|
||||||
} else {
|
} else {
|
||||||
context.eventsDB.resetEventsWithType(eventTypeId!!)
|
eventsDB.resetEventsWithType(eventTypeId!!)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
context.eventTypesDB.deleteEventTypes(typesToDelete)
|
eventTypesDB.deleteEventTypes(typesToDelete)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getEventRepetitionIgnoredOccurrences(event: Event): ArrayList<String> {
|
fun getEventRepetitionIgnoredOccurrences(event: Event): ArrayList<String> {
|
||||||
|
@ -78,17 +83,17 @@ class EventsHelper(val context: Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
val id = context.eventsDB.insertOrUpdate(event)
|
val id = eventsDB.insertOrUpdate(event)
|
||||||
event.id = id
|
event.id = id
|
||||||
|
|
||||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||||
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||||
}
|
}
|
||||||
|
|
||||||
context.updateWidgets()
|
context.updateWidgets()
|
||||||
//context.scheduleNextEventReminder(event, this, activity)
|
//context.scheduleNextEventReminder(event, this, activity)
|
||||||
|
|
||||||
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
|
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) {
|
||||||
CalDAVHandler(context).insertCalDAVEvent(event)
|
CalDAVHandler(context).insertCalDAVEvent(event)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,15 +107,15 @@ class EventsHelper(val context: Context) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
val id = context.eventsDB.insertOrUpdate(event)
|
val id = eventsDB.insertOrUpdate(event)
|
||||||
event.id = id
|
event.id = id
|
||||||
|
|
||||||
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
if (event.repeatInterval != 0 && event.parentId == 0L) {
|
||||||
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||||
}
|
}
|
||||||
|
|
||||||
//context.scheduleNextEventReminder(event, this)
|
//context.scheduleNextEventReminder(event, this)
|
||||||
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && context.config.caldavSync) {
|
if (addToCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && event.source != SOURCE_IMPORTED_ICS && config.caldavSync) {
|
||||||
CalDAVHandler(context).insertCalDAVEvent(event)
|
CalDAVHandler(context).insertCalDAVEvent(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -120,38 +125,38 @@ class EventsHelper(val context: Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun updateEvent(activity: Activity? = null, event: Event, updateAtCalDAV: Boolean, callback: (() -> Unit)? = null) {
|
fun updateEvent(activity: Activity? = null, event: Event, updateAtCalDAV: Boolean, callback: (() -> Unit)? = null) {
|
||||||
context.eventsDB.insertOrUpdate(event)
|
eventsDB.insertOrUpdate(event)
|
||||||
|
|
||||||
if (event.repeatInterval == 0) {
|
if (event.repeatInterval == 0) {
|
||||||
context.eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!)
|
eventRepetitionsDB.deleteEventRepetitionsOfEvent(event.id!!)
|
||||||
} else {
|
} else {
|
||||||
context.eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
eventRepetitionsDB.insertOrUpdate(event.getEventRepetition())
|
||||||
}
|
}
|
||||||
|
|
||||||
context.updateWidgets()
|
context.updateWidgets()
|
||||||
//context.scheduleNextEventReminder(event, this, activity)
|
//context.scheduleNextEventReminder(event, this, activity)
|
||||||
if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && context.config.caldavSync) {
|
if (updateAtCalDAV && event.source != SOURCE_SIMPLE_CALENDAR && config.caldavSync) {
|
||||||
CalDAVHandler(context).updateCalDAVEvent(event)
|
CalDAVHandler(context).updateCalDAVEvent(event)
|
||||||
}
|
}
|
||||||
callback?.invoke()
|
callback?.invoke()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteAllEvents() {
|
fun deleteAllEvents() {
|
||||||
val eventIds = context.eventsDB.getEventIds().toMutableList()
|
val eventIds = eventsDB.getEventIds().toMutableList()
|
||||||
deleteEvents(eventIds, true)
|
deleteEvents(eventIds, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteEvent(id: Long, deleteFromCalDAV: Boolean) = deleteEvents(arrayListOf(id), deleteFromCalDAV)
|
fun deleteEvent(id: Long, deleteFromCalDAV: Boolean) = deleteEvents(arrayListOf(id), deleteFromCalDAV)
|
||||||
|
|
||||||
fun deleteEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
|
fun deleteEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
|
||||||
val eventsWithImportId = context.eventsDB.getEventsByIdsWithImportIds(ids)
|
val eventsWithImportId = eventsDB.getEventsByIdsWithImportIds(ids)
|
||||||
context.eventsDB.deleteEvents(ids)
|
eventsDB.deleteEvents(ids)
|
||||||
|
|
||||||
ids.forEach {
|
ids.forEach {
|
||||||
context.cancelNotification(it)
|
context.cancelNotification(it)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (deleteFromCalDAV && context.config.caldavSync) {
|
if (deleteFromCalDAV && config.caldavSync) {
|
||||||
eventsWithImportId.forEach {
|
eventsWithImportId.forEach {
|
||||||
CalDAVHandler(context).deleteCalDAVEvent(it)
|
CalDAVHandler(context).deleteCalDAVEvent(it)
|
||||||
}
|
}
|
||||||
|
@ -161,24 +166,24 @@ class EventsHelper(val context: Context) {
|
||||||
context.updateWidgets()
|
context.updateWidgets()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteChildEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
|
private fun deleteChildEvents(ids: MutableList<Long>, deleteFromCalDAV: Boolean) {
|
||||||
val childIds = context.eventsDB.getEventIdsWithParentIds(ids).toMutableList()
|
val childIds = eventsDB.getEventIdsWithParentIds(ids).toMutableList()
|
||||||
if (childIds.isNotEmpty()) {
|
if (childIds.isNotEmpty()) {
|
||||||
deleteEvents(childIds, deleteFromCalDAV)
|
deleteEvents(childIds, deleteFromCalDAV)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun deleteEventsWithType(eventTypeId: Long) {
|
private fun deleteEventsWithType(eventTypeId: Long) {
|
||||||
val eventIds = context.eventsDB.getEventIdsByEventType(eventTypeId).toMutableList()
|
val eventIds = eventsDB.getEventIdsByEventType(eventTypeId).toMutableList()
|
||||||
deleteEvents(eventIds, true)
|
deleteEvents(eventIds, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addEventRepeatLimit(eventId: Long, limitTS: Int) {
|
fun addEventRepeatLimit(eventId: Long, limitTS: Int) {
|
||||||
val time = Formatter.getDateTimeFromTS(limitTS)
|
val time = Formatter.getDateTimeFromTS(limitTS)
|
||||||
context.eventRepetitionsDB.updateEventRepetitionLimit(limitTS - time.hourOfDay, eventId)
|
eventRepetitionsDB.updateEventRepetitionLimit(limitTS - time.hourOfDay, eventId)
|
||||||
|
|
||||||
if (context.config.caldavSync) {
|
if (config.caldavSync) {
|
||||||
val event = context.eventsDB.getEventWithId(eventId)
|
val event = eventsDB.getEventWithId(eventId)
|
||||||
if (event?.getCalDAVCalendarId() != 0) {
|
if (event?.getCalDAVCalendarId() != 0) {
|
||||||
CalDAVHandler(context).updateCalDAVEvent(event!!)
|
CalDAVHandler(context).updateCalDAVEvent(event!!)
|
||||||
}
|
}
|
||||||
|
@ -187,7 +192,7 @@ class EventsHelper(val context: Context) {
|
||||||
|
|
||||||
fun doEventTypesContainEvents(eventTypeIds: ArrayList<Long>, callback: (contain: Boolean) -> Unit) {
|
fun doEventTypesContainEvents(eventTypeIds: ArrayList<Long>, callback: (contain: Boolean) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
val eventIds = context.eventsDB.getEventIdsByEventType(eventTypeIds)
|
val eventIds = eventsDB.getEventIdsByEventType(eventTypeIds)
|
||||||
callback(eventIds.isNotEmpty())
|
callback(eventIds.isNotEmpty())
|
||||||
}.start()
|
}.start()
|
||||||
}
|
}
|
||||||
|
@ -195,8 +200,8 @@ class EventsHelper(val context: Context) {
|
||||||
fun getEventsWithSearchQuery(text: String, activity: Activity, callback: (searchedText: String, events: List<Event>) -> Unit) {
|
fun getEventsWithSearchQuery(text: String, activity: Activity, callback: (searchedText: String, events: List<Event>) -> Unit) {
|
||||||
Thread {
|
Thread {
|
||||||
val searchQuery = "%$text%"
|
val searchQuery = "%$text%"
|
||||||
val events = context.eventsDB.getEventsForSearch(searchQuery)
|
val events = eventsDB.getEventsForSearch(searchQuery)
|
||||||
val displayEventTypes = context.config.displayEventTypes
|
val displayEventTypes = config.displayEventTypes
|
||||||
val filteredEvents = events.filter { displayEventTypes.contains(it.eventType.toString()) }
|
val filteredEvents = events.filter { displayEventTypes.contains(it.eventType.toString()) }
|
||||||
activity.runOnUiThread {
|
activity.runOnUiThread {
|
||||||
callback(text, filteredEvents)
|
callback(text, filteredEvents)
|
||||||
|
|
Loading…
Reference in New Issue