apply event type filter right at fetching for better performance

This commit is contained in:
tibbi 2018-10-24 15:02:48 +02:00
parent f5fba4c477
commit 7501844d72
8 changed files with 44 additions and 24 deletions

View file

@ -156,7 +156,7 @@ class EventListWidgetAdapter(val context: Context) : RemoteViewsService.RemoteVi
mediumFontSize = context.config.getFontSize()
val fromTS = DateTime().seconds() - context.config.displayPastEvents * 60
val toTS = DateTime().plusYears(1).seconds()
context.dbHelper.getEventsInBackground(fromTS, toTS) {
context.dbHelper.getEventsInBackground(fromTS, toTS, applyTypeFilter = true) {
val listItems = ArrayList<ListItem>(it.size)
val replaceDescription = context.config.replaceDescription
val sorted = it.sortedWith(compareBy({ it.startTS }, { it.endTS }, { it.title }, { if (replaceDescription) it.location else it.description }))

View file

@ -85,7 +85,7 @@ fun Context.scheduleNextEventReminder(event: Event, dbHelper: DBHelper, activity
val now = getNowSeconds()
val reminderSeconds = event.getReminders().reversed().map { it * 60 }
dbHelper.getEvents(now, now + YEAR, event.id) {
dbHelper.getEvents(now, now + YEAR, event.id, false) {
if (it.isNotEmpty()) {
for (curEvent in it) {
for (curReminder in reminderSeconds) {

View file

@ -105,7 +105,7 @@ class DayFragment : Fragment() {
fun updateCalendar() {
val startTS = Formatter.getDayStartTS(mDayCode)
val endTS = Formatter.getDayEndTS(mDayCode)
context?.dbHelper?.getEvents(startTS, endTS) {
context?.dbHelper?.getEvents(startTS, endTS, applyTypeFilter = true) {
receivedEvents(it)
}
}

View file

@ -76,7 +76,7 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
maxFetchedTS = DateTime().plusMonths(6).seconds()
}
context!!.dbHelper.getEvents(minFetchedTS, maxFetchedTS) {
context!!.dbHelper.getEvents(minFetchedTS, maxFetchedTS, applyTypeFilter = true) {
if (it.size >= MIN_EVENTS_TRESHOLD) {
receivedEvents(it, false)
} else {
@ -84,7 +84,7 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
minFetchedTS -= FETCH_INTERVAL
maxFetchedTS += FETCH_INTERVAL
}
context!!.dbHelper.getEvents(minFetchedTS, maxFetchedTS) {
context!!.dbHelper.getEvents(minFetchedTS, maxFetchedTS, applyTypeFilter = true) {
mEvents = it
receivedEvents(mEvents, false, !wereInitialEventsAdded)
}
@ -150,7 +150,7 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
private fun fetchPreviousPeriod() {
val oldMinFetchedTS = minFetchedTS - 1
minFetchedTS -= FETCH_INTERVAL
context!!.dbHelper.getEvents(minFetchedTS, oldMinFetchedTS) {
context!!.dbHelper.getEvents(minFetchedTS, oldMinFetchedTS, applyTypeFilter = true) {
mEvents.addAll(0, it)
receivedEvents(mEvents, false)
}
@ -159,7 +159,7 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener {
private fun fetchNextPeriod(scrollAfterUpdating: Boolean) {
val oldMaxFetchedTS = maxFetchedTS + 1
maxFetchedTS += FETCH_INTERVAL
context!!.dbHelper.getEvents(oldMaxFetchedTS, maxFetchedTS) {
context!!.dbHelper.getEvents(oldMaxFetchedTS, maxFetchedTS, applyTypeFilter = true) {
mEvents.addAll(it)
receivedEvents(mEvents, scrollAfterUpdating)
}

View file

@ -681,30 +681,34 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
}.start()
}
fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: ArrayList<Event>) -> Unit) {
fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, applyTypeFilter: Boolean = false, callback: (events: ArrayList<Event>) -> Unit) {
Thread {
getEventsInBackground(fromTS, toTS, eventId, callback)
getEventsInBackground(fromTS, toTS, eventId, applyTypeFilter, callback)
}.start()
}
fun getEventsInBackground(fromTS: Int, toTS: Int, eventId: Int = -1, callback: (events: ArrayList<Event>) -> Unit) {
fun getEventsInBackground(fromTS: Int, toTS: Int, eventId: Int = -1, applyTypeFilter: Boolean, callback: (events: ArrayList<Event>) -> Unit) {
var events = ArrayList<Event>()
var selection = "$COL_START_TS <= ? AND $COL_END_TS >= ? AND $COL_REPEAT_INTERVAL IS NULL AND $COL_START_TS != 0"
if (eventId != -1)
selection += " AND $MAIN_TABLE_NAME.$COL_ID = $eventId"
if (applyTypeFilter) {
val displayEventTypes = context.config.displayEventTypes
if (displayEventTypes.isNotEmpty()) {
val types = displayEventTypes.joinToString(",", "(", ")")
selection += " AND $COL_EVENT_TYPE IN $types"
}
}
val selectionArgs = arrayOf(toTS.toString(), fromTS.toString())
val cursor = getEventsCursor(selection, selectionArgs)
events.addAll(fillEvents(cursor))
events.addAll(getRepeatableEventsFor(fromTS, toTS, eventId))
events.addAll(getRepeatableEventsFor(fromTS, toTS, eventId, applyTypeFilter))
events.addAll(getAllDayEvents(fromTS, eventId))
val displayEventTypes = context.config.displayEventTypes
events = events.filter {
displayEventTypes.contains(it.eventType.toString())
} as ArrayList<Event>
events.addAll(getAllDayEvents(fromTS, eventId, applyTypeFilter))
events = events
.asSequence()
@ -714,13 +718,22 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
callback(events)
}
fun getRepeatableEventsFor(fromTS: Int, toTS: Int, eventId: Int = -1): List<Event> {
fun getRepeatableEventsFor(fromTS: Int, toTS: Int, eventId: Int = -1, applyTypeFilter: Boolean = false): List<Event> {
val newEvents = ArrayList<Event>()
// get repeatable events
var selection = "$COL_REPEAT_INTERVAL != 0 AND $COL_START_TS <= $toTS AND $COL_START_TS != 0"
if (eventId != -1)
selection += " AND $MAIN_TABLE_NAME.$COL_ID = $eventId"
if (applyTypeFilter) {
val displayEventTypes = context.config.displayEventTypes
if (displayEventTypes.isNotEmpty()) {
val types = displayEventTypes.joinToString(",", "(", ")")
selection += " AND $COL_EVENT_TYPE IN $types"
}
}
val events = getEvents(selection)
val startTimes = SparseIntArray(events.size)
events.forEach {
@ -801,12 +814,20 @@ class DBHelper private constructor(val context: Context) : SQLiteOpenHelper(cont
return events
}
private fun getAllDayEvents(fromTS: Int, eventId: Int = -1): List<Event> {
private fun getAllDayEvents(fromTS: Int, eventId: Int = -1, applyTypeFilter: Boolean = false): List<Event> {
val events = ArrayList<Event>()
var selection = "($COL_FLAGS & $FLAG_ALL_DAY) != 0"
if (eventId != -1)
selection += " AND $MAIN_TABLE_NAME.$COL_ID = $eventId"
if (applyTypeFilter) {
val displayEventTypes = context.config.displayEventTypes
if (displayEventTypes.isNotEmpty()) {
val types = displayEventTypes.joinToString(",", "(", ")")
selection += " AND $COL_EVENT_TYPE IN $types"
}
}
val dayCode = Formatter.getDayCodeFromTS(fromTS)
val cursor = getEventsCursor(selection)
events.addAll(fillEvents(cursor).filter { dayCode == Formatter.getDayCodeFromTS(it.startTS) })

View file

@ -24,7 +24,7 @@ class MonthlyCalendarImpl(val mCallback: MonthlyCalendar, val mContext: Context)
mTargetDate = targetDate
val startTS = mTargetDate.minusDays(7).seconds()
val endTS = mTargetDate.plusDays(43).seconds()
mContext.dbHelper.getEvents(startTS, endTS) {
mContext.dbHelper.getEvents(startTS, endTS, applyTypeFilter = true) {
gotEvents(it)
}
}

View file

@ -11,9 +11,8 @@ class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) {
var mEvents = ArrayList<Event>()
fun updateWeeklyCalendar(weekStartTS: Int) {
val startTS = weekStartTS
val endTS = startTS + WEEK_SECONDS
mContext.dbHelper.getEvents(startTS, endTS) {
val endTS = weekStartTS + WEEK_SECONDS
mContext.dbHelper.getEvents(weekStartTS, endTS, applyTypeFilter = true) {
mEvents = it
mCallback.updateWeeklyCalendar(it)
}

View file

@ -16,7 +16,7 @@ class YearlyCalendarImpl(val callback: YearlyCalendar, val context: Context, val
val startDateTime = DateTime().withTime(0, 0, 0, 0).withDate(year, 1, 1)
val startTS = startDateTime.seconds()
val endTS = startDateTime.plusYears(1).minusSeconds(1).seconds()
context.dbHelper.getEvents(startTS, endTS) {
context.dbHelper.getEvents(startTS, endTS, applyTypeFilter = true) {
gotEvents(it)
}
}