optimize/rename function

This commit is contained in:
MM20 2025-07-19 18:35:33 +02:00
parent c0f69160b9
commit 154a81cafa
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 39 additions and 32 deletions

1
.gitignore vendored
View File

@ -308,5 +308,6 @@ fabric.properties
.idea/deploymentTargetSelector.xml .idea/deploymentTargetSelector.xml
.idea/copilot .idea/copilot
.idea/other.xml .idea/other.xml
.idea/studiobot.xml
.kotlin .kotlin

View File

@ -250,10 +250,10 @@ class SearchVM : ViewModel(), KoinComponent {
previousResults = SearchResults(apps = apps) previousResults = SearchResults(apps = apps)
searchActionResults.clear() searchActionResults.clear()
appResults.mergeWith(apps) appResults.updateItems(apps)
workAppResults.mergeWith(workApps) workAppResults.updateItems(workApps)
privateSpaceAppResults.mergeWith(privateApps) privateSpaceAppResults.updateItems(privateApps)
hiddenResults.mergeWith(hiddenItems) hiddenResults.updateItems(hiddenItems)
} }
} else { } else {
@ -273,19 +273,31 @@ class SearchVM : ViewModel(), KoinComponent {
workAppResults.clear() workAppResults.clear()
privateSpaceAppResults.clear() privateSpaceAppResults.clear()
appResults.mergeWith(results.apps, hiddenKeys, query) appResults.updateItems(
appShortcutResults.mergeWith(results.shortcuts, hiddenKeys, query) results.apps
fileResults.mergeWith(results.files, hiddenKeys, query) ?.filterNot { hiddenKeys.contains(it.key) }
?.applyRanking(query)
)
appShortcutResults.updateItems(
results.shortcuts
?.filterNot { hiddenKeys.contains(it.key) }
?.applyRanking(query)
)
fileResults.updateItems(
results.files
?.filterNot { hiddenKeys.contains(it.key) }
?.applyRanking(query)
)
contactResults.mergeWith( contactResults.updateItems(
results.contacts?.filterNot { hiddenKeys.contains(it.key) } results.contacts?.filterNot { hiddenKeys.contains(it.key) }
?.applyRanking(query) ?.applyRanking(query)
) )
calendarResults.mergeWith( calendarResults.updateItems(
results.calendars?.filterNot { hiddenKeys.contains(it.key) } results.calendars?.filterNot { hiddenKeys.contains(it.key) }
?.applyRanking(query) ?.applyRanking(query)
) )
locationResults.mergeWith( locationResults.updateItems(
results.locations?.filterNot { hiddenKeys.contains(it.key) } results.locations?.filterNot { hiddenKeys.contains(it.key) }
?.let { locations -> ?.let { locations ->
devicePoseProvider.lastCachedLocation?.let { devicePoseProvider.lastCachedLocation?.let {
@ -298,17 +310,17 @@ class SearchVM : ViewModel(), KoinComponent {
} ?: locations.applyRanking(query) } ?: locations.applyRanking(query)
} }
) )
articleResults.mergeWith( articleResults.updateItems(
results.wikipedia?.applyRanking(query) results.wikipedia?.applyRanking(query)
) )
websiteResults.mergeWith( websiteResults.updateItems(
results.websites?.applyRanking(query) results.websites?.applyRanking(query)
) )
calculatorResults.mergeWith(results.calculators) calculatorResults.updateItems(results.calculators)
unitConverterResults.mergeWith(results.unitConverters) unitConverterResults.updateItems(results.unitConverters)
if (results.searchActions != null) { if (results.searchActions != null) {
searchActionResults.mergeWith(results.searchActions!!) searchActionResults.updateItems(results.searchActions!!)
} }
if (launchOnEnter.value) { if (launchOnEnter.value) {
@ -429,24 +441,18 @@ class SearchVM : ViewModel(), KoinComponent {
return sorted.distinctBy { it.key }.toList() return sorted.distinctBy { it.key }.toList()
} }
private fun <T> SnapshotStateList<T>.mergeWith(newItems: List<T>?) { /**
val items = newItems ?: emptyList() * Merges a list of new items into the current SnapshotStateList.
val diff = toSet() subtract items.toSet() * It removes items that are in the current list but not in the new list.
removeAll(diff) * Then, it updates existing items or adds new items from the new list.
for ((i, item) in items.withIndex()) { *
if (i < size) * @param T The type of items in the list.
set(i, item) * @param newItems The list of new items to merge with. If null, an empty list is used.
else */
add(item) private fun <T> SnapshotStateList<T>.updateItems(newItems: List<T>?) {
} clear()
addAll(newItems ?: emptyList())
} }
private suspend fun <T : SavableSearchable> SnapshotStateList<T>.mergeWith(
newItems: List<T>?,
hiddenKeys: List<String>,
query: String
) = this.mergeWith((newItems ?: emptyList()).filterNot { hiddenKeys.contains(it.key) }
.applyRanking(query))
} }