Skip to content

Commit

Permalink
Minimizes relay updates when rotating status events for each user.
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed Aug 2, 2024
1 parent fc98442 commit 63e036a
Showing 1 changed file with 46 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.lessImportantLink
import com.vitorpamplona.amethyst.ui.theme.nip05
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.events.AddressableEvent
import com.vitorpamplona.quartz.events.UserMetadata
import com.vitorpamplona.quartz.utils.TimeUtils
Expand Down Expand Up @@ -168,7 +169,7 @@ private fun VerifyAndDisplayNIP05OrStatusLine(
if (nip05Verified.value != true) {
DisplayNIP05(nip05, nip05Verified, accountViewModel)
} else if (!statuses.isEmpty()) {
RotateStatuses(statuses, accountViewModel, nav)
ObserveRotateStatuses(statuses, accountViewModel, nav)
} else {
DisplayNIP05(nip05, nip05Verified, accountViewModel)
}
Expand All @@ -183,6 +184,29 @@ private fun VerifyAndDisplayNIP05OrStatusLine(
}
}

@Composable
fun ObserveRotateStatuses(
statuses: ImmutableList<AddressableNote>,
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {
ObserveAllStatusesToAvoidSwitchigAllTheTime(statuses)

RotateStatuses(
statuses,
accountViewModel,
nav,
)
}

@Composable
fun ObserveAllStatusesToAvoidSwitchigAllTheTime(statuses: ImmutableList<AddressableNote>) {
statuses
.map {
it.live().metadata.observeAsState()
}
}

@Composable
fun RotateStatuses(
statuses: ImmutableList<AddressableNote>,
Expand Down Expand Up @@ -221,14 +245,29 @@ fun DisplayStatus(
nav: (String) -> Unit,
) {
val noteState by addressableNote.live().metadata.observeAsState()
val noteEvent = noteState?.note?.event ?: return

val content = remember(noteState) { addressableNote.event?.content() ?: "" }
val type = remember(noteState) { (addressableNote.event as? AddressableEvent)?.dTag() ?: "" }
val url = remember(noteState) { addressableNote.event?.firstTaggedUrl()?.ifBlank { null } }
val nostrATag = remember(noteState) { addressableNote.event?.firstTaggedAddress() }
val nostrHexID =
remember(noteState) { addressableNote.event?.firstTaggedEvent()?.ifBlank { null } }
DisplayStatusInner(
noteEvent.content(),
(noteEvent as? AddressableEvent)?.dTag() ?: "",
noteEvent.firstTaggedUrl()?.ifBlank { null },
noteEvent.firstTaggedAddress(),
noteEvent.firstTaggedEvent()?.ifBlank { null },
accountViewModel,
nav,
)
}

@Composable
fun DisplayStatusInner(
content: String,
type: String,
url: String?,
nostrATag: ATag?,
nostrHexID: String?,
accountViewModel: AccountViewModel,
nav: (String) -> Unit,
) {
when (type) {
"music" ->
Icon(
Expand Down

0 comments on commit 63e036a

Please sign in to comment.