refactor: more reusable functions

Signed-off-by: Infi <wingit@geist.ga>
This commit is contained in:
Infi 2023-04-27 19:05:18 +02:00
parent 815a8b2b23
commit 6e008a2148
4 changed files with 44 additions and 21 deletions

View File

@ -83,7 +83,7 @@ fun InReplyTo(
InlineBadges( InlineBadges(
bot = message.masquerade == null && author?.bot != null, 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), colour = contentColor.copy(alpha = 0.5f),
modifier = Modifier.size(8.dp), modifier = Modifier.size(8.dp),
followingIfAny = { followingIfAny = {

View File

@ -11,7 +11,7 @@ import chat.revolt.R
enum class InlineBadge { enum class InlineBadge {
Bot, Bot,
Masquerade, Bridge,
PlatformModeration, PlatformModeration,
Developer Developer
} }
@ -29,7 +29,7 @@ fun InlineBadge(
tint = colour, tint = colour,
modifier = modifier modifier = modifier
) )
InlineBadge.Masquerade -> Icon( InlineBadge.Bridge -> Icon(
painter = painterResource(id = R.drawable.ic_link_variant_24dp), painter = painterResource(id = R.drawable.ic_link_variant_24dp),
contentDescription = stringResource(id = R.string.badge_masquerade_alt), contentDescription = stringResource(id = R.string.badge_masquerade_alt),
tint = colour, tint = colour,
@ -44,14 +44,16 @@ fun InlineBadge(
fun InlineBadges( fun InlineBadges(
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
bot: Boolean = false, bot: Boolean = false,
masquerade: Boolean = false, bridge: Boolean = false,
platformModeration: Boolean = false, platformModeration: Boolean = false,
developer: Boolean = false, developer: Boolean = false,
colour: Color = Color.Unspecified, colour: Color = Color.Unspecified,
precedingIfAny: @Composable () -> Unit = {}, precedingIfAny: @Composable () -> Unit = {},
followingIfAny: @Composable () -> Unit = {}, followingIfAny: @Composable () -> Unit = {},
) { ) {
if (bot || masquerade || platformModeration || developer) { val hasBadges = bot || bridge || platformModeration || developer
if (hasBadges) {
precedingIfAny() precedingIfAny()
} }
@ -63,9 +65,9 @@ fun InlineBadges(
colour = colour colour = colour
) )
} }
if (masquerade) { if (bridge) {
InlineBadge( InlineBadge(
badge = InlineBadge.Masquerade, badge = InlineBadge.Bridge,
modifier = modifier, modifier = modifier,
colour = colour colour = colour
) )
@ -86,7 +88,7 @@ fun InlineBadges(
} }
} }
if (bot || masquerade || platformModeration || developer) { if (hasBadges) {
followingIfAny() followingIfAny()
} }
} }

View File

@ -19,6 +19,7 @@ import androidx.compose.material3.*
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource 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.activities.media.VideoViewActivity
import chat.revolt.api.REVOLT_FILES import chat.revolt.api.REVOLT_FILES
import chat.revolt.api.RevoltAPI import chat.revolt.api.RevoltAPI
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.internals.ULID import chat.revolt.api.internals.ULID
import chat.revolt.api.internals.WebCompat import chat.revolt.api.internals.WebCompat
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.schemas.AutumnResource import chat.revolt.api.schemas.AutumnResource
import chat.revolt.components.generic.UserAvatar import chat.revolt.components.generic.UserAvatar
import chat.revolt.components.generic.UserAvatarWidthPlaceholder import chat.revolt.components.generic.UserAvatarWidthPlaceholder
import chat.revolt.api.schemas.Message as MessageSchema 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) { fun viewUrlInBrowser(ctx: android.content.Context, url: String) {
val customTab = CustomTabsIntent val customTab = CustomTabsIntent
.Builder() .Builder()
@ -157,20 +176,16 @@ fun Message(
if (message.tail == false) { if (message.tail == false) {
Row(verticalAlignment = Alignment.CenterVertically) { Row(verticalAlignment = Alignment.CenterVertically) {
Text( Text(
text = message.masquerade?.name text = authorName(message),
?: author.username
?: stringResource(id = R.string.unknown),
fontWeight = FontWeight.Bold, fontWeight = FontWeight.Bold,
color = if (message.masquerade?.colour != null) { color = authorColour(message),
WebCompat.parseColour(message.masquerade.colour)
} else LocalContentColor.current,
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis overflow = TextOverflow.Ellipsis
) )
InlineBadges( InlineBadges(
bot = author.bot != null && message.masquerade == null, 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), colour = MaterialTheme.colorScheme.onBackground.copy(alpha = 0.5f),
modifier = Modifier.size(16.dp), modifier = Modifier.size(16.dp),
precedingIfAny = { precedingIfAny = {

View File

@ -32,8 +32,18 @@ import chat.revolt.api.RevoltAPI
import chat.revolt.api.internals.WebCompat import chat.revolt.api.internals.WebCompat
import chat.revolt.api.routes.channel.SendMessageReply import chat.revolt.api.routes.channel.SendMessageReply
import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl import chat.revolt.api.routes.microservices.january.asJanuaryProxyUrl
import chat.revolt.api.schemas.Message
import chat.revolt.components.generic.UserAvatar 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 @Composable
fun ManageableReply( fun ManageableReply(
reply: SendMessageReply, reply: SendMessageReply,
@ -90,11 +100,7 @@ fun ManageableReply(
) )
Text( Text(
text = if (replyMessage.content?.trim().isNullOrEmpty()) { text = replyContentText(replyMessage),
stringResource(id = R.string.reply_message_empty_has_attachments)
} else {
replyMessage.content!!
},
fontSize = 12.sp, fontSize = 12.sp,
maxLines = 1, maxLines = 1,
overflow = TextOverflow.Ellipsis, overflow = TextOverflow.Ellipsis,