mirror of
https://github.com/vitorpamplona/amethyst.git
synced 2025-04-08 20:08:06 +02:00
Migrating livedata to flow for contact list updates of the user.
This commit is contained in:
parent
0bff3462b1
commit
75e45f37b2
@ -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)
|
||||
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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<AccountState> = account.liveLanguages.map { it }
|
||||
val accountMarkAsReadUpdates = mutableIntStateOf(0)
|
||||
|
||||
val userFollows: LiveData<UserState> =
|
||||
account
|
||||
.userProfile()
|
||||
.live()
|
||||
.follows
|
||||
.map { it }
|
||||
val userRelays: LiveData<UserState> =
|
||||
account
|
||||
.userProfile()
|
||||
.live()
|
||||
.relays
|
||||
.map { it }
|
||||
// TODO: contact lists are not notes yet
|
||||
// val kind3Relays: StateFlow<ContactListEvent?> = 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<ContactListEvent?> = observeByAuthor(ContactListEvent.KIND, account.signer.pubKey)
|
||||
val dmRelays: StateFlow<ChatMessageRelayListEvent?> = observeByAuthor(ChatMessageRelayListEvent.KIND, account.signer.pubKey)
|
||||
val searchRelays: StateFlow<SearchRelayListEvent?> = 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),
|
||||
|
Loading…
x
Reference in New Issue
Block a user