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
@@ -43,6 +43,7 @@ import kotlinx.collections.immutable.ImmutableList
import kotlinx.collections.immutable.persistentListOf import kotlinx.collections.immutable.persistentListOf
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
@Composable @Composable
fun LoadDecryptedContent( fun LoadDecryptedContent(
@@ -116,10 +117,12 @@ fun LoadAddressableNote(
if (note == null) { if (note == null) {
LaunchedEffect(key1 = aTag) { LaunchedEffect(key1 = aTag) {
accountViewModel.getOrCreateAddressableNote(aTag) { newNote -> val newNote =
if (newNote != note) { withContext(Dispatchers.IO) {
note = newNote accountViewModel.getOrCreateAddressableNote(aTag)
} }
if (note != newNote) {
note = newNote
} }
} }
} }
@@ -946,7 +946,7 @@ class AccountViewModel(val account: Account, val settings: SettingsState) : View
viewModelScope.launch(Dispatchers.IO) { onResult(checkGetOrCreateAddressableNote(key)) } viewModelScope.launch(Dispatchers.IO) { onResult(checkGetOrCreateAddressableNote(key)) }
} }
private suspend fun getOrCreateAddressableNote(key: ATag): AddressableNote? { suspend fun getOrCreateAddressableNote(key: ATag): AddressableNote? {
return LocalCache.getOrCreateAddressableNote(key) return LocalCache.getOrCreateAddressableNote(key)
} }
@@ -48,6 +48,7 @@ import androidx.compose.foundation.pager.PagerState
import androidx.compose.foundation.pager.rememberPagerState import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.CircleShape import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CutCornerShape
import androidx.compose.foundation.text.ClickableText import androidx.compose.foundation.text.ClickableText
import androidx.compose.foundation.verticalScroll import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons 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.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.ZeroPadding import com.vitorpamplona.amethyst.ui.theme.ZeroPadding
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.quartz.encoders.ATag
import com.vitorpamplona.quartz.events.AppDefinitionEvent import com.vitorpamplona.quartz.events.AppDefinitionEvent
import com.vitorpamplona.quartz.events.BadgeDefinitionEvent import com.vitorpamplona.quartz.events.BadgeDefinitionEvent
import com.vitorpamplona.quartz.events.BadgeProfilesEvent import com.vitorpamplona.quartz.events.BadgeProfilesEvent
@@ -1289,14 +1289,8 @@ private fun DisplayBadges(
} }
LoadAddressableNote( LoadAddressableNote(
aTag = aTag = BadgeProfilesEvent.createAddressTag(baseUser.pubkeyHex),
ATag( accountViewModel = accountViewModel,
BadgeProfilesEvent.KIND,
baseUser.pubkeyHex,
BadgeProfilesEvent.STANDARD_D_TAG,
null,
),
accountViewModel,
) { note -> ) { note ->
if (note != null) { if (note != null) {
WatchAndRenderBadgeList(note, automaticallyShowProfilePicture, nav) WatchAndRenderBadgeList(note, automaticallyShowProfilePicture, nav)
@@ -1342,7 +1336,7 @@ private fun LoadAndRenderBadge(
loadProfilePicture: Boolean, loadProfilePicture: Boolean,
nav: (String) -> Unit, nav: (String) -> Unit,
) { ) {
var baseNote by remember { mutableStateOf(LocalCache.getNoteIfExists(badgeAwardEventHex)) } var baseNote by remember(badgeAwardEventHex) { mutableStateOf(LocalCache.getNoteIfExists(badgeAwardEventHex)) }
LaunchedEffect(key1 = badgeAwardEventHex) { LaunchedEffect(key1 = badgeAwardEventHex) {
if (baseNote == null) { if (baseNote == null) {
@@ -1440,7 +1434,7 @@ private fun WatchAndRenderBadgeImage(
pictureModifier pictureModifier
.width(size) .width(size)
.height(size) .height(size)
.clip(shape = CircleShape) .clip(shape = CutCornerShape(20))
.background(bgColor) .background(bgColor)
.run { .run {
if (onClick != null) { if (onClick != null) {
@@ -33,21 +33,17 @@ class BadgeProfilesEvent(
content: String, content: String,
sig: HexKey, sig: HexKey,
) : BaseAddressableEvent(id, pubKey, createdAt, KIND, tags, content, sig) { ) : BaseAddressableEvent(id, pubKey, createdAt, KIND, tags, content, sig) {
fun badgeAwardEvents() = tags.filter { it.firstOrNull() == "e" }.mapNotNull { it.getOrNull(1) } fun badgeAwardEvents() = taggedEvents()
fun badgeAwardDefinitions() = fun badgeAwardDefinitions() = taggedAddresses()
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
}
companion object { companion object {
const val KIND = 30008 const val KIND = 30008
const val STANDARD_D_TAG = "profile_badges" private const val STANDARD_D_TAG = "profile_badges"
const val ALT = "List of accepted badges by the author" private const val ALT = "List of accepted badges by the author"
fun createAddressTag(pubKey: HexKey): ATag {
return ATag(KIND, pubKey, STANDARD_D_TAG, null)
}
} }
} }