Skip to content

Commit

Permalink
Optimizes the code for chat bubble rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
vitorpamplona committed May 16, 2023
1 parent 389afcb commit 1355965
Showing 1 changed file with 50 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,18 +315,18 @@ private fun StatusRow(
val grayTint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
val time = remember { baseNote.createdAt() ?: 0 }

Row() {
Row(verticalAlignment = Alignment.CenterVertically) {
ChatTimeAgo(time)
RelayBadges(baseNote)
Spacer(modifier = Modifier.width(10.dp))
}

Row() {
Row(verticalAlignment = Alignment.CenterVertically) {
LikeReaction(baseNote, grayTint, accountViewModel)
Spacer(modifier = Modifier.width(5.dp))
ZapReaction(baseNote, grayTint, accountViewModel)
Spacer(modifier = Modifier.width(5.dp))
ReplyReaction(baseNote, grayTint, accountViewModel, showCounter = false) {
ReplyReaction(baseNote, grayTint, accountViewModel, showCounter = false, iconSize = 16.dp) {
onWantsToReply(baseNote)
}
}
Expand Down Expand Up @@ -479,37 +479,49 @@ private fun DrawAuthorInfo(
}
}

data class RelayBadgesState(
val shouldDisplayExpandButton: Boolean,
val noteRelays: List<String>,
val noteRelaysSimple: List<String>
)

@Composable
private fun RelayBadges(baseNote: Note) {
val noteRelaysState by baseNote.live().relays.observeAsState()
val noteRelays = remember(noteRelaysState) { noteRelaysState?.note?.relays ?: emptySet() }
val noteRelaysSimple = remember(noteRelaysState) { noteRelaysState?.note?.relays?.take(3) ?: emptySet() }

val state: RelayBadgesState by remember(noteRelaysState) {
val newShouldDisplayExpandButton = (noteRelaysState?.note?.relays?.size ?: 0) > 3
val noteRelays = noteRelaysState?.note?.relays?.toList() ?: emptyList()
val noteRelaysSimple = noteRelaysState?.note?.relays?.take(3)?.toList() ?: emptyList()

mutableStateOf(RelayBadgesState(newShouldDisplayExpandButton, noteRelays, noteRelaysSimple))
}

var expanded by remember { mutableStateOf(false) }

val relaysToDisplay by remember {
derivedStateOf {
if (expanded) noteRelays else noteRelaysSimple
if (expanded) state.noteRelays else state.noteRelaysSimple
}
}

FlowRow(Modifier.padding(start = 10.dp)) {
relaysToDisplay.forEach {
RenderRelay(it)
}
}

if (noteRelays.size > 3 && !expanded) {
IconButton(
modifier = Modifier.then(Modifier.size(15.dp)),
onClick = { expanded = true }
) {
Icon(
imageVector = Icons.Default.ChevronRight,
null,
modifier = Modifier.size(15.dp),
tint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
)
}
if (state.shouldDisplayExpandButton && !expanded) {
IconButton(
modifier = Modifier.then(Modifier.size(15.dp)),
onClick = { expanded = true }
) {
Icon(
imageVector = Icons.Default.ChevronRight,
null,
modifier = Modifier.size(15.dp),
tint = MaterialTheme.colors.onSurface.copy(alpha = 0.32f)
)
}
}
}
Expand All @@ -526,24 +538,33 @@ private fun RenderRelay(dirtyUrl: String) {
"https://$cleanUrl/favicon.ico"
}

val clickableModifier = remember {
Modifier
.size(15.dp)
.padding(1.dp)
.clickable(onClick = { uri.openUri(website) })
}

val colorFilter = remember {
ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0.5f) })
}

val iconModifier = remember {
Modifier
.fillMaxSize(1f)
.clip(shape = CircleShape)
}

Box(
remember {
Modifier
.size(15.dp)
.padding(1.dp)
}
modifier = clickableModifier
) {
RobohashFallbackAsyncImage(
robot = iconUrl,
robotSize = 15.dp,
model = iconUrl,
contentDescription = stringResource(id = R.string.relay_icon),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }),
modifier = Modifier
.fillMaxSize(1f)
.clip(shape = CircleShape)
.background(MaterialTheme.colors.background)
.clickable(onClick = { uri.openUri(website) })
colorFilter = colorFilter,
modifier = iconModifier.background(MaterialTheme.colors.background)
)
}
}

0 comments on commit 1355965

Please sign in to comment.