add initial event fetching in Room

This commit is contained in:
tibbi 2018-11-15 11:45:47 +01:00
parent 847db07d66
commit 427a3fa6fc
5 changed files with 41 additions and 17 deletions

View File

@ -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

View File

@ -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<Int>
fun getDisplayEventTypessAsList() = displayEventTypes.map { it.toLong() }.toMutableList() as ArrayList<Long>
fun addDisplayEventType(type: String) {
addDisplayEventTypes(HashSet<String>(Arrays.asList(type)))
}

View File

@ -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<Event>) -> Unit) {
/*var events = ArrayList<Event>()
var events: ArrayList<Event>
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<Event>
}
} else {
if (eventId == -1L) {
eventsDB.getEventsFromTo(toTS, fromTS).toMutableList() as ArrayList<Event>
} else {
eventsDB.getEventFromToWithId(eventId, toTS, fromTS).toMutableList() as ArrayList<Event>
}
}
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<Event>
callback(events)*/
val eventTypeColors = LongSparseArray<Int>()
context.eventTypesDB.getEventTypes().forEach {
eventTypeColors.put(it.id!!, it.color)
}
events.forEach {
it.updateIsPastEvent()
it.color = eventTypeColors.get(it.eventType)!!
}
callback(events)
}
}

View File

@ -11,14 +11,23 @@ import com.simplemobiletools.calendar.pro.models.Event
@Dao
interface EventsDao {
@Query("SELECT * FROM events")
fun getAllEvents(): List<Event>
@Query("SELECT * FROM events WHERE id = :id")
fun getEventWithId(id: Long): Event?
@Query("SELECT id FROM events")
fun getEventIds(): List<Long>
@Query("SELECT * FROM events WHERE start_ts <= :startTS AND end_ts >= :endTS AND start_ts != 0")
fun getEventsFromTo(startTS: Long, endTS: Long): List<Event>
@Query("SELECT * FROM events WHERE start_ts <= :toTS AND end_ts >= :fromTS")
fun getEventsFromTo(toTS: Long, fromTS: Long): List<Event>
@Query("SELECT * FROM events WHERE id = :id AND start_ts <= :toTS AND end_ts >= :fromTS")
fun getEventFromToWithId(id: Long, toTS: Long, fromTS: Long): List<Event>
@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<Long>): List<Event>
@Query("SELECT id FROM events WHERE import_id = :importId")
fun getEventIdWithImportId(importId: String): Long?

View File

@ -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)
}
}