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

View File

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

View File

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

View File

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