Fixes the need for main thread note loading for galleries

This commit is contained in:
Vitor Pamplona
2024-07-31 14:57:44 -04:00
parent b24999d78c
commit 0af0f745bf

View File

@@ -54,11 +54,12 @@ import com.vitorpamplona.amethyst.commons.richtext.BaseMediaContent
import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage
import com.vitorpamplona.amethyst.commons.richtext.MediaUrlVideo import com.vitorpamplona.amethyst.commons.richtext.MediaUrlVideo
import com.vitorpamplona.amethyst.commons.richtext.RichTextParser.Companion.isVideoUrl import com.vitorpamplona.amethyst.commons.richtext.RichTextParser.Companion.isVideoUrl
import com.vitorpamplona.amethyst.model.LocalCache
import com.vitorpamplona.amethyst.model.Note import com.vitorpamplona.amethyst.model.Note
import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled import com.vitorpamplona.amethyst.ui.actions.CrossfadeIfEnabled
import com.vitorpamplona.amethyst.ui.components.GalleryContentView import com.vitorpamplona.amethyst.ui.components.GalleryContentView
import com.vitorpamplona.amethyst.ui.components.LoadNote
import com.vitorpamplona.amethyst.ui.components.SensitivityWarning import com.vitorpamplona.amethyst.ui.components.SensitivityWarning
import com.vitorpamplona.amethyst.ui.components.mockAccountViewModel
import com.vitorpamplona.amethyst.ui.note.CheckHiddenFeedWatchBlockAndReport import com.vitorpamplona.amethyst.ui.note.CheckHiddenFeedWatchBlockAndReport
import com.vitorpamplona.amethyst.ui.note.ClickableNote import com.vitorpamplona.amethyst.ui.note.ClickableNote
import com.vitorpamplona.amethyst.ui.note.LongPressToQuickActionGallery import com.vitorpamplona.amethyst.ui.note.LongPressToQuickActionGallery
@@ -130,7 +131,12 @@ private fun GalleryFeedLoaded(
state = listState, state = listState,
) { ) {
itemsIndexed(state.feed.value, key = { _, item -> item.idHex }) { _, item -> itemsIndexed(state.feed.value, key = { _, item -> item.idHex }) { _, item ->
val defaultModifier = remember { Modifier.fillMaxWidth().animateItemPlacement() } val defaultModifier =
remember {
Modifier
.fillMaxWidth()
.animateItemPlacement()
}
Row(defaultModifier) { Row(defaultModifier) {
GalleryCardCompose( GalleryCardCompose(
@@ -169,29 +175,21 @@ fun GalleryCardCompose(
nav = nav, nav = nav,
) { canPreview -> ) { canPreview ->
// TODO Vitor, this works, but maybe you know of a better way to run this here in the background (baseNote.event as ProfileGalleryEntryEvent).event()?.let {
// as LocalCache.checkGetOrCreateNote(it) can not run on the main thread LoadNote(baseNoteHex = it, accountViewModel = accountViewModel) { note ->
var note: Note? = null note?.let {
val thread = (baseNote.event as ProfileGalleryEntryEvent).url()?.let { image ->
Thread { GalleryCard(
note = (baseNote.event as ProfileGalleryEntryEvent).event()?.let { LocalCache.checkGetOrCreateNote(it) } galleryNote = baseNote,
} baseNote = it,
thread.start() image = image,
thread.join() modifier = modifier,
// TODO End parentBackgroundColor = parentBackgroundColor,
accountViewModel = accountViewModel,
val image = (baseNote.event as ProfileGalleryEntryEvent).url() nav = nav,
if (image != null) { )
note?.let { }
GalleryCard( }
galleryNote = baseNote,
baseNote = it,
image = image,
modifier = modifier,
parentBackgroundColor = parentBackgroundColor,
accountViewModel = accountViewModel,
nav = nav,
)
} }
} }
} }
@@ -309,7 +307,9 @@ fun RenderGalleryThumb(
@Preview @Preview
@Composable @Composable
fun RenderGalleryThumbPreview(accountViewModel: AccountViewModel) { fun RenderGalleryThumbPreview() {
val accountViewModel = mockAccountViewModel()
Surface(Modifier.size(200.dp)) { Surface(Modifier.size(200.dp)) {
InnerRenderGalleryThumb( InnerRenderGalleryThumb(
card = card =