Faster ReactionRow compositions

This commit is contained in:
Vitor Pamplona
2023-08-22 12:23:45 -04:00
parent 1121cfcaf9
commit 0a49298ec2

View File

@@ -57,6 +57,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.math.BigDecimal import java.math.BigDecimal
import java.time.Instant import java.time.Instant
@@ -109,26 +110,58 @@ fun UserReactionsRow(
@Composable @Composable
private fun UserZapModel(model: UserReactionsViewModel) { private fun UserZapModel(model: UserReactionsViewModel) {
val zaps by model.zaps.collectAsState() Icon(
UserZapReaction(showAmountAxis(zaps[model.today()])) imageVector = Icons.Default.Bolt,
contentDescription = stringResource(R.string.zaps),
modifier = Size24Modifier,
tint = BitcoinOrange
)
Spacer(modifier = Modifier.width(8.dp))
UserZapReaction(model)
} }
@Composable @Composable
private fun UserReactionModel(model: UserReactionsViewModel) { private fun UserReactionModel(model: UserReactionsViewModel) {
val reactions by model.reactions.collectAsState() Icon(
UserLikeReaction(reactions[model.today()]) painter = painterResource(R.drawable.ic_liked),
null,
modifier = Size20Modifier,
tint = Color.Unspecified
)
Spacer(modifier = StdHorzSpacer)
UserLikeReaction(model)
} }
@Composable @Composable
private fun UserBoostModel(model: UserReactionsViewModel) { private fun UserBoostModel(model: UserReactionsViewModel) {
val boosts by model.boosts.collectAsState() Icon(
UserBoostReaction(boosts[model.today()]) painter = painterResource(R.drawable.ic_retweeted),
null,
modifier = Size24Modifier,
tint = Color.Unspecified
)
Spacer(modifier = StdHorzSpacer)
UserBoostReaction(model)
} }
@Composable @Composable
private fun UserReplyModel(model: UserReactionsViewModel) { private fun UserReplyModel(model: UserReactionsViewModel) {
val replies by model.replies.collectAsState() Icon(
UserReplyReaction(replies[model.today()]) painter = painterResource(R.drawable.ic_comment),
null,
modifier = Size20Modifier,
tint = RoyalBlue
)
Spacer(modifier = StdHorzSpacer)
UserReplyReaction(model)
} }
@Stable @Stable
@@ -154,6 +187,11 @@ class UserReactionsViewModel(val account: Account) : ViewModel() {
private var takenIntoAccount = setOf<HexKey>() private var takenIntoAccount = setOf<HexKey>()
private val sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd") // SimpleDateFormat() private val sdf = DateTimeFormatter.ofPattern("yyyy-MM-dd") // SimpleDateFormat()
val todaysReplyCount = _replies.map { showCount(it[today()]) }
val todaysBoostCount = _boosts.map { showCount(it[today()]) }
val todaysReactionCount = _reactions.map { showCount(it[today()]) }
val todaysZapAmount = _zaps.map { showAmountAxis(it[today()]) }
fun formatDate(createAt: Long): String { fun formatDate(createAt: Long): String {
return sdf.format( return sdf.format(
Instant.ofEpochSecond(createAt) Instant.ofEpochSecond(createAt)
@@ -339,18 +377,9 @@ class UserReactionsViewModel(val account: Account) : ViewModel() {
@Composable @Composable
fun UserReplyReaction( fun UserReplyReaction(
replyCount: Int? model: UserReactionsViewModel
) { ) {
val showCounts = remember(replyCount) { showCount(replyCount) } val showCounts by model.todaysReplyCount.collectAsState("")
Icon(
painter = painterResource(R.drawable.ic_comment),
null,
modifier = Size20Modifier,
tint = RoyalBlue
)
Spacer(modifier = StdHorzSpacer)
Text( Text(
showCounts, showCounts,
@@ -361,21 +390,12 @@ fun UserReplyReaction(
@Composable @Composable
fun UserBoostReaction( fun UserBoostReaction(
boostCount: Int? model: UserReactionsViewModel
) { ) {
val showCounts = remember(boostCount) { showCount(boostCount) } val boosts by model.todaysBoostCount.collectAsState("")
Icon(
painter = painterResource(R.drawable.ic_retweeted),
null,
modifier = Size24Modifier,
tint = Color.Unspecified
)
Spacer(modifier = StdHorzSpacer)
Text( Text(
showCounts, boosts,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
fontSize = 18.sp fontSize = 18.sp
) )
@@ -383,21 +403,12 @@ fun UserBoostReaction(
@Composable @Composable
fun UserLikeReaction( fun UserLikeReaction(
likeCount: Int? model: UserReactionsViewModel
) { ) {
val showCounts = remember(likeCount) { showCount(likeCount) } val reactions by model.todaysReactionCount.collectAsState("")
Icon(
painter = painterResource(R.drawable.ic_liked),
null,
modifier = Size20Modifier,
tint = Color.Unspecified
)
Spacer(modifier = StdHorzSpacer)
Text( Text(
text = showCounts, text = reactions,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
fontSize = 18.sp fontSize = 18.sp
) )
@@ -405,17 +416,9 @@ fun UserLikeReaction(
@Composable @Composable
fun UserZapReaction( fun UserZapReaction(
amount: String model: UserReactionsViewModel
) { ) {
Icon( val amount by model.todaysZapAmount.collectAsState("")
imageVector = Icons.Default.Bolt,
contentDescription = stringResource(R.string.zaps),
modifier = Size24Modifier,
tint = BitcoinOrange
)
Spacer(modifier = Modifier.width(8.dp))
Text( Text(
amount, amount,
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,