From 427a3fa6fc92c95098f29ba1f6ef360a40eea033 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 15 Nov 2018 11:45:47 +0100 Subject: [PATCH] add initial event fetching in Room --- .../calendar/pro/extensions/Context.kt | 4 +- .../calendar/pro/helpers/Config.kt | 2 + .../calendar/pro/helpers/EventsHelper.kt | 37 +++++++++++++------ .../calendar/pro/interfaces/EventsDao.kt | 13 ++++++- .../pro/receivers/NotificationReceiver.kt | 2 +- 5 files changed, 41 insertions(+), 17 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt index 41d8be7f9..3fe7fe3ee 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/extensions/Context.kt @@ -82,11 +82,11 @@ fun Context.updateListWidget() { fun Context.scheduleAllEvents() { val events = dbHelper.getEventsAtReboot() events.forEach { - scheduleNextEventReminder(it, dbHelper) + scheduleNextEventReminder(it) } } -fun Context.scheduleNextEventReminder(event: Event, dbHelper: DBHelper, activity: SimpleActivity? = null) { +fun Context.scheduleNextEventReminder(event: Event, activity: SimpleActivity? = null) { if (event.getReminders().isEmpty()) { activity?.toast(R.string.saving) return diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt index 99ba11062..0bf685248 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/Config.kt @@ -121,6 +121,8 @@ class Config(context: Context) : BaseConfig(context) { fun getSyncedCalendarIdsAsList() = caldavSyncedCalendarIDs.split(",").filter { it.trim().isNotEmpty() }.map { Integer.parseInt(it) }.toMutableList() as ArrayList + fun getDisplayEventTypessAsList() = displayEventTypes.map { it.toLong() }.toMutableList() as ArrayList + fun addDisplayEventType(type: String) { addDisplayEventTypes(HashSet(Arrays.asList(type))) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt index f16c0f517..379864627 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/helpers/EventsHelper.kt @@ -2,6 +2,7 @@ package com.simplemobiletools.calendar.pro.helpers import android.app.Activity import android.content.Context +import androidx.collection.LongSparseArray import com.simplemobiletools.calendar.pro.activities.SimpleActivity import com.simplemobiletools.calendar.pro.extensions.* import com.simplemobiletools.calendar.pro.models.Event @@ -261,38 +262,50 @@ class EventsHelper(val context: Context) { } fun getEventsSync(fromTS: Long, toTS: Long, eventId: Long = -1L, applyTypeFilter: Boolean, callback: (events: ArrayList) -> Unit) { - /*var events = ArrayList() + var events: ArrayList - var selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL AND $COL_START_TS != 0" - var selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_START_TS != 0" - if (eventId != -1L) { - selection += " AND $MAIN_TABLE_NAME.$COL_ID = $eventId" - } + //var selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL AND $COL_START_TS != 0" - if (applyTypeFilter) { + events = if (applyTypeFilter) { val displayEventTypes = context.config.displayEventTypes if (displayEventTypes.isEmpty()) { callback(ArrayList()) return } else { - val types = TextUtils.join(",", displayEventTypes) - selection += " AND $COL_EVENT_TYPE IN ($types)" + eventsDB.getEventsFromToWithTypes(toTS, fromTS, context.config.getDisplayEventTypessAsList()).toMutableList() as ArrayList + } + } else { + if (eventId == -1L) { + eventsDB.getEventsFromTo(toTS, fromTS).toMutableList() as ArrayList + } else { + eventsDB.getEventFromToWithId(eventId, toTS, fromTS).toMutableList() as ArrayList } } - val selectionArgs = arrayOf(toTS.toString(), fromTS.toString()) + /*val selectionArgs = arrayOf(toTS.toString(), fromTS.toString()) val cursor = getEventsCursor(selection, selectionArgs) events.addAll(fillEvents(cursor)) events.addAll(getRepeatableEventsFor(fromTS, toTS, eventId, applyTypeFilter)) - events.addAll(getAllDayEvents(fromTS, eventId, applyTypeFilter)) + events.addAll(getAllDayEvents(fromTS, eventId, applyTypeFilter))*/ events = events .asSequence() .distinct() .filterNot { context.eventsHelper.getEventRepetitionIgnoredOccurrences(it).contains(Formatter.getDayCodeFromTS(it.startTS)) } .toMutableList() as ArrayList - callback(events)*/ + + val eventTypeColors = LongSparseArray() + context.eventTypesDB.getEventTypes().forEach { + eventTypeColors.put(it.id!!, it.color) + } + + events.forEach { + it.updateIsPastEvent() + it.color = eventTypeColors.get(it.eventType)!! + } + + callback(events) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt index 7a00a6cda..4e5e6dd74 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/interfaces/EventsDao.kt @@ -11,14 +11,23 @@ import com.simplemobiletools.calendar.pro.models.Event @Dao interface EventsDao { + @Query("SELECT * FROM events") + fun getAllEvents(): List + @Query("SELECT * FROM events WHERE id = :id") fun getEventWithId(id: Long): Event? @Query("SELECT id FROM events") fun getEventIds(): List - @Query("SELECT * FROM events WHERE start_ts <= :startTS AND end_ts >= :endTS AND start_ts != 0") - fun getEventsFromTo(startTS: Long, endTS: Long): List + @Query("SELECT * FROM events WHERE start_ts <= :toTS AND end_ts >= :fromTS") + fun getEventsFromTo(toTS: Long, fromTS: Long): List + + @Query("SELECT * FROM events WHERE id = :id AND start_ts <= :toTS AND end_ts >= :fromTS") + fun getEventFromToWithId(id: Long, toTS: Long, fromTS: Long): List + + @Query("SELECT * FROM events WHERE start_ts <= :toTS AND end_ts >= :fromTS AND start_ts != 0 AND event_type IN (:eventTypeIds)") + fun getEventsFromToWithTypes(toTS: Long, fromTS: Long, eventTypeIds: List): List @Query("SELECT id FROM events WHERE import_id = :importId") fun getEventIdWithImportId(importId: String): Long? diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt index eb5a15d24..61de44d93 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/receivers/NotificationReceiver.kt @@ -34,6 +34,6 @@ class NotificationReceiver : BroadcastReceiver() { if (!context.eventsHelper.getEventRepetitionIgnoredOccurrences(event).contains(Formatter.getDayCodeFromTS(event.startTS))) { context.notifyEvent(event) } - context.scheduleNextEventReminder(event, context.dbHelper) + context.scheduleNextEventReminder(event) } }