From ee692a5a60553c8bac1d60fa0ade3965e934c21b Mon Sep 17 00:00:00 2001 From: Infi Date: Thu, 7 Dec 2023 19:37:42 +0100 Subject: [PATCH] feat: channels autocomplete Signed-off-by: Infi --- .../components/chat/NativeMessageField.kt | 19 ++++++++++-- .../chat/revolt/internals/Autocomplete.kt | 31 ++++++++++++++++--- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/chat/revolt/components/chat/NativeMessageField.kt b/app/src/main/java/chat/revolt/components/chat/NativeMessageField.kt index 482e0b53..75ac74b1 100644 --- a/app/src/main/java/chat/revolt/components/chat/NativeMessageField.kt +++ b/app/src/main/java/chat/revolt/components/chat/NativeMessageField.kt @@ -98,7 +98,7 @@ fun String.applyAutocompleteSuggestion( } is AutocompleteSuggestion.Channel -> { - if (suggestion.channel.name?.contains(" ") == true) { + if (suggestion.channel.name?.contains(" ", ignoreCase = true) == true) { this.replaceRange( cursorPosition - suggestion.query.length - 1, cursorPosition, @@ -277,7 +277,12 @@ fun NativeMessageField( }, label = { Text("#${item.channel.name}") }, icon = { - item.channel.channelType?.let { type -> ChannelIcon(channelType = type) } + item.channel.channelType?.let { type -> + ChannelIcon( + channelType = type, + modifier = Modifier.size(SuggestionChipDefaults.IconSize) + ) + } }, modifier = Modifier .animateItemPlacement() @@ -418,6 +423,16 @@ fun NativeMessageField( ) ) } + + lastWord.startsWith('#') -> { + if (this.serverId == null) return + autocompleteSuggestions.addAll( + Autocomplete.channel( + this.serverId!!, + lastWord.substring(1) + ) + ) + } } } }.apply { diff --git a/app/src/main/java/chat/revolt/internals/Autocomplete.kt b/app/src/main/java/chat/revolt/internals/Autocomplete.kt index 418552e7..50795894 100644 --- a/app/src/main/java/chat/revolt/internals/Autocomplete.kt +++ b/app/src/main/java/chat/revolt/internals/Autocomplete.kt @@ -10,7 +10,7 @@ object Autocomplete { fun emoji(query: String): List { val unicodeResults = emojiImpl.shortcodeContains(query).map { AutocompleteSuggestion.Emoji( - it.shortcodes.find { shortcode -> shortcode.contains(query) } + it.shortcodes.find { shortcode -> shortcode.contains(query, ignoreCase = true) } ?: it.shortcodes.first(), it.base.joinToString("") { s -> String(Character.toChars(s.toInt())) }, null, @@ -19,7 +19,9 @@ object Autocomplete { }.distinctBy { it.shortcode } val customResults = - RevoltAPI.emojiCache.values.filter { it.name?.contains(query) ?: false }.map { + RevoltAPI.emojiCache.values.filter { + it.name?.contains(query, ignoreCase = true) ?: false + }.map { if (it.name != null) { AutocompleteSuggestion.Emoji( ":${it.id}:", @@ -47,7 +49,7 @@ object Autocomplete { val otherUser = channel.recipients?.find { it != RevoltAPI.selfId } if (otherUser != null) { val user = RevoltAPI.userCache[otherUser] - if (user != null && user.username?.contains(query) == true) { + if (user != null && user.username?.contains(query, ignoreCase = true) == true) { listOf( AutocompleteSuggestion.User( user, @@ -67,7 +69,7 @@ object Autocomplete { val users = channel.recipients?.mapNotNull { RevoltAPI.userCache[it] } ?: emptyList() users - .filter { it.username?.contains(query) ?: false } + .filter { it.username?.contains(query, ignoreCase = true) ?: false } .map { AutocompleteSuggestion.User( it, @@ -79,7 +81,11 @@ object Autocomplete { ChannelType.SavedMessages -> { val user = RevoltAPI.userCache[RevoltAPI.selfId] - return if (user != null && user.username?.contains(query) == true) { + return if (user != null && user.username?.contains( + query, + ignoreCase = true + ) == true + ) { listOf( AutocompleteSuggestion.User( user, @@ -134,4 +140,19 @@ object Autocomplete { null -> emptyList() } } + + fun channel( + serverId: String, + query: String + ): List { + val server = RevoltAPI.serverCache[serverId] ?: return emptyList() + val channels = server.channels?.mapNotNull { RevoltAPI.channelCache[it] } ?: emptyList() + + return channels.filter { it.name?.contains(query, ignoreCase = true) ?: false }.map { + AutocompleteSuggestion.Channel( + it, + query + ) + } + } } \ No newline at end of file