fix: images always being full-width

Signed-off-by: Infi <infi@infi.sh>
This commit is contained in:
Infi 2023-11-01 17:59:32 +01:00
parent 88faa795ba
commit 89cc41b414
1 changed files with 45 additions and 37 deletions

View File

@ -4,6 +4,7 @@ import android.text.format.Formatter
import androidx.compose.foundation.background import androidx.compose.foundation.background
import androidx.compose.foundation.clickable import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxWithConstraints
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.Spacer
@ -75,54 +76,61 @@ fun FileAttachment(attachment: AutumnResource) {
fun ImageAttachment(attachment: AutumnResource) { fun ImageAttachment(attachment: AutumnResource) {
val url = "$REVOLT_FILES/attachments/${attachment.id}/${attachment.filename}" val url = "$REVOLT_FILES/attachments/${attachment.id}/${attachment.filename}"
RemoteImage( BoxWithConstraints {
url = url, RemoteImage(
contentScale = ContentScale.Fit, url = url,
modifier = Modifier contentScale = ContentScale.Fit,
.fillMaxWidth() modifier = Modifier
.aspectRatio( .width(attachment.metadata?.width?.toInt()?.dp ?: maxWidth)
attachment.metadata!!.width!!.toFloat() / attachment.metadata.height!!.toFloat() .aspectRatio(
), attachment.metadata!!.width!!.toFloat() / attachment.metadata.height!!.toFloat()
description = attachment.filename ?: "Image" ),
) description = attachment.filename ?: "Image"
)
}
} }
@Composable @Composable
fun VideoAttachment(attachment: AutumnResource) { fun VideoAttachment(attachment: AutumnResource) {
val url = "$REVOLT_FILES/attachments/${attachment.id}/${attachment.filename}" val url = "$REVOLT_FILES/attachments/${attachment.id}/${attachment.filename}"
Box( BoxWithConstraints {
contentAlignment = Alignment.Center, Box(
modifier = Modifier contentAlignment = Alignment.Center,
.fillMaxWidth()
) {
// Turns out that when you give Glide a video URL, you get a perfectly cromulent thumbnail.
RemoteImage(
url = url,
contentScale = ContentScale.Fit,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .width(attachment.metadata?.width?.toInt()?.dp ?: maxWidth)
.aspectRatio( .aspectRatio(
attachment.metadata!!.width!!.toFloat() / attachment.metadata.height!!.toFloat() attachment.metadata!!.width!!.toFloat() / attachment.metadata.height!!.toFloat()
), )
description = attachment.filename ?: "Video" ) {
) // Turns out that when you give Glide a video URL, you get a perfectly cromulent thumbnail.
RemoteImage(
url = url,
contentScale = ContentScale.Fit,
modifier = Modifier
.fillMaxWidth()
.aspectRatio(
attachment.metadata.width!!.toFloat() / attachment.metadata.height.toFloat()
),
description = attachment.filename ?: "Video"
)
Box( Box(
modifier = Modifier modifier = Modifier
.width(48.dp) .width(48.dp)
.aspectRatio(1f) .aspectRatio(1f)
.clip(MaterialTheme.shapes.medium) .clip(MaterialTheme.shapes.medium)
.background(MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp)) .background(MaterialTheme.colorScheme.surfaceColorAtElevation(2.dp))
) )
Icon( Icon(
imageVector = Icons.Default.PlayArrow, imageVector = Icons.Default.PlayArrow,
contentDescription = stringResource(id = R.string.media_viewer_play), contentDescription = stringResource(id = R.string.media_viewer_play),
modifier = Modifier modifier = Modifier
.width(32.dp) .width(32.dp)
.aspectRatio(1f) .aspectRatio(1f)
) )
}
} }
} }