From 6e008a21483ecf28d8662cb5457c7fc4694cd3f8 Mon Sep 17 00:00:00 2001 From: Infi Date: Thu, 27 Apr 2023 19:05:18 +0200 Subject: [PATCH] refactor: more reusable functions Signed-off-by: Infi --- .../chat/revolt/components/chat/InReplyTo.kt | 2 +- .../revolt/components/chat/InlineBadge.kt | 16 +++++----- .../chat/revolt/components/chat/Message.kt | 31 ++++++++++++++----- .../components/screens/chat/ReplyManager.kt | 16 +++++++--- 4 files changed, 44 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/chat/revolt/components/chat/InReplyTo.kt b/app/src/main/java/chat/revolt/components/chat/InReplyTo.kt index 99ed1d24..626fa2ae 100644 --- a/app/src/main/java/chat/revolt/components/chat/InReplyTo.kt +++ b/app/src/main/java/chat/revolt/components/chat/InReplyTo.kt @@ -83,7 +83,7 @@ fun InReplyTo( InlineBadges( bot = message.masquerade == null && author?.bot != null, - masquerade = message.masquerade != null && author?.bot != null, + bridge = message.masquerade != null && author?.bot != null, colour = contentColor.copy(alpha = 0.5f), modifier = Modifier.size(8.dp), followingIfAny = { diff --git a/app/src/main/java/chat/revolt/components/chat/InlineBadge.kt b/app/src/main/java/chat/revolt/components/chat/InlineBadge.kt index a5f8d7fc..045f604b 100644 --- a/app/src/main/java/chat/revolt/components/chat/InlineBadge.kt +++ b/app/src/main/java/chat/revolt/components/chat/InlineBadge.kt @@ -11,7 +11,7 @@ import chat.revolt.R enum class InlineBadge { Bot, - Masquerade, + Bridge, PlatformModeration, Developer } @@ -29,7 +29,7 @@ fun InlineBadge( tint = colour, modifier = modifier ) - InlineBadge.Masquerade -> Icon( + InlineBadge.Bridge -> Icon( painter = painterResource(id = R.drawable.ic_link_variant_24dp), contentDescription = stringResource(id = R.string.badge_masquerade_alt), tint = colour, @@ -44,14 +44,16 @@ fun InlineBadge( fun InlineBadges( modifier: Modifier = Modifier, bot: Boolean = false, - masquerade: Boolean = false, + bridge: Boolean = false, platformModeration: Boolean = false, developer: Boolean = false, colour: Color = Color.Unspecified, precedingIfAny: @Composable () -> Unit = {}, followingIfAny: @Composable () -> Unit = {}, ) { - if (bot || masquerade || platformModeration || developer) { + val hasBadges = bot || bridge || platformModeration || developer + + if (hasBadges) { precedingIfAny() } @@ -63,9 +65,9 @@ fun InlineBadges( colour = colour ) } - if (masquerade) { + if (bridge) { InlineBadge( - badge = InlineBadge.Masquerade, + badge = InlineBadge.Bridge, modifier = modifier, colour = colour ) @@ -86,7 +88,7 @@ fun InlineBadges( } } - if (bot || masquerade || platformModeration || developer) { + if (hasBadges) { followingIfAny() } } \ No newline at end of file diff --git a/app/src/main/java/chat/revolt/components/chat/Message.kt b/app/src/main/java/chat/revolt/components/chat/Message.kt index 07fd184a..126943ea 100644 --- a/app/src/main/java/chat/revolt/components/chat/Message.kt +++ b/app/src/main/java/chat/revolt/components/chat/Message.kt @@ -19,6 +19,7 @@ import androidx.compose.material3.* import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource @@ -33,14 +34,32 @@ import chat.revolt.activities.media.ImageViewActivity import chat.revolt.activities.media.VideoViewActivity import chat.revolt.api.REVOLT_FILES import chat.revolt.api.RevoltAPI -import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl import chat.revolt.api.internals.ULID import chat.revolt.api.internals.WebCompat +import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl import chat.revolt.api.schemas.AutumnResource import chat.revolt.components.generic.UserAvatar import chat.revolt.components.generic.UserAvatarWidthPlaceholder import chat.revolt.api.schemas.Message as MessageSchema +@Composable +fun authorColour(message: MessageSchema): Color { + return if (message.masquerade?.colour != null) { + WebCompat.parseColour(message.masquerade.colour) + } else { + LocalContentColor.current + } +} + +@Composable +fun authorName(message: MessageSchema): String { + return if (message.masquerade?.name != null) { + message.masquerade.name + } else { + RevoltAPI.userCache[message.author]?.username ?: stringResource(id = R.string.unknown) + } +} + fun viewUrlInBrowser(ctx: android.content.Context, url: String) { val customTab = CustomTabsIntent .Builder() @@ -157,20 +176,16 @@ fun Message( if (message.tail == false) { Row(verticalAlignment = Alignment.CenterVertically) { Text( - text = message.masquerade?.name - ?: author.username - ?: stringResource(id = R.string.unknown), + text = authorName(message), fontWeight = FontWeight.Bold, - color = if (message.masquerade?.colour != null) { - WebCompat.parseColour(message.masquerade.colour) - } else LocalContentColor.current, + color = authorColour(message), maxLines = 1, overflow = TextOverflow.Ellipsis ) InlineBadges( bot = author.bot != null && message.masquerade == null, - masquerade = message.masquerade != null && author.bot != null, + bridge = message.masquerade != null && author.bot != null, colour = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.5f), modifier = Modifier.size(16.dp), precedingIfAny = { diff --git a/app/src/main/java/chat/revolt/components/screens/chat/ReplyManager.kt b/app/src/main/java/chat/revolt/components/screens/chat/ReplyManager.kt index aeab75ed..f675d201 100644 --- a/app/src/main/java/chat/revolt/components/screens/chat/ReplyManager.kt +++ b/app/src/main/java/chat/revolt/components/screens/chat/ReplyManager.kt @@ -32,8 +32,18 @@ import chat.revolt.api.RevoltAPI import chat.revolt.api.internals.WebCompat import chat.revolt.api.routes.channel.SendMessageReply import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl +import chat.revolt.api.schemas.Message import chat.revolt.components.generic.UserAvatar +@Composable +fun replyContentText(message: Message): String { + return if (message.content.isNullOrBlank()) { + stringResource(id = R.string.reply_message_empty_has_attachments) + } else { + message.content + } +} + @Composable fun ManageableReply( reply: SendMessageReply, @@ -90,11 +100,7 @@ fun ManageableReply( ) Text( - text = if (replyMessage.content?.trim().isNullOrEmpty()) { - stringResource(id = R.string.reply_message_empty_has_attachments) - } else { - replyMessage.content!! - }, + text = replyContentText(replyMessage), fontSize = 12.sp, maxLines = 1, overflow = TextOverflow.Ellipsis,