Add search filters

This commit is contained in:
MM20
2024-04-21 01:30:33 +02:00
parent 3c489fc648
commit 58ebd5646b
14 changed files with 574 additions and 98 deletions
@@ -0,0 +1,23 @@
package de.mm20.launcher2.search
import de.mm20.launcher2.ktx.toInt
data class SearchFilters(
val allowNetwork: Boolean = false,
val hiddenItems: Boolean = false,
val apps: Boolean = true,
val websites: Boolean = true,
val articles: Boolean = true,
val places: Boolean = true,
val files: Boolean = true,
val shortcuts: Boolean = true,
val contacts: Boolean = true,
val events: Boolean = true,
val tools: Boolean = true,
) {
val allCategoriesEnabled
get() = apps && websites && articles && places && files && shortcuts && contacts && events && tools
val enabledCategories: Int
get() = apps.toInt() + websites.toInt() + articles.toInt() + places.toInt() + files.toInt() + shortcuts.toInt() + contacts.toInt() + events.toInt() + tools.toInt()
}
@@ -23,7 +23,7 @@ import kotlinx.coroutines.supervisorScope
interface SearchService {
fun search(
query: String,
allowNetwork: Boolean = false,
filters: SearchFilters,
): Flow<SearchResults>
}
@@ -44,7 +44,7 @@ internal class SearchServiceImpl(
override fun search(
query: String,
allowNetwork: Boolean,
filters: SearchFilters,
): Flow<SearchResults> = channelFlow {
val results = MutableStateFlow(SearchResults())
supervisorScope {
@@ -56,98 +56,116 @@ internal class SearchServiceImpl(
}
}
}
launch {
appRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(apps = r.toImmutableList())
if (filters.apps) {
launch {
appRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(apps = r.toImmutableList())
}
}
}
}
launch {
appShortcutRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(shortcuts = r.toImmutableList())
}
}
}
launch {
contactRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(contacts = r.toImmutableList())
}
}
}
launch {
calendarRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(calendars = r.toImmutableList())
}
}
}
launch {
calculatorRepository.search(query).collectLatest { r ->
results.update {
it.copy(calculators = r?.let { persistentListOf(it) }
?: persistentListOf())
}
}
}
launch {
unitConverterRepository.search(query)
.collectLatest { r ->
if (filters.shortcuts) {
launch {
appShortcutRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(shortcuts = r.toImmutableList())
}
}
}
}
if (filters.contacts) {
launch {
contactRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(contacts = r.toImmutableList())
}
}
}
}
if (filters.events) {
launch {
calendarRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(calendars = r.toImmutableList())
}
}
}
}
if (filters.tools) {
launch {
calculatorRepository.search(query).collectLatest { r ->
results.update {
it.copy(unitConverters = r?.let { persistentListOf(it) }
it.copy(calculators = r?.let { persistentListOf(it) }
?: persistentListOf())
}
}
}
launch {
websiteRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(websites = r.toImmutableList())
}
launch {
unitConverterRepository.search(query)
.collectLatest { r ->
results.update {
it.copy(unitConverters = r?.let { persistentListOf(it) }
?: persistentListOf())
}
}
}
}
}
launch {
delay(750)
articleRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(wikipedia = r.toImmutableList())
if (filters.websites) {
launch {
websiteRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(websites = r.toImmutableList())
}
}
}
}
}
launch {
locationRepository.search(query, allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(locations = r.toImmutableList())
if (filters.articles) {
launch {
delay(750)
articleRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(wikipedia = r.toImmutableList())
}
}
}
}
}
launch {
fileRepository.search(
query,
allowNetwork
)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(files = r.toImmutableList())
if (filters.places) {
launch {
locationRepository.search(query, filters.allowNetwork)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(locations = r.toImmutableList())
}
}
}
}
}
if (filters.files) {
launch {
fileRepository.search(
query,
filters.allowNetwork
)
.withCustomLabels(customAttributesRepository)
.collectLatest { r ->
results.update {
it.copy(files = r.toImmutableList())
}
}
}
}
launch {
customAttributesRepository.search(query)