Merge remote-tracking branch 'upstream/main' into better_gallery_view

This commit is contained in:
Believethehype 2024-07-31 16:36:25 +02:00
commit 8bca729edd
9 changed files with 55 additions and 27 deletions

View File

@ -26,21 +26,35 @@ import com.vitorpamplona.amethyst.commons.richtext.RichTextViewerState
import com.vitorpamplona.quartz.events.ImmutableListOfLists
object CachedRichTextParser {
val richTextCache = LruCache<String, RichTextViewerState>(50)
val richTextCache = LruCache<Int, RichTextViewerState>(50)
fun getCached(content: String): RichTextViewerState? = richTextCache[content]
// fun getCached(content: String): RichTextViewerState? = richTextCache[content]
fun hashCodeCache(
content: String,
tags: ImmutableListOfLists<String>,
callbackUri: String?,
): Int {
var result = content.hashCode()
result = 31 * result + tags.lists.hashCode()
if (callbackUri != null) {
result = 31 * result + callbackUri.hashCode()
}
return result
}
fun parseText(
content: String,
tags: ImmutableListOfLists<String>,
callbackUri: String? = null,
): RichTextViewerState {
val cached = richTextCache[content]
val key = hashCodeCache(content, tags, callbackUri)
val cached = richTextCache[key]
return if (cached != null) {
cached
} else {
val newUrls = RichTextParser().parseText(content, tags, callbackUri)
richTextCache.put(content, newUrls)
richTextCache.put(key, newUrls)
newUrls
}
}

View File

@ -42,9 +42,10 @@ import kotlinx.coroutines.launch
@Composable
fun DisplayUncitedHashtags(
event: Event,
callbackUri: String? = null,
nav: (String) -> Unit,
) {
DisplayUncitedHashtags(event, event.content, nav)
DisplayUncitedHashtags(event, event.content, callbackUri, nav)
}
@OptIn(ExperimentalLayoutApi::class)
@ -52,6 +53,7 @@ fun DisplayUncitedHashtags(
fun DisplayUncitedHashtags(
event: Event,
content: String,
callbackUri: String? = null,
nav: (String) -> Unit,
) {
val unusedHashtags by
@ -59,7 +61,7 @@ fun DisplayUncitedHashtags(
val tagsInEvent = event.hashtags()
if (tagsInEvent.isNotEmpty()) {
launch(Dispatchers.Default) {
val state = CachedRichTextParser.parseText(content, event.tags.toImmutableListOfLists())
val state = CachedRichTextParser.parseText(content, event.tags.toImmutableListOfLists(), callbackUri)
val tagsInContent =
state

View File

@ -216,6 +216,8 @@ fun AudioHeader(
}
}
val callbackUri = remember(note) { note.toNostrUri() }
content?.let {
Row(
verticalAlignment = Alignment.CenterVertically,
@ -231,7 +233,7 @@ fun AudioHeader(
tags = tags,
backgroundColor = background,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
@ -240,7 +242,7 @@ fun AudioHeader(
if (noteEvent.hasHashtags()) {
Row(Modifier.fillMaxWidth(), verticalAlignment = Alignment.CenterVertically) {
DisplayUncitedHashtags(noteEvent, nav)
DisplayUncitedHashtags(noteEvent, callbackUri, nav)
}
}
}

View File

@ -170,23 +170,23 @@ private fun RenderGitPatchEvent(
overflow = TextOverflow.Ellipsis,
)
} else {
val callbackUri = remember(note) { note.toNostrUri() }
SensitivityWarning(
note = note,
accountViewModel = accountViewModel,
) {
val modifier = remember(note) { Modifier.fillMaxWidth() }
val tags =
remember(note) { note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList }
val tags = remember(note) { note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList }
TranslatableRichTextViewer(
content = eventContent,
canPreview = canPreview && !makeItShort,
quotesLeft = quotesLeft,
modifier = modifier,
modifier = Modifier.fillMaxWidth(),
tags = tags,
backgroundColor = backgroundColor,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
@ -196,6 +196,7 @@ private fun RenderGitPatchEvent(
DisplayUncitedHashtags(
event = noteEvent,
content = eventContent,
callbackUri = callbackUri,
nav = nav,
)
}
@ -274,11 +275,12 @@ private fun RenderGitIssueEvent(
overflow = TextOverflow.Ellipsis,
)
} else {
val callbackUri = remember(note) { note.toNostrUri() }
SensitivityWarning(
note = note,
accountViewModel = accountViewModel,
) {
val modifier = remember(note) { Modifier.fillMaxWidth() }
val tags =
remember(note) { note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList }
@ -286,18 +288,18 @@ private fun RenderGitIssueEvent(
content = eventContent,
canPreview = canPreview && !makeItShort,
quotesLeft = quotesLeft,
modifier = modifier,
modifier = Modifier.fillMaxWidth(),
tags = tags,
backgroundColor = backgroundColor,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
}
if (note.event?.hasHashtags() == true) {
DisplayUncitedHashtags(noteEvent, eventContent, nav)
DisplayUncitedHashtags(noteEvent, eventContent, callbackUri, nav)
}
}
}

View File

@ -47,6 +47,7 @@ fun RenderNIP90ContentDiscoveryResponse(
nav: (String) -> Unit,
) {
val noteEvent = note.event as? NIP90ContentDiscoveryResponseEvent ?: return
val callbackUri = remember(note) { note.toNostrUri() }
SensitivityWarning(
note = note,
@ -64,13 +65,13 @@ fun RenderNIP90ContentDiscoveryResponse(
tags = tags,
backgroundColor = backgroundColor,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
}
if (noteEvent.hasHashtags()) {
DisplayUncitedHashtags(noteEvent, noteEvent.content, nav)
DisplayUncitedHashtags(noteEvent, noteEvent.content, callbackUri, nav)
}
}

View File

@ -97,6 +97,7 @@ fun RenderPoll(
)
} else {
val tags = remember(note) { note.event?.tags()?.toImmutableListOfLists() ?: EmptyTagList }
val callbackUri = remember(note) { note.toNostrUri() }
SensitivityWarning(
note = note,
@ -110,7 +111,7 @@ fun RenderPoll(
tags = tags,
backgroundColor = backgroundColor,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
@ -125,7 +126,7 @@ fun RenderPoll(
}
if (noteEvent.hasHashtags()) {
DisplayUncitedHashtags(noteEvent, eventContent, nav)
DisplayUncitedHashtags(noteEvent, eventContent, callbackUri, nav)
}
}
}

View File

@ -101,6 +101,8 @@ fun RenderPrivateMessage(
overflow = TextOverflow.Ellipsis,
)
} else {
val callbackUri = remember(note) { note.toNostrUri() }
SensitivityWarning(
note = note,
accountViewModel = accountViewModel,
@ -113,14 +115,14 @@ fun RenderPrivateMessage(
tags = tags,
backgroundColor = backgroundColor,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
}
if (noteEvent.hasHashtags()) {
DisplayUncitedHashtags(noteEvent, eventContent, nav)
DisplayUncitedHashtags(noteEvent, eventContent, callbackUri, nav)
}
}
}

View File

@ -128,6 +128,8 @@ fun RenderTextEvent(
overflow = TextOverflow.Ellipsis,
)
} else {
val callbackUri = remember(note) { note.toNostrUri() }
SensitivityWarning(
note = note,
accountViewModel = accountViewModel,
@ -150,14 +152,14 @@ fun RenderTextEvent(
} else {
note.idHex
},
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
}
if (noteEvent.hasHashtags()) {
DisplayUncitedHashtags(noteEvent, eventContent, nav)
DisplayUncitedHashtags(noteEvent, eventContent, callbackUri, nav)
}
}
}

View File

@ -165,6 +165,8 @@ fun VideoDisplay(
}
summary?.let {
val callbackUri = remember(note) { note.toNostrUri() }
TranslatableRichTextViewer(
content = it,
canPreview = canPreview && !makeItShort,
@ -173,7 +175,7 @@ fun VideoDisplay(
tags = tags,
backgroundColor = backgroundColor,
id = note.idHex,
callbackUri = note.toNostrUri(),
callbackUri = callbackUri,
accountViewModel = accountViewModel,
nav = nav,
)
@ -182,7 +184,7 @@ fun VideoDisplay(
Row(
Modifier.fillMaxWidth(),
) {
DisplayUncitedHashtags(event, summary, nav)
DisplayUncitedHashtags(event, summary, callbackUri, nav)
}
}
}