From 80234487da698913009aadd5ef4a74b65866a93b Mon Sep 17 00:00:00 2001 From: tibbi Date: Sun, 8 Jan 2023 20:51:11 +0100 Subject: [PATCH] fix #351, show search results the same way as on Event List view --- .../calendar/pro/activities/MainActivity.kt | 35 ++++++++++++------- .../pro/fragments/EventListFragment.kt | 6 ++-- .../calendar/pro/helpers/EventsHelper.kt | 23 ------------ .../calendar/pro/interfaces/EventsDao.kt | 3 -- 4 files changed, 25 insertions(+), 42 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt index 6f12ca63f..da29c696f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/activities/MainActivity.kt @@ -1184,22 +1184,31 @@ class MainActivity : SimpleActivity(), RefreshRecyclerViewListener { search_placeholder_2.beVisibleIf(text.length == 1) if (text.length >= 2) { - eventsHelper.getEventsWithSearchQuery(text, this) { searchedText, events -> - if (searchedText == mLatestSearchQuery) { - search_results_list.beVisibleIf(events.isNotEmpty()) - search_placeholder.beVisibleIf(events.isEmpty()) - val listItems = getEventListItems(events) - val eventsAdapter = EventListAdapter(this, listItems, true, this, search_results_list) { - hideKeyboard() - if (it is ListEvent) { - Intent(applicationContext, getActivityToOpen(it.isTask)).apply { - putExtra(EVENT_ID, it.id) - startActivity(this) + val minFetchedTS = DateTime().minusMinutes(config.displayPastEvents).seconds() + val maxFetchedTS = DateTime().plusMonths(6).seconds() + + eventsHelper.getEvents(minFetchedTS, maxFetchedTS) { events -> + if (text == mLatestSearchQuery) { + runOnUiThread { + val filtered = events.filter { + it.title.contains(text, true) || it.location.contains(text, true) || it.description.contains(text, true) + } + + search_results_list.beVisibleIf(filtered.isNotEmpty()) + search_placeholder.beVisibleIf(filtered.isEmpty()) + val listItems = getEventListItems(filtered) + val eventsAdapter = EventListAdapter(this, listItems, true, this, search_results_list) { + hideKeyboard() + if (it is ListEvent) { + Intent(applicationContext, getActivityToOpen(it.isTask)).apply { + putExtra(EVENT_ID, it.id) + startActivity(this) + } } } - } - search_results_list.adapter = eventsAdapter + search_results_list.adapter = eventsAdapter + } } } } else if (text.length == 1) { diff --git a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt index fd542912a..4921430ea 100644 --- a/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/calendar/pro/fragments/EventListFragment.kt @@ -86,9 +86,9 @@ class EventListFragment : MyFragmentHolder(), RefreshRecyclerViewListener { maxFetchedTS = DateTime().plusMonths(6).seconds() } - requireContext().eventsHelper.getEvents(minFetchedTS, maxFetchedTS) { - if (it.size >= MIN_EVENTS_TRESHOLD) { - receivedEvents(it, NOT_UPDATING) + requireContext().eventsHelper.getEvents(minFetchedTS, maxFetchedTS) { events -> + if (events.size >= MIN_EVENTS_TRESHOLD) { + receivedEvents(events, NOT_UPDATING) } else { if (!wereInitialEventsAdded) { maxFetchedTS += FETCH_INTERVAL 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 d2805ede3..6f84c052a 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 @@ -232,29 +232,6 @@ class EventsHelper(val context: Context) { } } - fun getEventsWithSearchQuery(text: String, activity: Activity, callback: (searchedText: String, events: List) -> Unit) { - ensureBackgroundThread { - val searchQuery = "%$text%" - val events = eventsDB.getEventsForSearch(searchQuery) - val displayEventTypes = config.displayEventTypes - val filteredEvents = events.filter { displayEventTypes.contains(it.eventType.toString()) } - - val eventTypeColors = LongSparseArray() - eventTypesDB.getEventTypes().forEach { - eventTypeColors.put(it.id!!, it.color) - } - - filteredEvents.forEach { - it.updateIsPastEvent() - it.color = eventTypeColors.get(it.eventType) ?: context.getProperPrimaryColor() - } - - activity.runOnUiThread { - callback(text, filteredEvents) - } - } - } - fun addEventRepetitionException(parentEventId: Long, occurrenceTS: Long, addToCalDAV: Boolean) { ensureBackgroundThread { val parentEvent = eventsDB.getEventOrTaskWithId(parentEventId) ?: return@ensureBackgroundThread 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 791ec4667..b20d88fe9 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 @@ -57,9 +57,6 @@ interface EventsDao { @Query("SELECT * FROM events WHERE id IN (:ids) AND import_id != \"\" AND type = $TYPE_EVENT") fun getEventsByIdsWithImportIds(ids: List): List - @Query("SELECT * FROM events WHERE title LIKE :searchQuery OR location LIKE :searchQuery OR description LIKE :searchQuery AND type = $TYPE_EVENT") - fun getEventsForSearch(searchQuery: String): List - @Query("SELECT * FROM events WHERE source = \'$SOURCE_CONTACT_BIRTHDAY\' AND type = $TYPE_EVENT") fun getBirthdays(): List