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.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountViewModel)
this.userSuggestions = UserSuggestionState(accountViewModel.account)
}
open fun load(
@@ -20,8 +20,8 @@
*/
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.ui.screen.loggedIn.AccountViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
@@ -29,11 +29,11 @@ import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.flowOn
class EmojiSuggestionState(
val accountViewModel: AccountViewModel,
val account: Account,
) {
val search: MutableStateFlow<String> = MutableStateFlow("")
val results: Flow<List<EmojiPackState.EmojiMedia>> =
accountViewModel.account
account
.emoji.myEmojis
.combine(search) { list, search ->
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.input.TextFieldValue
import com.vitorpamplona.amethyst.logTime
import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.relayClient.searchCommand.SearchQueryState
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.flow.MutableStateFlow
@@ -38,11 +38,12 @@ import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.update
class UserSuggestionState(
val accountViewModel: AccountViewModel,
val account: Account,
val requireAtSymbol: Boolean = true,
) {
val invalidations = MutableStateFlow<Int>(0)
val currentWord = MutableStateFlow("")
val searchDataSourceState = SearchQueryState(MutableStateFlow(""), accountViewModel.account)
val searchDataSourceState = SearchQueryState(MutableStateFlow(""), account)
@OptIn(FlowPreview::class)
val searchTerm =
@@ -57,7 +58,7 @@ class UserSuggestionState(
combine(searchTerm, invalidations.debounce(100)) { prefix, version ->
if (prefix != null) {
logTime("UserSuggestionState Search $prefix version $version") {
accountViewModel.findUsersStartingWithSync(prefix)
account.cache.findUsersStartingWith(prefix, account)
}
} else {
emptyList()
@@ -78,10 +79,18 @@ class UserSuggestionState(
}
fun userSearchTermOrNull(currentWord: String): String? =
if (currentWord.startsWith("@") && currentWord.length > 2) {
currentWord.removePrefix("@")
if (requireAtSymbol) {
if (currentWord.startsWith("@") && currentWord.length > 2) {
currentWord.removePrefix("@")
} else {
null
}
} else {
null
if (currentWord.length > 1) {
currentWord
} else {
null
}
}
fun updateDataSource(searchTerm: String?) {
@@ -189,10 +189,10 @@ open class CommentPostViewModel :
this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM)
this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM)
this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
}
fun newPostFor(externalIdentity: ExternalId) {
@@ -188,10 +188,10 @@ class ChatNewMessageViewModel :
this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM)
this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM)
this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
this.uploadState =
ChatFileUploadState(
@@ -168,10 +168,10 @@ open class ChannelNewMessageViewModel :
this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM)
this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM)
this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
this.uploadState = ChatFileUploadState(account.settings.defaultFileServer)
}
@@ -182,10 +182,10 @@ open class NewProductViewModel :
this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM)
this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM)
this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
}
fun editFromDraft(draft: Note) {
@@ -229,10 +229,10 @@ open class ShortNotePostViewModel :
this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM)
this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM)
this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
}
open fun load(
@@ -191,10 +191,10 @@ class NewPublicMessageViewModel :
this.canAddZapRaiser = hasLnAddress()
this.userSuggestions?.reset()
this.userSuggestions = UserSuggestionState(accountVM)
this.userSuggestions = UserSuggestionState(accountVM.account)
this.emojiSuggestions?.reset()
this.emojiSuggestions = EmojiSuggestionState(accountVM)
this.emojiSuggestions = EmojiSuggestionState(accountVM.account)
}
fun load(users: Set<HexKey>) {