From f06c3f73769f1c0fa8594fab369a0ae98aa9adea Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Wed, 31 Jul 2024 12:55:02 +0200 Subject: [PATCH] massive speed up of rendering the gallery --- .../com/vitorpamplona/amethyst/model/Note.kt | 2 - .../ui/dal/UserProfileGalleryFeedFilter.kt | 11 +--- .../ui/screen/loggedIn/ProfileGallery.kt | 63 ++++++++++--------- .../ui/screen/loggedIn/ProfileScreen.kt | 1 - 4 files changed, 35 insertions(+), 42 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Note.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Note.kt index a5118c559..055476c11 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Note.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/Note.kt @@ -139,8 +139,6 @@ open class Note( var relays = listOf() private set - var associatedNote: Note? = null - var lastReactionsDownloadTime: Map = emptyMap() fun id() = Hex.decode(idHex) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/UserProfileGalleryFeedFilter.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/UserProfileGalleryFeedFilter.kt index 5b84eeacf..9ee41fac6 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/UserProfileGalleryFeedFilter.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/dal/UserProfileGalleryFeedFilter.kt @@ -47,16 +47,7 @@ class UserProfileGalleryFeedFilter( } var sorted = sort(notes) - var finalnotes = setOf() - for (item in sorted) { - val note = (item.event as ProfileGalleryEntryEvent).event()?.let { LocalCache.checkGetOrCreateNote(it) } - if (note != null) { - note.associatedNote = item - finalnotes = finalnotes + note - } - } - - return finalnotes.toList() + return sorted.toList() } override fun applyFilter(collection: Set): Set = innerApplyFilter(collection) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileGallery.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileGallery.kt index 74f3ae4d1..0dc63b70b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileGallery.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileGallery.kt @@ -54,6 +54,7 @@ import com.vitorpamplona.amethyst.commons.richtext.BaseMediaContent import com.vitorpamplona.amethyst.commons.richtext.MediaUrlImage import com.vitorpamplona.amethyst.commons.richtext.MediaUrlVideo 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.ui.actions.CrossfadeIfEnabled import com.vitorpamplona.amethyst.ui.components.GalleryContentView @@ -80,7 +81,6 @@ import com.vitorpamplona.quartz.events.ProfileGalleryEntryEvent fun RenderGalleryFeed( viewModel: FeedViewModel, routeForLastRead: String?, - forceEventKind: Int?, listState: LazyGridState, accountViewModel: AccountViewModel, nav: (String) -> Unit, @@ -104,7 +104,6 @@ fun RenderGalleryFeed( state, routeForLastRead, listState, - forceEventKind, accountViewModel, nav, ) @@ -122,7 +121,6 @@ private fun GalleryFeedLoaded( state: FeedState.Loaded, routeForLastRead: String?, listState: LazyGridState, - forceEventKind: Int?, accountViewModel: AccountViewModel, nav: (String) -> Unit, ) { @@ -139,7 +137,6 @@ private fun GalleryFeedLoaded( baseNote = item, routeForLastRead = routeForLastRead, modifier = Modifier, - forceEventKind = forceEventKind, accountViewModel = accountViewModel, nav = nav, ) @@ -158,7 +155,6 @@ fun GalleryCardCompose( routeForLastRead: String? = null, modifier: Modifier = Modifier, parentBackgroundColor: MutableState? = null, - forceEventKind: Int?, isHiddenFeed: Boolean = false, accountViewModel: AccountViewModel, nav: (String) -> Unit, @@ -173,20 +169,29 @@ fun GalleryCardCompose( nav = nav, ) { canPreview -> - if (baseNote.associatedNote != null) { - if (baseNote.associatedNote!!.event != null) { - val image = (baseNote.associatedNote!!.event as ProfileGalleryEntryEvent).url() - if (image != null) { - GalleryCard( - galleryNote = baseNote.associatedNote!!, - baseNote = baseNote, - image = image, - modifier = modifier, - parentBackgroundColor = parentBackgroundColor, - accountViewModel = accountViewModel, - nav = nav, - ) - } + // TODO Vitor, this works, but maybe you know of a better way to run this here in the background + // as LocalCache.checkGetOrCreateNote(it) can not run on the main thread + var note: Note? = null + val thread = + Thread { + note = (baseNote.event as ProfileGalleryEntryEvent).event()?.let { LocalCache.checkGetOrCreateNote(it) } + } + thread.start() + thread.join() + // TODO End + + val image = (baseNote.event as ProfileGalleryEntryEvent).url() + if (image != null) { + note?.let { + GalleryCard( + galleryNote = baseNote, + baseNote = it, + image = image, + modifier = modifier, + parentBackgroundColor = parentBackgroundColor, + accountViewModel = accountViewModel, + nav = nav, + ) } } } @@ -207,6 +212,7 @@ fun GalleryCard( LongPressToQuickActionGallery(baseNote = galleryNote, accountViewModel = accountViewModel) { showPopup -> CheckNewAndRenderChannelCard( baseNote, + galleryNote, image, modifier, parentBackgroundColor, @@ -220,6 +226,7 @@ fun GalleryCard( @Composable private fun CheckNewAndRenderChannelCard( baseNote: Note, + galleryNote: Note, image: String, modifier: Modifier = Modifier, parentBackgroundColor: MutableState? = null, @@ -233,6 +240,7 @@ private fun CheckNewAndRenderChannelCard( parentBackgroundColor = parentBackgroundColor, accountViewModel = accountViewModel, ) + println("NOTE:" + (baseNote.event?.id() ?: "nah")) ClickableNote( baseNote = baseNote, @@ -242,7 +250,7 @@ private fun CheckNewAndRenderChannelCard( showPopup = showPopup, nav = nav, ) { - InnerGalleryCardBox(baseNote, image, accountViewModel, nav) + InnerGalleryCardBox(galleryNote, image, accountViewModel, nav) } } @@ -268,7 +276,6 @@ data class GalleryThumb( val id: String?, val image: String?, val title: String?, - // val price: Price?, ) @Composable @@ -288,7 +295,6 @@ fun RenderGalleryThumb( image = image, title = "", // noteEvent?.title(), - // price = noteEvent?.price(), ) }.distinctUntilChanged() .observeAsState( @@ -299,7 +305,7 @@ fun RenderGalleryThumb( ), ) - InnerRenderGalleryThumb(card as GalleryThumb, baseNote, accountViewModel) + InnerRenderGalleryThumb(card, baseNote, accountViewModel) } @Preview @@ -312,7 +318,6 @@ fun RenderGalleryThumbPreview(accountViewModel: AccountViewModel) { id = "", image = null, title = "Like New", - // price = Price("800000", "SATS", null), ), note = Note("hex"), accountViewModel = accountViewModel, @@ -333,11 +338,11 @@ fun InnerRenderGalleryThumb( contentAlignment = BottomStart, ) { card.image?.let { - var blurHash = (note.associatedNote?.event as ProfileGalleryEntryEvent).blurhash() - var description = (note.associatedNote?.event as ProfileGalleryEntryEvent).content - // var hash = (note.associatedNote?.event as ProfileGalleryEntryEvent).hash() - var dimensions = (note.associatedNote?.event as ProfileGalleryEntryEvent).dimensions() - var mimeType = (note.associatedNote?.event as ProfileGalleryEntryEvent).mimeType() + var blurHash = (note.event as ProfileGalleryEntryEvent).blurhash() + var description = (note.event as ProfileGalleryEntryEvent).content + // var hash = (note.event as ProfileGalleryEntryEvent).hash() + var dimensions = (note.event as ProfileGalleryEntryEvent).dimensions() + var mimeType = (note.event as ProfileGalleryEntryEvent).mimeType() var content: BaseMediaContent? = null if (isVideoUrl(it)) { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt index 867e5ae0f..57b0a481b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/ProfileScreen.kt @@ -1574,7 +1574,6 @@ fun TabGallery( RenderGalleryFeed( feedViewModel, null, - 0, listState, accountViewModel = accountViewModel, nav = nav,