Updates emoji and user autocomplete state to use Account and avoid linking AccountViewModel

This commit is contained in:
Vitor Pamplona
2025-11-04 19:21:04 -05:00
parent 1411de32a0
commit 50b551bb80
9 changed files with 32 additions and 23 deletions
@@ -104,7 +104,7 @@ open class EditPostViewModel : ViewModel() {
this.editedFromNote = edit this.editedFromNote = edit
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountViewModel) this.userSuggestions = UserSuggestionState(accountViewModel.account)
} }
open fun load( open fun load(
@@ -20,8 +20,8 @@
*/ */
package com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions package com.vitorpamplona.amethyst.ui.note.creators.emojiSuggestions
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.nip30CustomEmojis.EmojiPackState import com.vitorpamplona.amethyst.model.nip30CustomEmojis.EmojiPackState
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@@ -29,11 +29,11 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
class EmojiSuggestionState( class EmojiSuggestionState(
val accountViewModel: AccountViewModel, val account: Account,
) { ) {
val search: MutableStateFlow<String> = MutableStateFlow("") val search: MutableStateFlow<String> = MutableStateFlow("")
val results: Flow<List<EmojiPackState.EmojiMedia>> = val results: Flow<List<EmojiPackState.EmojiMedia>> =
accountViewModel.account account
.emoji.myEmojis .emoji.myEmojis
.combine(search) { list, search -> .combine(search) { list, search ->
if (search.length == 1) { if (search.length == 1) {
@@ -23,9 +23,9 @@ package com.vitorpamplona.amethyst.ui.note.creators.userSuggestions
import androidx.compose.ui.text.TextRange import androidx.compose.ui.text.TextRange
import androidx.compose.ui.text.input.TextFieldValue import androidx.compose.ui.text.input.TextFieldValue
import com.vitorpamplona.amethyst.logTime import com.vitorpamplona.amethyst.logTime
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.relayClient.searchCommand.SearchQueryState import com.vitorpamplona.amethyst.service.relayClient.searchCommand.SearchQueryState
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
@@ -38,11 +38,12 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
class UserSuggestionState( class UserSuggestionState(
val accountViewModel: AccountViewModel, val account: Account,
val requireAtSymbol: Boolean = true,
) { ) {
val invalidations = MutableStateFlow<Int>(0) val invalidations = MutableStateFlow<Int>(0)
val currentWord = MutableStateFlow("") val currentWord = MutableStateFlow("")
val searchDataSourceState = SearchQueryState(MutableStateFlow(""), accountViewModel.account) val searchDataSourceState = SearchQueryState(MutableStateFlow(""), account)
@OptIn(FlowPreview::class) @OptIn(FlowPreview::class)
val searchTerm = val searchTerm =
@@ -57,7 +58,7 @@ class UserSuggestionState(
combine(searchTerm, invalidations.debounce(100)) { prefix, version -> combine(searchTerm, invalidations.debounce(100)) { prefix, version ->
if (prefix != null) { if (prefix != null) {
logTime("UserSuggestionState Search $prefix version $version") { logTime("UserSuggestionState Search $prefix version $version") {
accountViewModel.findUsersStartingWithSync(prefix) account.cache.findUsersStartingWith(prefix, account)
} }
} else { } else {
emptyList() emptyList()
@@ -78,10 +79,18 @@ class UserSuggestionState(
} }
fun userSearchTermOrNull(currentWord: String): String? = fun userSearchTermOrNull(currentWord: String): String? =
if (currentWord.startsWith("@") && currentWord.length > 2) { if (requireAtSymbol) {
currentWord.removePrefix("@") if (currentWord.startsWith("@") && currentWord.length > 2) {
currentWord.removePrefix("@")
} else {
null
}
} else { } else {
null if (currentWord.length > 1) {
currentWord
} else {
null
}
} }
fun updateDataSource(searchTerm: String?) { fun updateDataSource(searchTerm: String?) {
@@ -189,10 +189,10 @@ open class CommentPostViewModel :
this.canAddZapRaiser = hasLnAddress() this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM) this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset() this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM) this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
} }
fun newPostFor(externalIdentity: ExternalId) { fun newPostFor(externalIdentity: ExternalId) {
@@ -188,10 +188,10 @@ class ChatNewMessageViewModel :
this.canAddZapRaiser = hasLnAddress() this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM) this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset() this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM) this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
this.uploadState = this.uploadState =
ChatFileUploadState( ChatFileUploadState(
@@ -168,10 +168,10 @@ open class ChannelNewMessageViewModel :
this.canAddZapRaiser = hasLnAddress() this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM) this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset() this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM) this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
this.uploadState = ChatFileUploadState(account.settings.defaultFileServer) this.uploadState = ChatFileUploadState(account.settings.defaultFileServer)
} }
@@ -182,10 +182,10 @@ open class NewProductViewModel :
this.canAddZapRaiser = hasLnAddress() this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM) this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset() this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM) this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
} }
fun editFromDraft(draft: Note) { fun editFromDraft(draft: Note) {
@@ -229,10 +229,10 @@ open class ShortNotePostViewModel :
this.canAddZapRaiser = hasLnAddress() this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM) this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset() this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM) this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
} }
open fun load( open fun load(
@@ -191,10 +191,10 @@ class NewPublicMessageViewModel :
this.canAddZapRaiser = hasLnAddress() this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset() this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM) this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset() this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM) this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
} }
fun load(users: Set<HexKey>) { fun load(users: Set<HexKey>) {