Take screen density into account when requesting thumbnails (#1262)
* Take screen density into account when requesting thumbnails Otherwise, we could be asking for images with N size while we needed images for N@2x or N@3x size i.e.
This commit is contained in:
committed by
GitHub
parent
ac6d822bef
commit
ddb8f66c32
@@ -16,6 +16,7 @@
|
||||
|
||||
package io.element.android.libraries.matrix.ui.media
|
||||
|
||||
import android.content.Context
|
||||
import coil.ImageLoader
|
||||
import coil.decode.DataSource
|
||||
import coil.decode.ImageSource
|
||||
@@ -32,8 +33,10 @@ import okio.Buffer
|
||||
import okio.Path.Companion.toOkioPath
|
||||
import timber.log.Timber
|
||||
import java.nio.ByteBuffer
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
internal class CoilMediaFetcher(
|
||||
private val scalingFunction: (Float) -> Float,
|
||||
private val mediaLoader: MatrixMediaLoader,
|
||||
private val mediaData: MediaRequestData?,
|
||||
private val options: Options
|
||||
@@ -80,8 +83,8 @@ internal class CoilMediaFetcher(
|
||||
private suspend fun fetchThumbnail(mediaSource: MediaSource, kind: MediaRequestData.Kind.Thumbnail, options: Options): FetchResult? {
|
||||
return mediaLoader.loadMediaThumbnail(
|
||||
source = mediaSource,
|
||||
width = kind.width,
|
||||
height = kind.height
|
||||
width = scalingFunction(kind.width.toFloat()).roundToLong(),
|
||||
height = scalingFunction(kind.height.toFloat()).roundToLong(),
|
||||
).map { byteArray ->
|
||||
byteArray.asSourceResult(options)
|
||||
}.getOrNull()
|
||||
@@ -102,6 +105,7 @@ internal class CoilMediaFetcher(
|
||||
}
|
||||
|
||||
class MediaRequestDataFactory(
|
||||
private val context: Context,
|
||||
private val client: MatrixClient
|
||||
) :
|
||||
Fetcher.Factory<MediaRequestData> {
|
||||
@@ -111,6 +115,7 @@ internal class CoilMediaFetcher(
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data,
|
||||
options = options
|
||||
@@ -119,6 +124,7 @@ internal class CoilMediaFetcher(
|
||||
}
|
||||
|
||||
class AvatarFactory(
|
||||
private val context: Context,
|
||||
private val client: MatrixClient
|
||||
) :
|
||||
Fetcher.Factory<AvatarData> {
|
||||
@@ -129,6 +135,7 @@ internal class CoilMediaFetcher(
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data.toMediaRequestData(),
|
||||
options = options
|
||||
|
||||
@@ -46,8 +46,8 @@ class LoggedInImageLoaderFactory @Inject constructor(
|
||||
}
|
||||
add(AvatarDataKeyer())
|
||||
add(MediaRequestDataKeyer())
|
||||
add(CoilMediaFetcher.AvatarFactory(matrixClient))
|
||||
add(CoilMediaFetcher.MediaRequestDataFactory(matrixClient))
|
||||
add(CoilMediaFetcher.AvatarFactory(context, matrixClient))
|
||||
add(CoilMediaFetcher.MediaRequestDataFactory(context, matrixClient))
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user