Increasing performance of the follow/follower count

This commit is contained in:
Vitor Pamplona
2023-06-16 19:11:14 -04:00
parent 3cd0828cba
commit e3eae80d4c
3 changed files with 54 additions and 30 deletions
@@ -67,6 +67,7 @@ import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountBackupDialog
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.screen.loggedIn.ConnectOrbotDialog
import com.vitorpamplona.amethyst.ui.theme.DoubleHorzSpacer
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
@@ -229,39 +230,57 @@ fun ProfileContent(
@Composable
private fun FollowingAndFollowerCounts(baseAccountUser: User) {
val accountUserFollowsState by baseAccountUser.live().follows.observeAsState()
val accountUserFollowersState by baseAccountUser.live().followers.observeAsState()
var followingCount by remember { mutableStateOf("--") }
var followerCount by remember { mutableStateOf("--") }
LaunchedEffect(key1 = accountUserFollowsState, key2 = accountUserFollowersState) {
launch(Dispatchers.IO) {
val newFollowing = accountUserFollowsState?.user?.cachedFollowCount()?.toString() ?: "--"
val newFollower = accountUserFollowersState?.user?.cachedFollowerCount()?.toString() ?: "--"
if (followingCount != newFollowing) {
followingCount = newFollowing
}
if (followerCount != newFollower) {
followerCount = newFollower
}
WatchFollow(baseAccountUser = baseAccountUser) { newFollowing ->
if (followingCount != newFollowing) {
followingCount = newFollowing
}
}
Row() {
Text(
text = followingCount,
fontWeight = FontWeight.Bold
)
Text(stringResource(R.string.following))
WatchFollower(baseAccountUser = baseAccountUser) { newFollower ->
if (followerCount != newFollower) {
followerCount = newFollower
}
}
Row(modifier = Modifier.padding(start = 10.dp)) {
Text(
text = followerCount,
fontWeight = FontWeight.Bold
)
Text(stringResource(R.string.followers))
Text(
text = followingCount,
fontWeight = FontWeight.Bold
)
Text(stringResource(R.string.following))
Spacer(modifier = DoubleHorzSpacer)
Text(
text = followerCount,
fontWeight = FontWeight.Bold
)
Text(stringResource(R.string.followers))
}
@Composable
fun WatchFollow(baseAccountUser: User, onReady: (String) -> Unit) {
val accountUserFollowsState by baseAccountUser.live().follows.observeAsState()
LaunchedEffect(key1 = accountUserFollowsState) {
launch(Dispatchers.IO) {
onReady(accountUserFollowsState?.user?.cachedFollowCount()?.toString() ?: "--")
}
}
}
@Composable
fun WatchFollower(baseAccountUser: User, onReady: (String) -> Unit) {
val accountUserFollowersState by baseAccountUser.live().followers.observeAsState()
LaunchedEffect(key1 = accountUserFollowersState) {
launch(Dispatchers.IO) {
onReady(accountUserFollowersState?.user?.cachedFollowerCount()?.toString() ?: "--")
}
}
}
@@ -385,9 +385,11 @@ private fun FollowersTabHeader(baseUser: User) {
val userState by baseUser.live().followers.observeAsState()
var followerCount by remember { mutableStateOf("--") }
val text = stringResource(R.string.followers)
LaunchedEffect(key1 = userState) {
launch(Dispatchers.IO) {
val newFollower = userState?.user?.transientFollowerCount()?.toString() ?: "--"
val newFollower = (userState?.user?.transientFollowerCount()?.toString() ?: "--") + " " + text
if (followerCount != newFollower) {
followerCount = newFollower
@@ -395,7 +397,7 @@ private fun FollowersTabHeader(baseUser: User) {
}
}
Text(text = "$followerCount ${stringResource(id = R.string.followers)}")
Text(text = followerCount)
}
@Composable
@@ -403,9 +405,11 @@ private fun FollowTabHeader(baseUser: User) {
val userState by baseUser.live().follows.observeAsState()
var followCount by remember { mutableStateOf("--") }
val text = stringResource(R.string.follows)
LaunchedEffect(key1 = userState) {
launch(Dispatchers.IO) {
val newFollow = userState?.user?.transientFollowCount()?.toString() ?: "--"
val newFollow = (userState?.user?.transientFollowCount()?.toString() ?: "--") + " " + text
if (followCount != newFollow) {
followCount = newFollow
@@ -413,7 +417,7 @@ private fun FollowTabHeader(baseUser: User) {
}
}
Text(text = "$followCount ${stringResource(R.string.follows)}")
Text(text = followCount)
}
@Composable
@@ -21,3 +21,4 @@ val ChatBubbleShapeThem = RoundedCornerShape(3.dp, 15.dp, 15.dp, 15.dp)
val StdButtonSizeModifier = Modifier.size(20.dp)
val StdHorzSpacer = Modifier.width(5.dp)
val DoubleHorzSpacer = Modifier.width(10.dp)