Minor code and performance improvements.

This commit is contained in:
Vitor Pamplona
2023-06-24 18:03:04 -04:00
parent 5cc7f3bf2b
commit 70a962062b
2 changed files with 81 additions and 71 deletions
@@ -6,6 +6,7 @@ import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
@@ -40,6 +41,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.text.withStyle import androidx.compose.ui.text.withStyle
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp import androidx.compose.ui.unit.sp
import androidx.lifecycle.map
import com.vitorpamplona.amethyst.R import com.vitorpamplona.amethyst.R
import com.vitorpamplona.amethyst.model.Channel import com.vitorpamplona.amethyst.model.Channel
import com.vitorpamplona.amethyst.model.HexKey import com.vitorpamplona.amethyst.model.HexKey
@@ -51,7 +53,9 @@ import com.vitorpamplona.amethyst.service.model.ChannelMetadataEvent
import com.vitorpamplona.amethyst.service.model.PrivateDmEvent import com.vitorpamplona.amethyst.service.model.PrivateDmEvent
import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy import com.vitorpamplona.amethyst.ui.components.RobohashAsyncImageProxy
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
import com.vitorpamplona.amethyst.ui.theme.Size55Modifier
import com.vitorpamplona.amethyst.ui.theme.Size55dp import com.vitorpamplona.amethyst.ui.theme.Size55dp
import com.vitorpamplona.amethyst.ui.theme.StdHorzSpacer
import com.vitorpamplona.amethyst.ui.theme.grayText import com.vitorpamplona.amethyst.ui.theme.grayText
import com.vitorpamplona.amethyst.ui.theme.placeholderText import com.vitorpamplona.amethyst.ui.theme.placeholderText
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@@ -63,11 +67,9 @@ fun ChatroomCompose(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
val noteState by baseNote.live().metadata.observeAsState() val isBlank by baseNote.live().metadata.map {
it.note.event == null
val isBlank = remember(noteState) { }.observeAsState(baseNote.event == null)
noteState?.note?.event == null
}
if (isBlank) { if (isBlank) {
BlankNote(Modifier) BlankNote(Modifier)
@@ -227,12 +229,23 @@ private fun UserRoomCompose(
accountViewModel: AccountViewModel, accountViewModel: AccountViewModel,
nav: (String) -> Unit nav: (String) -> Unit
) { ) {
var hasNewMessages = remember { mutableStateOf<Boolean>(false) } val hasNewMessages = remember { mutableStateOf<Boolean>(false) }
val route = remember(user) { val route = remember(user) {
"Room/${user.pubkeyHex}" "Room/${user.pubkeyHex}"
} }
val createAt by remember(note) {
derivedStateOf {
note.createdAt()
}
}
val content by remember(note) {
derivedStateOf {
accountViewModel.decrypt(note)
}
}
WatchNotificationChanges(note, route, accountViewModel) { newHasNewMessages -> WatchNotificationChanges(note, route, accountViewModel) { newHasNewMessages ->
if (hasNewMessages.value != newHasNewMessages) { if (hasNewMessages.value != newHasNewMessages) {
hasNewMessages.value = newHasNewMessages hasNewMessages.value = newHasNewMessages
@@ -248,8 +261,8 @@ private fun UserRoomCompose(
) )
}, },
channelTitle = { UsernameDisplay(user, it) }, channelTitle = { UsernameDisplay(user, it) },
channelLastTime = remember(note) { note.createdAt() }, channelLastTime = createAt,
channelLastContent = remember(note) { accountViewModel.decrypt(note) }, channelLastContent = content,
hasNewMessages = hasNewMessages, hasNewMessages = hasNewMessages,
onClick = { nav(route) } onClick = { nav(route) }
) )
@@ -338,16 +351,28 @@ fun ChannelName(
Row( Row(
modifier = remember { Modifier.padding(start = 12.dp, end = 12.dp, top = 10.dp) } modifier = remember { Modifier.padding(start = 12.dp, end = 12.dp, top = 10.dp) }
) { ) {
Column(remember { Modifier.width(Size55dp) }) { Column(Size55Modifier) {
channelPicture() channelPicture()
} }
Spacer(modifier = StdHorzSpacer)
Column( Column(
modifier = remember { Modifier.padding(start = 10.dp).fillMaxWidth() }, modifier = Modifier.fillMaxWidth(),
verticalArrangement = Arrangement.SpaceAround verticalArrangement = Arrangement.SpaceAround
) { ) {
FirstRow(channelTitle, channelLastTime) Row(
SecondRow(channelLastContent, hasNewMessages) verticalAlignment = Alignment.CenterVertically,
modifier = remember { Modifier.padding(bottom = 4.dp) }
) {
FirstRow(channelTitle, channelLastTime, remember { Modifier.weight(1f) })
}
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
SecondRow(channelLastContent, hasNewMessages, remember { Modifier.weight(1f) })
}
} }
} }
@@ -359,11 +384,7 @@ fun ChannelName(
} }
@Composable @Composable
private fun SecondRow(channelLastContent: String?, hasNewMessages: MutableState<Boolean>) { private fun SecondRow(channelLastContent: String?, hasNewMessages: MutableState<Boolean>, modifier: Modifier) {
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
if (channelLastContent != null) { if (channelLastContent != null) {
Text( Text(
channelLastContent, channelLastContent,
@@ -371,7 +392,7 @@ private fun SecondRow(channelLastContent: String?, hasNewMessages: MutableState<
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
style = LocalTextStyle.current.copy(textDirection = TextDirection.Content), style = LocalTextStyle.current.copy(textDirection = TextDirection.Content),
modifier = remember { Modifier.weight(1f) } modifier = modifier
) )
} else { } else {
Text( Text(
@@ -379,40 +400,40 @@ private fun SecondRow(channelLastContent: String?, hasNewMessages: MutableState<
color = MaterialTheme.colors.grayText, color = MaterialTheme.colors.grayText,
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,
modifier = remember { Modifier.weight(1f) } modifier = modifier
) )
} }
if (hasNewMessages.value) { if (hasNewMessages.value) {
NewItemsBubble() NewItemsBubble()
} }
}
} }
@Composable @Composable
private fun FirstRow( private fun FirstRow(
channelTitle: @Composable (Modifier) -> Unit, channelTitle: @Composable (Modifier) -> Unit,
channelLastTime: Long? channelLastTime: Long?,
modifier: Modifier
) { ) {
Row( channelTitle(modifier)
verticalAlignment = Alignment.CenterVertically, TimeAgo(channelLastTime)
modifier = remember { Modifier.padding(bottom = 4.dp) } }
) {
channelTitle( @Composable
remember { private fun TimeAgo(channelLastTime: Long?) {
Modifier.weight(1f) if (channelLastTime == null) return
}
)
channelLastTime?.let {
val context = LocalContext.current val context = LocalContext.current
val timeAgo = remember(channelLastTime) { timeAgo(channelLastTime, context) } val timeAgo by remember(channelLastTime) {
derivedStateOf {
timeAgo(channelLastTime, context)
}
}
Text( Text(
timeAgo, text = timeAgo,
color = MaterialTheme.colors.grayText color = MaterialTheme.colors.grayText,
maxLines = 1
) )
}
}
} }
@Composable @Composable
@@ -431,6 +452,7 @@ fun NewItemsBubble() {
color = Color.White, color = Color.White,
textAlign = TextAlign.Center, textAlign = TextAlign.Center,
fontSize = 12.sp, fontSize = 12.sp,
maxLines = 1,
modifier = Modifier modifier = Modifier
.wrapContentHeight() .wrapContentHeight()
.align(Alignment.Center) .align(Alignment.Center)
@@ -203,7 +203,7 @@ fun NoteCompose(
) { ) {
val isBlank by baseNote.live().metadata.map { val isBlank by baseNote.live().metadata.map {
it.note.event == null it.note.event == null
}.observeAsState(true) }.observeAsState(baseNote.event == null)
if (isBlank) { if (isBlank) {
LongPressToQuickAction(baseNote = baseNote, accountViewModel = accountViewModel) { showPopup -> LongPressToQuickAction(baseNote = baseNote, accountViewModel = accountViewModel) { showPopup ->
@@ -3154,21 +3154,9 @@ fun PictureAndFollowingMark(
@Composable @Composable
private fun ObserveAndDisplayFollowingMark(userHex: String, iconSize: Dp, accountViewModel: AccountViewModel) { private fun ObserveAndDisplayFollowingMark(userHex: String, iconSize: Dp, accountViewModel: AccountViewModel) {
val accountFollowsState by accountViewModel.userFollows.observeAsState() val showFollowingMark by accountViewModel.userFollows.map {
it.user.isFollowingCached(userHex) == true || (userHex == accountViewModel.account.userProfile().pubkeyHex)
var showFollowingMark by remember { mutableStateOf(false) } }.observeAsState(true)
LaunchedEffect(key1 = accountFollowsState) {
launch(Dispatchers.Default) {
val newShowFollowingMark =
accountFollowsState?.user?.isFollowingCached(userHex) == true ||
(userHex == accountViewModel.account.userProfile().pubkeyHex)
if (newShowFollowingMark != showFollowingMark) {
showFollowingMark = newShowFollowingMark
}
}
}
if (showFollowingMark) { if (showFollowingMark) {
FollowingIcon(iconSize) FollowingIcon(iconSize)