From 2b27ac3aec64f5c91bef404921abc8d46e1f3d57 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Thu, 30 Nov 2023 11:22:06 -0500 Subject: [PATCH] Cache zap calculations in notification cards. --- .../amethyst/ui/note/MultiSetCompose.kt | 4 ++- .../amethyst/ui/note/ReactionsRow.kt | 2 +- .../ui/screen/loggedIn/AccountViewModel.kt | 28 +++++++++++++++++++ .../amethyst/ui/screen/loggedIn/MainScreen.kt | 1 - 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt index 74d0f89a1..618191e81 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/MultiSetCompose.kt @@ -176,7 +176,9 @@ private fun Galeries( if (hasZapEvents) { var zapEvents by remember(multiSetCard.zapEvents) { - mutableStateOf>(persistentListOf()) + mutableStateOf( + accountViewModel.cachedDecryptAmountMessageInGroup(multiSetCard.zapEvents) + ) } LaunchedEffect(key1 = Unit) { diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt index 83db96829..0751acbb9 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/note/ReactionsRow.kt @@ -453,7 +453,7 @@ private fun WatchZapAndRenderGallery( val zapsState by baseNote.live().zaps.observeAsState() var zapEvents by remember(zapsState) { - mutableStateOf>( + mutableStateOf( accountViewModel.cachedDecryptAmountMessageInGroup(baseNote) ) } diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt index dad98ede9..4830f9585 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/AccountViewModel.kt @@ -248,6 +248,34 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View } } + fun cachedDecryptAmountMessageInGroup(zapNotes: List): ImmutableList { + return zapNotes.map { + val request = it.request.event as? LnZapRequestEvent + if (request?.isPrivateZap() == true) { + val cachedPrivateRequest = request.cachedPrivateZap() + if (cachedPrivateRequest != null) { + ZapAmountCommentNotification( + LocalCache.getUserIfExists(cachedPrivateRequest.pubKey) ?: it.request.author, + cachedPrivateRequest.content.ifBlank { null }, + showAmountAxis((it.response.event as? LnZapEvent)?.amount) + ) + } else { + ZapAmountCommentNotification( + it.request.author, + it.request.event?.content()?.ifBlank { null }, + showAmountAxis((it.response.event as? LnZapEvent)?.amount) + ) + } + } else { + ZapAmountCommentNotification( + it.request.author, + it.request.event?.content()?.ifBlank { null }, + showAmountAxis((it.response.event as? LnZapEvent)?.amount) + ) + } + }.toImmutableList() + } + fun cachedDecryptAmountMessageInGroup(baseNote: Note): ImmutableList { val myList = baseNote.zaps.toList() diff --git a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt index fd0725d0f..fc0f6ef5a 100644 --- a/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt +++ b/app/src/main/java/com/vitorpamplona/amethyst/ui/screen/loggedIn/MainScreen.kt @@ -84,7 +84,6 @@ import com.vitorpamplona.amethyst.ui.screen.NostrHomeRepliesFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NostrVideoFeedViewModel import com.vitorpamplona.amethyst.ui.screen.NotificationViewModel import com.vitorpamplona.amethyst.ui.screen.SharedPreferencesViewModel -import com.vitorpamplona.amethyst.ui.theme.Size55Modifier import kotlinx.coroutines.launch import kotlin.math.abs