Cache zap calculations in notification cards.

This commit is contained in:
Vitor Pamplona
2023-11-30 11:22:06 -05:00
parent c692c9125b
commit 2b27ac3aec
4 changed files with 32 additions and 3 deletions
@@ -176,7 +176,9 @@ private fun Galeries(
if (hasZapEvents) {
var zapEvents by remember(multiSetCard.zapEvents) {
mutableStateOf<ImmutableList<ZapAmountCommentNotification>>(persistentListOf())
mutableStateOf(
accountViewModel.cachedDecryptAmountMessageInGroup(multiSetCard.zapEvents)
)
}
LaunchedEffect(key1 = Unit) {
@@ -453,7 +453,7 @@ private fun WatchZapAndRenderGallery(
val zapsState by baseNote.live().zaps.observeAsState()
var zapEvents by remember(zapsState) {
mutableStateOf<ImmutableList<ZapAmountCommentNotification>>(
mutableStateOf(
accountViewModel.cachedDecryptAmountMessageInGroup(baseNote)
)
}
@@ -248,6 +248,34 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
}
}
fun cachedDecryptAmountMessageInGroup(zapNotes: List<CombinedZap>): ImmutableList<ZapAmountCommentNotification> {
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<ZapAmountCommentNotification> {
val myList = baseNote.zaps.toList()
@@ -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