Optimizing the performance of Highlight rendering

This commit is contained in:
Vitor Pamplona
2024-03-19 10:30:21 -04:00
parent e40cd8d932
commit 99270662ee
2 changed files with 16 additions and 23 deletions
@@ -40,8 +40,6 @@ import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.AnnotatedString import androidx.compose.ui.text.AnnotatedString
import androidx.lifecycle.distinctUntilChanged
import androidx.lifecycle.map
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.ui.components.ClickableUrl import com.vitorpamplona.amethyst.ui.components.ClickableUrl
@@ -67,16 +65,13 @@ fun RenderHighlight(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
val quote = remember { (note.event as? HighlightEvent)?.quote() ?: "" } val noteEvent = note.event as? HighlightEvent ?: return
val author = remember { (note.event as? HighlightEvent)?.author() }
val url = remember { (note.event as? HighlightEvent)?.inUrl() }
val postHex = remember { (note.event as? HighlightEvent)?.taggedAddresses()?.firstOrNull() }
DisplayHighlight( DisplayHighlight(
highlight = quote, highlight = noteEvent.quote(),
authorHex = author, authorHex = noteEvent.author(),
url = url, url = noteEvent.inUrl(),
postAddress = postHex, postAddress = noteEvent.inPost(),
makeItShort = makeItShort, makeItShort = makeItShort,
canPreview = canPreview, canPreview = canPreview,
quotesLeft = quotesLeft, quotesLeft = quotesLeft,
@@ -175,7 +170,7 @@ fun LoadAndDisplayUrl(url: String) {
} }
validatedUrl?.host?.let { host -> validatedUrl?.host?.let { host ->
Text(remember { "-" }, maxLines = 1) Text("-", maxLines = 1)
ClickableUrl(urlText = host, url = url) ClickableUrl(urlText = host, url = url)
} }
} }
@@ -186,17 +181,15 @@ private fun LoadAndDisplayPost(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
LoadAddressableNote(aTag = postAddress, accountViewModel) { LoadAddressableNote(aTag = postAddress, accountViewModel) { aTag ->
it?.let { note -> aTag?.let { note ->
val noteEvent by val noteState by note.live().metadata.observeAsState()
note.live().metadata.map { it.note.event }.distinctUntilChanged().observeAsState(note.event) val noteEvent = noteState?.note?.event as? LongTextNoteEvent ?: return@LoadAddressableNote
val title = remember(noteEvent) { (noteEvent as? LongTextNoteEvent)?.title() } noteEvent.title()?.let {
Text("-", maxLines = 1)
title?.let {
Text(remember { "-" }, maxLines = 1)
ClickableText( ClickableText(
text = AnnotatedString(title), text = AnnotatedString(it),
onClick = { routeFor(note, accountViewModel.userProfile())?.let { nav(it) } }, onClick = { routeFor(note, accountViewModel.userProfile())?.let { nav(it) } },
style = LocalTextStyle.current.copy(color = MaterialTheme.colorScheme.primary), style = LocalTextStyle.current.copy(color = MaterialTheme.colorScheme.primary),
) )
@@ -34,13 +34,13 @@ class HighlightEvent(
content: String, content: String,
sig: HexKey, sig: HexKey,
) : BaseTextNoteEvent(id, pubKey, createdAt, KIND, tags, content, sig) { ) : BaseTextNoteEvent(id, pubKey, createdAt, KIND, tags, content, sig) {
fun inUrl() = taggedUrls().firstOrNull() fun inUrl() = firstTaggedUrl()
fun author() = taggedUsers().firstOrNull() fun author() = firstTaggedUser()
fun quote() = content fun quote() = content
fun inPost() = taggedAddresses().firstOrNull() fun inPost() = firstTaggedAddress()
companion object { companion object {
const val KIND = 9802 const val KIND = 9802