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(
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 = {

View File

@ -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()
}
}

View File

@ -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 = {

View File

@ -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,