Optimizes Drawer for Recomposition

This commit is contained in:
Vitor Pamplona
2023-05-22 15:07:13 -04:00
parent 5da49c920b
commit 460187c4a4
@@ -96,7 +96,7 @@ fun DrawerContent(
modifier = Modifier.padding(top = 20.dp) modifier = Modifier.padding(top = 20.dp)
) )
ListContent( ListContent(
account.userProfile(), account.userProfile().pubkeyHex,
navController, navController,
scaffoldState, scaffoldState,
sheetState, sheetState,
@@ -121,16 +121,25 @@ fun ProfileContent(
val coroutineScope = rememberCoroutineScope() val coroutineScope = rememberCoroutineScope()
val accountUserState by baseAccountUser.live().metadata.observeAsState() val accountUserState by baseAccountUser.live().metadata.observeAsState()
val accountUser = accountUserState?.user ?: return val accountUser = remember (accountUserState) { accountUserState?.user } ?: return
val profilePubHex = remember (accountUserState) { accountUserState?.user?.pubkeyHex } ?: return
val profileBanner = remember (accountUserState) { accountUserState?.user?.info?.banner?.ifBlank { null } }
val profilePicture = remember (accountUserState) { accountUserState?.user?.profilePicture()?.ifBlank { null }?.let { ResizeImage(it, 100.dp) } }
val bestUserName = remember(accountUserState) { accountUserState?.user?.bestUsername() }
val bestDisplayName = remember(accountUserState) { accountUserState?.user?.bestDisplayName() }
val tags = remember(accountUserState) { accountUserState?.user?.info?.latestMetadata?.tags }
val route = remember(accountUserState) { "User/${accountUserState?.user?.pubkeyHex}" }
val accountUserFollowsState by baseAccountUser.live().follows.observeAsState() val accountUserFollowsState by baseAccountUser.live().follows.observeAsState()
val accountUserFollows = accountUserFollowsState?.user ?: return val followingCount = remember(accountUserFollowsState) { accountUserFollowsState?.user?.cachedFollowCount()?.toString() ?: "--" }
val followerCount = remember(accountUserFollowsState) { accountUserFollowsState?.user?.cachedFollowerCount()?.toString() ?: "--"}
Box { Box {
val banner = accountUser.info?.banner if (profileBanner != null) {
if (!banner.isNullOrBlank()) {
AsyncImage( AsyncImage(
model = banner, model = profileBanner,
contentDescription = stringResource(id = R.string.profile_image), contentDescription = stringResource(id = R.string.profile_image),
contentScale = ContentScale.FillWidth, contentScale = ContentScale.FillWidth,
modifier = Modifier modifier = Modifier
@@ -149,35 +158,34 @@ fun ProfileContent(
} }
Column(modifier = modifier) { Column(modifier = modifier) {
RobohashAsyncImageProxy( profilePicture?.let {
robot = accountUser.pubkeyHex, RobohashAsyncImageProxy(
model = ResizeImage(accountUser.profilePicture(), 100.dp), robot = profilePubHex,
contentDescription = stringResource(id = R.string.profile_image), model = profilePicture,
modifier = Modifier contentDescription = stringResource(id = R.string.profile_image),
.width(100.dp) modifier = Modifier
.height(100.dp) .width(100.dp)
.clip(shape = CircleShape) .height(100.dp)
.border(3.dp, MaterialTheme.colors.background, CircleShape) .clip(shape = CircleShape)
.background(MaterialTheme.colors.background) .border(3.dp, MaterialTheme.colors.background, CircleShape)
.clickable(onClick = { .background(MaterialTheme.colors.background)
accountUser.let { .clickable(onClick = {
navController.navigate("User/${it.pubkeyHex}") navController.navigate(route)
} coroutineScope.launch {
coroutineScope.launch { scaffoldState.drawerState.close()
scaffoldState.drawerState.close() }
} })
}) )
) }
if (accountUser.bestDisplayName() != null) {
if (bestDisplayName != null) {
CreateTextWithEmoji( CreateTextWithEmoji(
text = accountUser.bestDisplayName() ?: "", text = bestDisplayName,
tags = accountUser.info?.latestMetadata?.tags, tags = tags,
modifier = Modifier modifier = Modifier
.padding(top = 7.dp) .padding(top = 7.dp)
.clickable(onClick = { .clickable(onClick = {
accountUser.let { navController.navigate(route)
navController.navigate("User/${it.pubkeyHex}")
}
coroutineScope.launch { coroutineScope.launch {
scaffoldState.drawerState.close() scaffoldState.drawerState.close()
} }
@@ -186,18 +194,16 @@ fun ProfileContent(
fontSize = 18.sp fontSize = 18.sp
) )
} }
if (accountUser.bestUsername() != null) { if (bestUserName != null) {
CreateTextWithEmoji( CreateTextWithEmoji(
text = " @${accountUser.bestUsername()}", text = " @${bestUserName}",
tags = accountUser.info?.latestMetadata?.tags, tags = accountUser.info?.latestMetadata?.tags,
color = Color.LightGray, color = Color.LightGray,
modifier = Modifier modifier = Modifier
.padding(top = 15.dp) .padding(top = 15.dp)
.clickable( .clickable(
onClick = { onClick = {
accountUser.let { navController.navigate(route)
navController.navigate("User/${it.pubkeyHex}")
}
coroutineScope.launch { coroutineScope.launch {
scaffoldState.drawerState.close() scaffoldState.drawerState.close()
} }
@@ -209,9 +215,7 @@ fun ProfileContent(
modifier = Modifier modifier = Modifier
.padding(top = 15.dp) .padding(top = 15.dp)
.clickable(onClick = { .clickable(onClick = {
accountUser.let { navController.navigate(route)
navController.navigate("User/${it.pubkeyHex}")
}
coroutineScope.launch { coroutineScope.launch {
scaffoldState.drawerState.close() scaffoldState.drawerState.close()
} }
@@ -219,14 +223,14 @@ fun ProfileContent(
) { ) {
Row() { Row() {
Text( Text(
"${accountUserFollows.cachedFollowCount() ?: "--"}", text = followingCount,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text(stringResource(R.string.following)) Text(stringResource(R.string.following))
} }
Row(modifier = Modifier.padding(start = 10.dp)) { Row(modifier = Modifier.padding(start = 10.dp)) {
Text( Text(
"${accountUserFollows.cachedFollowerCount() ?: "--"}", text = followerCount,
fontWeight = FontWeight.Bold fontWeight = FontWeight.Bold
) )
Text(stringResource(R.string.followers)) Text(stringResource(R.string.followers))
@@ -239,7 +243,7 @@ fun ProfileContent(
@OptIn(ExperimentalMaterialApi::class) @OptIn(ExperimentalMaterialApi::class)
@Composable @Composable
fun ListContent( fun ListContent(
accountUser: User?, accountUserPubKey: String?,
navController: NavHostController, navController: NavHostController,
scaffoldState: ScaffoldState, scaffoldState: ScaffoldState,
sheetState: ModalBottomSheetState, sheetState: ModalBottomSheetState,
@@ -254,14 +258,14 @@ fun ListContent(
var proxyPort = remember { mutableStateOf(account.proxyPort.toString()) } var proxyPort = remember { mutableStateOf(account.proxyPort.toString()) }
Column(modifier = modifier.fillMaxHeight().verticalScroll(rememberScrollState())) { Column(modifier = modifier.fillMaxHeight().verticalScroll(rememberScrollState())) {
if (accountUser != null) { if (accountUserPubKey != null) {
NavigationRow( NavigationRow(
title = stringResource(R.string.profile), title = stringResource(R.string.profile),
icon = Route.Profile.icon, icon = Route.Profile.icon,
tint = MaterialTheme.colors.primary, tint = MaterialTheme.colors.primary,
navController = navController, navController = navController,
scaffoldState = scaffoldState, scaffoldState = scaffoldState,
route = "User/${accountUser.pubkeyHex}" route = "User/$accountUserPubKey"
) )
NavigationRow( NavigationRow(