misc : makes sure to always use the same size for fetching avatar thumbnail so caching works properly.
This commit is contained in:
parent
366db4791b
commit
2e631b1ae5
7 changed files with 28 additions and 20 deletions
|
|
@ -7,7 +7,6 @@
|
|||
|
||||
package io.element.android.libraries.matrix.ui.media
|
||||
|
||||
import android.content.Context
|
||||
import coil.ImageLoader
|
||||
import coil.fetch.Fetcher
|
||||
import coil.request.Options
|
||||
|
|
@ -15,7 +14,6 @@ import io.element.android.libraries.designsystem.components.avatar.AvatarData
|
|||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
|
||||
internal class AvatarDataFetcherFactory(
|
||||
private val context: Context,
|
||||
private val client: MatrixClient
|
||||
) : Fetcher.Factory<AvatarData> {
|
||||
override fun create(
|
||||
|
|
@ -24,7 +22,6 @@ internal class AvatarDataFetcherFactory(
|
|||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data.toMediaRequestData(),
|
||||
options = options
|
||||
|
|
|
|||
|
|
@ -9,11 +9,25 @@ package io.element.android.libraries.matrix.ui.media
|
|||
|
||||
import io.element.android.libraries.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.libraries.matrix.api.media.MediaSource
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
/**
|
||||
* The size in pixel of the thumbnail to generate for the avatar.
|
||||
* This is not the size of the avatar displayed in the UI but the size to get from the servers.
|
||||
* Servers SHOULD produce thumbnails with the following dimensions and methods:
|
||||
*
|
||||
* 32x32, crop
|
||||
* 96x96, crop
|
||||
* 320x240, scale
|
||||
* 640x480, scale
|
||||
* 800x600, scale
|
||||
*
|
||||
* Let's always use the same size so coil caching works properly.
|
||||
*/
|
||||
const val AVATAR_THUMBNAIL_SIZE_IN_PIXEL = 240L
|
||||
|
||||
internal fun AvatarData.toMediaRequestData(): MediaRequestData {
|
||||
return MediaRequestData(
|
||||
source = url?.let { MediaSource(it) },
|
||||
kind = MediaRequestData.Kind.Thumbnail(size.dp.value.roundToLong())
|
||||
kind = MediaRequestData.Kind.Thumbnail(AVATAR_THUMBNAIL_SIZE_IN_PIXEL)
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,10 +20,8 @@ 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
|
||||
|
|
@ -74,8 +72,8 @@ internal class CoilMediaFetcher(
|
|||
private suspend fun fetchThumbnail(mediaSource: MediaSource, kind: MediaRequestData.Kind.Thumbnail, options: Options): FetchResult? {
|
||||
return mediaLoader.loadMediaThumbnail(
|
||||
source = mediaSource,
|
||||
width = scalingFunction(kind.width.toFloat()).roundToLong(),
|
||||
height = scalingFunction(kind.height.toFloat()).roundToLong(),
|
||||
width = kind.width,
|
||||
height = kind.height,
|
||||
).map { byteArray ->
|
||||
byteArray.asSourceResult(options)
|
||||
}.onFailure {
|
||||
|
|
|
|||
|
|
@ -43,8 +43,8 @@ class DefaultLoggedInImageLoaderFactory @Inject constructor(
|
|||
}
|
||||
add(AvatarDataKeyer())
|
||||
add(MediaRequestDataKeyer())
|
||||
add(AvatarDataFetcherFactory(context, matrixClient))
|
||||
add(MediaRequestDataFetcherFactory(context, matrixClient))
|
||||
add(AvatarDataFetcherFactory(matrixClient))
|
||||
add(MediaRequestDataFetcherFactory(matrixClient))
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,14 +7,12 @@
|
|||
|
||||
package io.element.android.libraries.matrix.ui.media
|
||||
|
||||
import android.content.Context
|
||||
import coil.ImageLoader
|
||||
import coil.fetch.Fetcher
|
||||
import coil.request.Options
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
|
||||
internal class MediaRequestDataFetcherFactory(
|
||||
private val context: Context,
|
||||
private val client: MatrixClient
|
||||
) : Fetcher.Factory<MediaRequestData> {
|
||||
override fun create(
|
||||
|
|
@ -23,7 +21,6 @@ internal class MediaRequestDataFetcherFactory(
|
|||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data,
|
||||
options = options
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue