From c2243c53cfb8abee2f427078139de50109fef3d0 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 10 Dec 2024 19:07:37 -0500 Subject: [PATCH] Optimizes user search to account for names that start with the typed prefix --- .../amethyst/model/LocalCache.kt | 26 ++++++++++++----- .../amethyst/ui/actions/EditPostViewModel.kt | 6 +--- .../amethyst/ui/actions/NewPostViewModel.kt | 29 ++++++------------- .../loggedIn/search/SearchBarViewModel.kt | 10 +------ 4 files changed, 29 insertions(+), 42 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index 25de3555e..cfb7ae2ab 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -1751,14 +1751,24 @@ object LocalCache { } } - return users.filter { _, user: User -> - ( - (user.anyNameStartsWith(username)) || - user.pubkeyHex.startsWith(username, true) || - user.pubkeyNpub().startsWith(username, true) - ) && - (forAccount == null || (!forAccount.isHidden(user) && !user.containsAny(forAccount.flowHiddenUsers.value.hiddenWordsCase))) - } + val finds = + users.filter { _, user: User -> + ( + (user.anyNameStartsWith(username)) || + user.pubkeyHex.startsWith(username, true) || + user.pubkeyNpub().startsWith(username, true) + ) && + (forAccount == null || (!forAccount.isHidden(user) && !user.containsAny(forAccount.flowHiddenUsers.value.hiddenWordsCase))) + } + + return finds.sortedWith( + compareBy( + { forAccount?.isFollowing(it) == false }, + { !it.toBestDisplayName().startsWith(username, ignoreCase = true) }, + { it.toBestDisplayName().lowercase() }, + { it.pubkeyHex }, + ), + ) } fun findNotesStartingWith( diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt index aba84e8fd..408c31a6a 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/EditPostViewModel.kt @@ -315,11 +315,7 @@ open class EditPostViewModel : ViewModel() { if (lastWord.startsWith("@") && lastWord.length > 2) { NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) viewModelScope.launch(Dispatchers.IO) { - userSuggestions = - LocalCache - .findUsersStartingWith(lastWord.removePrefix("@"), account) - .sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex })) - .reversed() + userSuggestions = LocalCache.findUsersStartingWith(lastWord.removePrefix("@"), account) } } else { NostrSearchEventOrUserDataSource.clear() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt index 2122e42bd..58d9292d7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/actions/NewPostViewModel.kt @@ -1074,13 +1074,10 @@ open class NewPostViewModel : ViewModel() { userSuggestionAnchor = it.selection userSuggestionsMainMessage = UserSuggestionAnchor.MAIN_MESSAGE if (lastWord.startsWith("@") && lastWord.length > 2) { - NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) + val prefix = lastWord.removePrefix("@") + NostrSearchEventOrUserDataSource.search(prefix) viewModelScope.launch(Dispatchers.IO) { - userSuggestions = - LocalCache - .findUsersStartingWith(lastWord.removePrefix("@"), account) - .sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex })) - .reversed() + userSuggestions = LocalCache.findUsersStartingWith(prefix, account) } } else { NostrSearchEventOrUserDataSource.clear() @@ -1103,13 +1100,12 @@ open class NewPostViewModel : ViewModel() { userSuggestionAnchor = it.selection userSuggestionsMainMessage = UserSuggestionAnchor.TO_USERS if (lastWord.startsWith("@") && lastWord.length > 2) { - NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) + val prefix = lastWord.removePrefix("@") + NostrSearchEventOrUserDataSource.search(prefix) viewModelScope.launch(Dispatchers.IO) { userSuggestions = LocalCache - .findUsersStartingWith(lastWord.removePrefix("@"), account) - .sortedWith(compareBy({ account?.isFollowing(it) }, { it.toBestDisplayName() }, { it.pubkeyHex })) - .reversed() + .findUsersStartingWith(prefix, account) } } else { NostrSearchEventOrUserDataSource.clear() @@ -1131,18 +1127,11 @@ open class NewPostViewModel : ViewModel() { userSuggestionAnchor = it.selection userSuggestionsMainMessage = UserSuggestionAnchor.FORWARD_ZAPS if (lastWord.length > 2) { - NostrSearchEventOrUserDataSource.search(lastWord.removePrefix("@")) + val prefix = lastWord.removePrefix("@") + NostrSearchEventOrUserDataSource.search(prefix) viewModelScope.launch(Dispatchers.IO) { userSuggestions = - LocalCache - .findUsersStartingWith(lastWord.removePrefix("@"), account) - .sortedWith( - compareBy( - { account?.isFollowing(it) }, - { it.toBestDisplayName() }, - { it.pubkeyHex }, - ), - ).reversed() + LocalCache.findUsersStartingWith(prefix, account) } } else { NostrSearchEventOrUserDataSource.clear() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/search/SearchBarViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/search/SearchBarViewModel.kt index becdd627c..747ddf6f7 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/search/SearchBarViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/search/SearchBarViewModel.kt @@ -74,15 +74,7 @@ class SearchBarViewModel( _hashtagResults.emit(findHashtags(searchValue)) _searchResultsUsers.emit( - LocalCache - .findUsersStartingWith(searchValue, account) - .sortedWith( - compareBy( - { it.toBestDisplayName().startsWith(searchValue, true) }, - { account.isFollowing(it) }, - { it.toBestDisplayName() }, - ), - ).reversed(), + LocalCache.findUsersStartingWith(searchValue, account), ) _searchResultsNotes.emit( LocalCache