Shows WoT Followers

This commit is contained in:
Vitor Pamplona
2025-12-02 19:15:51 -05:00
parent bb77163955
commit e7c1925c76
5 changed files with 120 additions and 14 deletions
@@ -32,6 +32,7 @@ import kotlinx.coroutines.flow.emitAll
import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update import kotlinx.coroutines.flow.update
import java.text.NumberFormat
class UserCardsCache : UserDependencies { class UserCardsCache : UserDependencies {
val receivedCards = MutableStateFlow(mapOf<User, AddressableNote>()) val receivedCards = MutableStateFlow(mapOf<User, AddressableNote>())
@@ -107,4 +108,38 @@ class UserCardsCache : UserDependencies {
}.map { }.map {
(it?.note?.event as? ContactCardEvent)?.rank() (it?.note?.event as? ContactCardEvent)?.rank()
}.flowOn(Dispatchers.IO) }.flowOn(Dispatchers.IO)
val formatter = NumberFormat.getInstance()
fun followerCountStrFlow(trustProviderList: TrustProviderListState) =
combineTransform(receivedCards, trustProviderList.liveUserFollowerCount) { cards, provider ->
if (provider != null) {
val flow =
cards.firstNotNullOfOrNull {
if (it.key.pubkeyHex == provider.pubkey) {
it.value
.flow()
.metadata.stateFlow
} else {
null
}
}
if (flow != null) {
emitAll(flow)
} else {
emit(null)
}
} else {
emit(null)
}
}.map {
val value = (it?.note?.event as? ContactCardEvent)?.followerCount()
if (value != null && value > 0) {
formatter.format(value)
} else {
"--"
}
}.flowOn(Dispatchers.IO)
} }
@@ -616,6 +616,21 @@ fun observeUserContactCardsScore(
return flow.collectAsStateWithLifecycle(null) return flow.collectAsStateWithLifecycle(null)
} }
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
@Composable
fun observeUserContactCardsFollowerCount(
user: User,
accountViewModel: AccountViewModel,
): State<String> {
// Subscribe in the relay for changes in the metadata of this user.
UserFinderFilterAssemblerSubscription(user, accountViewModel)
// Subscribe in the LocalCache for changes that arrive in the device
val flow = remember(user) { user.cards().followerCountStrFlow(accountViewModel.account.trustProviderList) }
return flow.collectAsStateWithLifecycle("--")
}
@OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class) @OptIn(ExperimentalCoroutinesApi::class, FlowPreview::class)
@Composable @Composable
fun observeUserStatuses( fun observeUserStatuses(
@@ -20,6 +20,7 @@
*/ */
package com.vitorpamplona.amethyst.ui.navigation.drawer package com.vitorpamplona.amethyst.ui.navigation.drawer
import android.R.attr.fontWeight
import androidx.compose.foundation.Image import androidx.compose.foundation.Image
import androidx.compose.foundation.border import androidx.compose.foundation.border
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
@@ -95,7 +96,7 @@ import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Account import com.vitorpamplona.amethyst.model.Account
import com.vitorpamplona.amethyst.model.User import com.vitorpamplona.amethyst.model.User
import com.vitorpamplona.amethyst.service.relayClient.reqCommand.event.observeNote import com.vitorpamplona.amethyst.service.relayClient.reqCommand.event.observeNote
import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserFollowerCount import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserContactCardsFollowerCount
import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserInfo import com.vitorpamplona.amethyst.service.relayClient.reqCommand.user.observeUserInfo
import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji import com.vitorpamplona.amethyst.ui.components.CreateTextWithEmoji
import com.vitorpamplona.amethyst.ui.components.RobohashFallbackAsyncImage import com.vitorpamplona.amethyst.ui.components.RobohashFallbackAsyncImage
@@ -121,6 +122,7 @@ import com.vitorpamplona.amethyst.ui.theme.bannerModifier
import com.vitorpamplona.amethyst.ui.theme.drawerSpacing import com.vitorpamplona.amethyst.ui.theme.drawerSpacing
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import com.vitorpamplona.amethyst.ui.theme.profileContentHeaderModifier import com.vitorpamplona.amethyst.ui.theme.profileContentHeaderModifier
import com.vitorpamplona.quartz.experimental.trustedAssertions.list.tags.ProviderTypes.followerCount
import com.vitorpamplona.quartz.nip01Core.core.Address import com.vitorpamplona.quartz.nip01Core.core.Address
import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.core.HexKey
import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists import com.vitorpamplona.quartz.nip01Core.core.ImmutableListOfLists
@@ -395,29 +397,41 @@ private fun FollowingAndFollowerCounts(
Row( Row(
modifier = drawerSpacing.clickable(onClick = onClick), modifier = drawerSpacing.clickable(onClick = onClick),
) { ) {
val followingCount = baseAccountUser.kind3FollowList.flow.collectAsStateWithLifecycle() DisplayFollowingCount(baseAccountUser)
val followerCount by observeUserFollowerCount(baseAccountUser.userProfile(), accountViewModel)
Text(
text =
followingCount.value.authors.size
.toString(),
fontWeight = FontWeight.Bold,
)
Text(stringRes(R.string.following)) Text(stringRes(R.string.following))
Spacer(modifier = DoubleHorzSpacer) Spacer(modifier = DoubleHorzSpacer)
Text( DisplayFollowerCount(baseAccountUser, accountViewModel)
text = if (followerCount > 0) followerCount.toString() else "--",
fontWeight = FontWeight.Bold,
)
Text(stringRes(R.string.followers)) Text(stringRes(R.string.followers))
} }
} }
@Composable
fun DisplayFollowingCount(baseAccountUser: Account) {
val followingCount by baseAccountUser.kind3FollowList.flow.collectAsStateWithLifecycle()
Text(
text = followingCount.authors.size.toString(),
fontWeight = FontWeight.Bold,
)
}
@Composable
fun DisplayFollowerCount(
baseAccountUser: Account,
accountViewModel: AccountViewModel,
) {
val followerCount by observeUserContactCardsFollowerCount(baseAccountUser.userProfile(), accountViewModel)
Text(
text = followerCount,
fontWeight = FontWeight.Bold,
)
}
@Composable @Composable
fun ListContent( fun ListContent(
modifier: Modifier, modifier: Modifier,
@@ -21,6 +21,7 @@
package com.vitorpamplona.quartz.experimental.relationshipStatus package com.vitorpamplona.quartz.experimental.relationshipStatus
import androidx.compose.runtime.Immutable import androidx.compose.runtime.Immutable
import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.FollowerCountTag
import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.PetNameTag import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.PetNameTag
import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.RankTag import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.RankTag
import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.SummaryTag import com.vitorpamplona.quartz.experimental.relationshipStatus.tags.SummaryTag
@@ -49,6 +50,8 @@ class ContactCardEvent(
fun rank() = tags.firstNotNullOfOrNull(RankTag::parse) fun rank() = tags.firstNotNullOfOrNull(RankTag::parse)
fun followerCount() = tags.firstNotNullOfOrNull(FollowerCountTag::parse)
fun petName() = tags.firstNotNullOfOrNull(PetNameTag::parse) fun petName() = tags.firstNotNullOfOrNull(PetNameTag::parse)
fun summary() = tags.firstNotNullOfOrNull(SummaryTag::parse) fun summary() = tags.firstNotNullOfOrNull(SummaryTag::parse)
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2025 Vitor Pamplona
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
* Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.vitorpamplona.quartz.experimental.relationshipStatus.tags
import com.vitorpamplona.quartz.nip01Core.core.has
import com.vitorpamplona.quartz.utils.ensure
class FollowerCountTag {
companion object {
const val TAG_NAME = "followers"
fun parse(tag: Array<String>): Int? {
ensure(tag.has(1)) { return null }
ensure(tag[0] == TAG_NAME) { return null }
ensure(tag[1].isNotEmpty()) { return null }
return tag[1].toIntOrNull()
}
fun assemble(followerCount: Int) = arrayOf(TAG_NAME, followerCount.toString())
}
}