From 75e45f37b23833c8a80f7cbb491b76c4666ab1df Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Fri, 16 Aug 2024 15:59:43 -0400 Subject: [PATCH] Migrating livedata to flow for contact list updates of the user. --- .../amethyst/ui/note/RelayCompose.kt | 12 ++----- .../amethyst/ui/note/UserProfilePicture.kt | 20 ++++------- .../amethyst/ui/note/types/CommunityHeader.kt | 12 ++----- .../ui/screen/loggedIn/AccountViewModel.kt | 36 +++++++++++-------- 4 files changed, 34 insertions(+), 46 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt index 8756f7ad1..b726e1a93 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/RelayCompose.kt @@ -32,7 +32,6 @@ import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue -import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier @@ -41,6 +40,7 @@ import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.text.font.FontWeight import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.RelayInfo import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel @@ -110,15 +110,9 @@ private fun RelayOptions( onAddRelay: () -> Unit, onRemoveRelay: () -> Unit, ) { - val userState by accountViewModel.userRelays.observeAsState() + val userState by accountViewModel.normalizedKind3RelaySetFlow.collectAsStateWithLifecycle() - val isNotUsingRelay = - remember(userState) { - accountViewModel.account.connectToRelays.value - .none { it.url == relay.url } - } - - if (isNotUsingRelay) { + if (!userState.contains(relay.url)) { AddRelayButton(onAddRelay) } else { RemoveRelayButton(onRemoveRelay) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt index d21901c5a..0f53768de 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt @@ -35,8 +35,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.unit.Dp -import androidx.lifecycle.distinctUntilChanged -import androidx.lifecycle.map +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.FeatureSetType import com.vitorpamplona.amethyst.model.Note @@ -363,16 +362,11 @@ fun WatchUserFollows( accountViewModel: AccountViewModel, onFollowChanges: @Composable (Boolean) -> Unit, ) { - val showFollowingMark by - remember { - accountViewModel.userFollows - .map { - accountViewModel.isFollowing(userHex) || (userHex == accountViewModel.account.userProfile().pubkeyHex) - }.distinctUntilChanged() - }.observeAsState( - accountViewModel.isFollowing(userHex) || - (userHex == accountViewModel.account.userProfile().pubkeyHex), - ) + if (accountViewModel.isLoggedUser(userHex)) { + onFollowChanges(true) + } else { + val state by accountViewModel.account.liveKind3Follows.collectAsStateWithLifecycle() - onFollowChanges(showFollowingMark) + onFollowChanges(state.users.contains(userHex)) + } } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/CommunityHeader.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/CommunityHeader.kt index 1f68e8add..a5252f608 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/CommunityHeader.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/CommunityHeader.kt @@ -42,8 +42,7 @@ import androidx.compose.ui.Modifier import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.text.style.TextOverflow import androidx.compose.ui.unit.dp -import androidx.lifecycle.distinctUntilChanged -import androidx.lifecycle.map +import androidx.lifecycle.compose.collectAsStateWithLifecycle import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.AddressableNote import com.vitorpamplona.amethyst.model.FeatureSetType @@ -353,12 +352,7 @@ fun WatchAddressableNoteFollows( accountViewModel: AccountViewModel, onFollowChanges: @Composable (Boolean) -> Unit, ) { - val showFollowingMark by - remember { - accountViewModel.userFollows - .map { it.user.latestContactList?.isTaggedAddressableNote(note.idHex) ?: false } - .distinctUntilChanged() - }.observeAsState(false) + val state by accountViewModel.account.liveKind3Follows.collectAsStateWithLifecycle() - onFollowChanges(showFollowingMark) + onFollowChanges(state.communities.contains(note.idHex)) } diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index c70ff789a..d254d8876 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -48,7 +48,6 @@ import com.vitorpamplona.amethyst.model.LocalCache import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.UrlCachedPreviewer import com.vitorpamplona.amethyst.model.User -import com.vitorpamplona.amethyst.model.UserState import com.vitorpamplona.amethyst.model.observables.CreatedAtComparator import com.vitorpamplona.amethyst.service.CashuProcessor import com.vitorpamplona.amethyst.service.CashuToken @@ -75,13 +74,13 @@ import com.vitorpamplona.quartz.encoders.ATag import com.vitorpamplona.quartz.encoders.HexKey import com.vitorpamplona.quartz.encoders.Nip11RelayInformation import com.vitorpamplona.quartz.encoders.Nip19Bech32 +import com.vitorpamplona.quartz.encoders.RelayUrlFormatter import com.vitorpamplona.quartz.encoders.toHexKey import com.vitorpamplona.quartz.events.AddressableEvent import com.vitorpamplona.quartz.events.AdvertisedRelayListEvent import com.vitorpamplona.quartz.events.ChatMessageRelayListEvent import com.vitorpamplona.quartz.events.ChatroomKey import com.vitorpamplona.quartz.events.ChatroomKeyable -import com.vitorpamplona.quartz.events.ContactListEvent import com.vitorpamplona.quartz.events.DraftEvent import com.vitorpamplona.quartz.events.Event import com.vitorpamplona.quartz.events.EventInterface @@ -153,20 +152,26 @@ class AccountViewModel( val accountLanguagesLiveData: LiveData = account.liveLanguages.map { it } val accountMarkAsReadUpdates = mutableIntStateOf(0) - val userFollows: LiveData = - account - .userProfile() - .live() - .follows - .map { it } - val userRelays: LiveData = - account - .userProfile() - .live() - .relays - .map { it } + // TODO: contact lists are not notes yet + // val kind3Relays: StateFlow = observeByAuthor(ContactListEvent.KIND, account.signer.pubKey) + + val normalizedKind3RelaySetFlow = + account + .userProfile() + .flow() + .relays.stateFlow + .map { contactListState -> + checkNotInMainThread() + contactListState.user.latestContactList?.relays()?.map { + RelayUrlFormatter.normalize(it.key) + } ?: emptySet() + }.flowOn(Dispatchers.Default) + .stateIn( + viewModelScope, + SharingStarted.WhileSubscribed(10000, 10000), + emptySet(), + ) - val kind3Relays: StateFlow = observeByAuthor(ContactListEvent.KIND, account.signer.pubKey) val dmRelays: StateFlow = observeByAuthor(ChatMessageRelayListEvent.KIND, account.signer.pubKey) val searchRelays: StateFlow = observeByAuthor(SearchRelayListEvent.KIND, account.signer.pubKey) @@ -333,6 +338,7 @@ class AccountViewModel( .relays .stateFlow .map { it.note.relays.size > 3 } + .flowOn(Dispatchers.Default) .stateIn( viewModelScope, SharingStarted.WhileSubscribed(10000, 10000),