From c5eb0664c0a353f642328ba60600dfb08905e6bb Mon Sep 17 00:00:00 2001 From: Jan Koll Date: Fri, 27 Jun 2025 00:38:18 +0200 Subject: [PATCH] Refactor, improve search accuracy when multiple `contains` results If multiple items contain the search string perfectly the fuzzy search still expand it with suggestions. These fuzzy-suggestions, however, might have **less** occurrences in the search string than the items that contained it. The changes ensure that fuzzy-suggestions must have at least the same amount of occurrences as apps that contain the search string. Also, I removed the `appsSecondary` wrapper-list and directly work on `r`. --- .../jrpie/android/launcher/apps/AppFilter.kt | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/de/jrpie/android/launcher/apps/AppFilter.kt b/app/src/main/java/de/jrpie/android/launcher/apps/AppFilter.kt index 21ee7ac..a777e4c 100644 --- a/app/src/main/java/de/jrpie/android/launcher/apps/AppFilter.kt +++ b/app/src/main/java/de/jrpie/android/launcher/apps/AppFilter.kt @@ -64,8 +64,7 @@ class AppFilter( if (query.isEmpty()) { return apps } else { - val r: MutableList = ArrayList() - val appsSecondary: MutableList = ArrayList() + val r: MutableSet = hashSetOf() val normalizedQuery: String = normalize(query) val subsequentResult: MutableList = mutableListOf(); val occurrences: MutableMap = mutableMapOf(); @@ -73,29 +72,28 @@ class AppFilter( val itemLabel: String = normalize(item.getCustomLabel(context)) if (itemLabel.startsWith(normalizedQuery)) { - appsSecondary.add(item); + r.add(item); } else if (itemLabel.contains(normalizedQuery)) { - appsSecondary.add(item) - } else if (LauncherPreferences.functionality().searchFuzzy()) { + r.add(item) + } + if (LauncherPreferences.functionality().searchFuzzy()) { if (isSubsequent(itemLabel, normalizedQuery)) { subsequentResult.add(item) } occurrences[item] = countOccurrences(itemLabel, normalizedQuery) } } - if (LauncherPreferences.functionality().searchFuzzy() && appsSecondary.size != 1) { + if (LauncherPreferences.functionality().searchFuzzy() && r.size != 1) { if (subsequentResult.isNotEmpty()) { - appsSecondary.addAll(subsequentResult) + r.addAll(subsequentResult) } else { val maxOccurrences = occurrences.values.maxOrNull() if (maxOccurrences == 0) return apps val result = occurrences.filter { it.value == maxOccurrences } - appsSecondary.addAll(result.keys) + r.addAll(result.keys) } } - r.addAll(appsSecondary) - - return r + return r.toList() } }