allowing the image info to be missing and reusing scaling logic for other image messge types

This commit is contained in:
Adam Brown 2022-04-25 22:38:44 +01:00
parent 53f3fc01de
commit 347074b10f
5 changed files with 17 additions and 19 deletions

View File

@ -241,7 +241,7 @@ private fun MessageImage(content: BubbleContent<RoomEvent.Image>) {
Spacer(modifier = Modifier.height(4.dp))
Image(
modifier = Modifier.size(content.message.imageMeta.scaleMeta(LocalDensity.current, LocalConfiguration.current)),
modifier = Modifier.size(content.message.imageMeta.scale(LocalDensity.current, LocalConfiguration.current)),
painter = rememberImagePainter(
data = content.message,
builder = { fetcher(decryptingFetcher) }
@ -266,16 +266,18 @@ private fun MessageImage(content: BubbleContent<RoomEvent.Image>) {
}
}
private fun RoomEvent.Image.ImageMeta.scaleMeta(density: Density, configuration: Configuration): DpSize {
private fun RoomEvent.Image.ImageMeta.scale(density: Density, configuration: Configuration): DpSize {
val height = this@scale.height ?: 250
val width = this@scale.width ?: 250
return with(density) {
val scaler = minOf(
this@scaleMeta.height.scalerFor(configuration.screenHeightDp.dp.toPx() * 0.5f),
this@scaleMeta.width.scalerFor(configuration.screenWidthDp.dp.toPx() * 0.6f)
height.scalerFor(configuration.screenHeightDp.dp.toPx() * 0.5f),
width.scalerFor(configuration.screenWidthDp.dp.toPx() * 0.6f)
)
DpSize(
width = (this@scaleMeta.width * scaler).toDp(),
height = (this@scaleMeta.height * scaler).toDp(),
width = (width * scaler).toDp(),
height = (height * scaler).toDp(),
)
}
}
@ -405,11 +407,9 @@ private fun ReplyBubbleContent(content: BubbleContent<RoomEvent.Reply>) {
)
}
is RoomEvent.Image -> {
val width = with(LocalDensity.current) { replyingTo.imageMeta.width.toDp() }
val height = with(LocalDensity.current) { replyingTo.imageMeta.height.toDp() }
Spacer(modifier = Modifier.height(4.dp))
Image(
modifier = Modifier.size(width, height),
modifier = Modifier.size(replyingTo.imageMeta.scale(LocalDensity.current, LocalConfiguration.current)),
painter = rememberImagePainter(
data = replyingTo,
builder = { fetcher(DecryptingFetcher()) }
@ -442,11 +442,9 @@ private fun ReplyBubbleContent(content: BubbleContent<RoomEvent.Reply>) {
)
}
is RoomEvent.Image -> {
val width = with(LocalDensity.current) { message.imageMeta.width.toDp() }
val height = with(LocalDensity.current) { message.imageMeta.height.toDp() }
Spacer(modifier = Modifier.height(4.dp))
Image(
modifier = Modifier.size(width, height),
modifier = Modifier.size(message.imageMeta.scale(LocalDensity.current, LocalConfiguration.current)),
painter = rememberImagePainter(
data = content.message,
builder = { fetcher(DecryptingFetcher()) }

View File

@ -90,8 +90,8 @@ sealed class RoomEvent {
@Serializable
data class ImageMeta(
@SerialName("width") val width: Int,
@SerialName("height") val height: Int,
@SerialName("width") val width: Int?,
@SerialName("height") val height: Int?,
@SerialName("url") val url: String,
@SerialName("keys") val keys: Keys?,
) {

View File

@ -461,7 +461,7 @@ internal sealed class ApiTimelineEvent {
data class Image(
@SerialName("url") val url: MxUrl? = null,
@SerialName("file") val file: File? = null,
@SerialName("info") val info: Info,
@SerialName("info") val info: Info? = null,
@SerialName("m.relates_to") override val relation: Relation? = null,
@SerialName("msgtype") val messageType: String = "m.image",
) : Content {

View File

@ -35,8 +35,8 @@ internal class RoomEventsDecrypter(
val content = model.content as ApiTimelineEvent.TimelineMessage.Content.Image
this.copy(
imageMeta = RoomEvent.Image.ImageMeta(
width = content.info.width,
height = content.info.height,
width = content.info?.width,
height = content.info?.height,
url = content.file?.url?.convertMxUrToUrl(userCredentials.homeServer) ?: content.url!!.convertMxUrToUrl(userCredentials.homeServer),
keys = content.file?.let { RoomEvent.Image.ImageMeta.Keys(it.key.k, it.iv, it.v, it.hashes) }
),

View File

@ -45,8 +45,8 @@ internal class RoomEventFactory(
private fun ApiTimelineEvent.TimelineMessage.readImageMeta(userCredentials: UserCredentials): RoomEvent.Image.ImageMeta {
val content = this.content as ApiTimelineEvent.TimelineMessage.Content.Image
return RoomEvent.Image.ImageMeta(
content.info.width,
content.info.height,
content.info?.width,
content.info?.height,
content.file?.url?.convertMxUrToUrl(userCredentials.homeServer) ?: content.url!!.convertMxUrToUrl(userCredentials.homeServer),
keys = content.file?.let { RoomEvent.Image.ImageMeta.Keys(it.key.k, it.iv, it.v, it.hashes) }
)