diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt index cf86ba243..a6a0966f9 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/adapters/EventListWidgetAdapter.kt @@ -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(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 })) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt index a39ea55d8..4f6c282b2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/extensions/Context.kt @@ -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) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt index 41a84b6f4..94fe46231 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/DayFragment.kt @@ -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) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt index d77b5e623..087064724 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/fragments/EventListFragment.kt @@ -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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt index c0919b76d..4e5bb712c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/DBHelper.kt @@ -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) -> Unit) { + fun getEvents(fromTS: Int, toTS: Int, eventId: Int = -1, applyTypeFilter: Boolean = false, callback: (events: ArrayList) -> 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) -> Unit) { + fun getEventsInBackground(fromTS: Int, toTS: Int, eventId: Int = -1, applyTypeFilter: Boolean, callback: (events: ArrayList) -> Unit) { var events = ArrayList() 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 + 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 { + fun getRepeatableEventsFor(fromTS: Int, toTS: Int, eventId: Int = -1, applyTypeFilter: Boolean = false): List { val newEvents = ArrayList() // 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 { + private fun getAllDayEvents(fromTS: Int, eventId: Int = -1, applyTypeFilter: Boolean = false): List { val events = ArrayList() 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) }) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt index 80d37e0d1..9c71dc151 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/MonthlyCalendarImpl.kt @@ -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) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt index ef777f8c1..b2b34cb6e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/WeeklyCalendarImpl.kt @@ -11,9 +11,8 @@ class WeeklyCalendarImpl(val mCallback: WeeklyCalendar, val mContext: Context) { var mEvents = ArrayList() 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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt index f4999ca27..3b8e34d5e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/helpers/YearlyCalendarImpl.kt @@ -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) } }