Slightly improving the threading for badges.

This commit is contained in:
Vitor Pamplona 2024-05-21 14:50:36 -04:00
parent 24be6cd90d
commit 7acdf56e68
4 changed files with 20 additions and 27 deletions

View File

@ -43,6 +43,7 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable
fun LoadDecryptedContent(
@ -116,10 +117,12 @@ fun LoadAddressableNote(
if (note == null) {
LaunchedEffect(key1 = aTag) {
accountViewModel.getOrCreateAddressableNote(aTag) { newNote ->
if (newNote != note) {
note = newNote
val newNote =
withContext(Dispatchers.IO) {
accountViewModel.getOrCreateAddressableNote(aTag)
}
if (note != newNote) {
note = newNote
}
}
}

View File

@ -946,7 +946,7 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
viewModelScope.launch(Dispatchers.IO) { onResult(checkGetOrCreateAddressableNote(key)) }
}
private suspend fun getOrCreateAddressableNote(key: ATag): AddressableNote? {
suspend fun getOrCreateAddressableNote(key: ATag): AddressableNote? {
return LocalCache.getOrCreateAddressableNote(key)
}

View File

@ -48,6 +48,7 @@ import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
@ -163,7 +164,6 @@ import com.vitorpamplona.amethyst.ui.theme.Size35dp
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.ZeroPadding
import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.events.AppDefinitionEvent
import com.vitorpamplona.quartz.events.BadgeDefinitionEvent
import com.vitorpamplona.quartz.events.BadgeProfilesEvent
@ -1289,14 +1289,8 @@ private fun DisplayBadges(
}
LoadAddressableNote(
aTag =
ATag(
BadgeProfilesEvent.KIND,
baseUser.pubkeyHex,
BadgeProfilesEvent.STANDARD_D_TAG,
null,
),
accountViewModel,
aTag = BadgeProfilesEvent.createAddressTag(baseUser.pubkeyHex),
accountViewModel = accountViewModel,
) { note ->
if (note != null) {
WatchAndRenderBadgeList(note, automaticallyShowProfilePicture, nav)
@ -1342,7 +1336,7 @@ private fun LoadAndRenderBadge(
loadProfilePicture: Boolean,
nav: (String) -> Unit,
) {
var baseNote by remember { mutableStateOf(LocalCache.getNoteIfExists(badgeAwardEventHex)) }
var baseNote by remember(badgeAwardEventHex) { mutableStateOf(LocalCache.getNoteIfExists(badgeAwardEventHex)) }
LaunchedEffect(key1 = badgeAwardEventHex) {
if (baseNote == null) {
@ -1440,7 +1434,7 @@ private fun WatchAndRenderBadgeImage(
pictureModifier
.width(size)
.height(size)
.clip(shape = CircleShape)
.clip(shape = CutCornerShape(20))
.background(bgColor)
.run {
if (onClick != null) {

View File

@ -33,21 +33,17 @@ class BadgeProfilesEvent(
content: String,
sig: HexKey,
) : BaseAddressableEvent(id, pubKey, createdAt, KIND, tags, content, sig) {
fun badgeAwardEvents() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) }
fun badgeAwardEvents() = taggedEvents()
fun badgeAwardDefinitions() =
tags
.filter { it.firstOrNull() == "a" }
.mapNotNull {
val aTagValue = it.getOrNull(1)
val relay = it.getOrNull(2)
if (aTagValue != null) ATag.parse(aTagValue, relay) else null
}
fun badgeAwardDefinitions() = taggedAddresses()
companion object {
const val KIND = 30008
const val STANDARD_D_TAG = "profile_badges"
const val ALT = "List of accepted badges by the author"
private const val STANDARD_D_TAG = "profile_badges"
private const val ALT = "List of accepted badges by the author"
fun createAddressTag(pubKey: HexKey): ATag {
return ATag(KIND, pubKey, STANDARD_D_TAG, null)
}
}
}