From 1123b3b3c1434d597527be606232852fd5965a5e Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Mon, 25 Mar 2024 13:51:08 -0400 Subject: [PATCH] Removes the need to observe author changes to event. --- .../com/vitorpamplona/amethyst/model/Note.kt | 3 -- .../amethyst/ui/note/ChannelCardCompose.kt | 6 +-- .../amethyst/ui/note/ChatroomHeaderCompose.kt | 11 ++++-- .../amethyst/ui/note/MultiSetCompose.kt | 23 +++-------- .../ui/note/NIP05VerificationDisplay.kt | 7 ++-- .../amethyst/ui/note/UserProfilePicture.kt | 5 +-- .../amethyst/ui/note/UsernameDisplay.kt | 39 +++++++++++++++++-- .../ui/note/elements/DefaultImageHeader.kt | 9 ++--- 8 files changed, 59 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt index 173df25de..1da77c850 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -965,8 +965,6 @@ class NoteLiveSet(u: Note) { val relays = innerRelays.map { it } val zaps = innerZaps.map { it } - val authorChanges = innerMetadata.map { it.note.author }.distinctUntilChanged() - val hasEvent = innerMetadata.map { it.note.event != null }.distinctUntilChanged() val hasReactions = @@ -1004,7 +1002,6 @@ class NoteLiveSet(u: Note) { reports.hasObservers() || relays.hasObservers() || zaps.hasObservers() || - authorChanges.hasObservers() || hasEvent.hasObservers() || hasReactions.hasObservers() || replyCount.hasObservers() || diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChannelCardCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChannelCardCompose.kt index 708884a0f..85f8ab9ca 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChannelCardCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChannelCardCompose.kt @@ -853,9 +853,7 @@ fun Gallery( @Composable fun DisplayAuthorBanner(note: Note) { - val authorState by note.live().authorChanges.observeAsState(note.author) - - authorState?.let { author -> - BannerImage(author, Modifier.fillMaxSize().clip(QuoteBorder)) + WatchAuthor(note) { + BannerImage(it, Modifier.fillMaxSize().clip(QuoteBorder)) } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt index 47702a68e..7e380fd09 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ChatroomHeaderCompose.kt @@ -84,12 +84,15 @@ fun ChatroomHeaderCompose( accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { - val hasEvent by baseNote.live().hasEvent.observeAsState(baseNote.event != null) - - if (hasEvent) { + if (baseNote.event != null) { ChatroomComposeChannelOrUser(baseNote, accountViewModel, nav) } else { - BlankNote() + val hasEvent by baseNote.live().hasEvent.observeAsState(baseNote.event != null) + if (hasEvent) { + ChatroomComposeChannelOrUser(baseNote, accountViewModel, nav) + } else { + BlankNote() + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt index f856575cb..a0c8681da 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt @@ -20,7 +20,6 @@ */ package com.vitorpamplona.amethyst.ui.note -import androidx.compose.animation.Crossfade import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.background import androidx.compose.foundation.clickable @@ -494,13 +493,11 @@ private fun BoxedAuthor( accountViewModel: AccountViewModel, ) { Box(modifier = Size35Modifier.clickable(onClick = { nav(authorRouteFor(note)) })) { - WatchNoteAuthor(note) { targetAuthor -> - Crossfade(targetState = targetAuthor, modifier = Size35Modifier) { author -> - WatchUserMetadataAndFollowsAndRenderUserProfilePictureOrDefaultAuthor( - author, - accountViewModel, - ) - } + WatchAuthorWithBlank(note, Size35Modifier) { author -> + WatchUserMetadataAndFollowsAndRenderUserProfilePictureOrDefaultAuthor( + author, + accountViewModel, + ) } } } @@ -546,16 +543,6 @@ fun WatchUserMetadataAndFollowsAndRenderUserProfilePicture( } } -@Composable -private fun WatchNoteAuthor( - baseNote: Note, - onContent: @Composable (User?) -> Unit, -) { - val author by baseNote.live().authorChanges.observeAsState(baseNote.author) - - onContent(author) -} - @Composable private fun WatchUserMetadata( author: User, diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt index 913cc66f4..421f9156c 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/NIP05VerificationDisplay.kt @@ -60,6 +60,7 @@ import com.vitorpamplona.amethyst.ui.note.LoadStatuses import com.vitorpamplona.amethyst.ui.note.NIP05CheckingIcon import com.vitorpamplona.amethyst.ui.note.NIP05FailedVerification import com.vitorpamplona.amethyst.ui.note.NIP05VerifiedIcon +import com.vitorpamplona.amethyst.ui.note.WatchAuthor import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.Font14SP import com.vitorpamplona.amethyst.ui.theme.NIP05IconSize @@ -116,9 +117,9 @@ fun ObserveDisplayNip05Status( accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { - val author by baseNote.live().authorChanges.observeAsState() - - author?.let { ObserveDisplayNip05Status(it, columnModifier, accountViewModel, nav) } + WatchAuthor(baseNote = baseNote) { + ObserveDisplayNip05Status(it, columnModifier, accountViewModel, nav) + } } @Composable diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt index 852bcc256..93bda3465 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UserProfilePicture.kt @@ -21,7 +21,6 @@ package com.vitorpamplona.amethyst.ui.note import androidx.compose.animation.AnimatedVisibility -import androidx.compose.animation.Crossfade import androidx.compose.animation.fadeIn import androidx.compose.animation.fadeOut import androidx.compose.foundation.ExperimentalFoundationApi @@ -77,9 +76,7 @@ fun NoteAuthorPicture( modifier: Modifier = Modifier, onClick: ((User) -> Unit)? = null, ) { - val author by baseNote.live().authorChanges.observeAsState(baseNote.author) - - Crossfade(targetState = author, label = "NoteAuthorPicture") { + WatchAuthorWithBlank(baseNote) { if (it == null) { DisplayBlankAuthor(size, modifier) } else { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt index 10d018bc9..d28211fe2 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/UsernameDisplay.kt @@ -52,10 +52,43 @@ fun NoteUsernameDisplay( showPlayButton: Boolean = true, textColor: Color = Color.Unspecified, ) { - val authorState by baseNote.live().authorChanges.observeAsState(baseNote.author) + WatchAuthor(baseNote) { + UsernameDisplay(it, weight, showPlayButton, textColor = textColor) + } +} - Crossfade(targetState = authorState, modifier = weight, label = "NoteUsernameDisplay") { - it?.let { UsernameDisplay(it, weight, showPlayButton, textColor = textColor) } +@Composable +fun WatchAuthor( + baseNote: Note, + inner: @Composable (User) -> Unit, +) { + val noteAuthor = baseNote.author + if (noteAuthor != null) { + inner(noteAuthor) + } else { + val authorState by baseNote.live().metadata.observeAsState() + + authorState?.note?.author?.let { + inner(it) + } + } +} + +@Composable +fun WatchAuthorWithBlank( + baseNote: Note, + modifier: Modifier = Modifier, + inner: @Composable (User?) -> Unit, +) { + val noteAuthor = baseNote.author + if (noteAuthor != null) { + inner(noteAuthor) + } else { + val authorState by baseNote.live().metadata.observeAsState() + + Crossfade(targetState = authorState?.note?.author, modifier = modifier, label = "WatchAuthorWithBlank") { newAuthor -> + inner(newAuthor) + } } } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/elements/DefaultImageHeader.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/elements/DefaultImageHeader.kt index e021842b5..3e62441f7 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/elements/DefaultImageHeader.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/elements/DefaultImageHeader.kt @@ -38,6 +38,7 @@ import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.ui.note.BaseUserPicture +import com.vitorpamplona.amethyst.ui.note.WatchAuthor import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.theme.Size55dp import com.vitorpamplona.amethyst.ui.theme.authorNotePictureForImageHeader @@ -47,14 +48,12 @@ fun DefaultImageHeader( note: Note, accountViewModel: AccountViewModel, ) { - val authorState by note.live().authorChanges.observeAsState(note.author) - - authorState?.let { author -> + WatchAuthor(baseNote = note) { Box { - BannerImage(author) + BannerImage(it) Box(authorNotePictureForImageHeader.align(Alignment.BottomStart)) { - BaseUserPicture(author, Size55dp, accountViewModel, Modifier) + BaseUserPicture(it, Size55dp, accountViewModel, Modifier) } } }