Merge pull request #3366 from element-hq/feature/bma/fixAvatarLoading
Fix avatar sometimes not loading
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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.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(
|
||||
data: AvatarData,
|
||||
options: Options,
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data.toMediaRequestData(),
|
||||
options = options
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -20,7 +20,7 @@ import io.element.android.libraries.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.libraries.matrix.api.media.MediaSource
|
||||
import kotlin.math.roundToLong
|
||||
|
||||
fun AvatarData.toMediaRequestData(): MediaRequestData {
|
||||
internal fun AvatarData.toMediaRequestData(): MediaRequestData {
|
||||
return MediaRequestData(
|
||||
source = url?.let { MediaSource(it) },
|
||||
kind = MediaRequestData.Kind.Thumbnail(size.dp.value.roundToLong())
|
||||
|
||||
@@ -16,16 +16,12 @@
|
||||
|
||||
package io.element.android.libraries.matrix.ui.media
|
||||
|
||||
import android.content.Context
|
||||
import coil.ImageLoader
|
||||
import coil.decode.DataSource
|
||||
import coil.decode.ImageSource
|
||||
import coil.fetch.FetchResult
|
||||
import coil.fetch.Fetcher
|
||||
import coil.fetch.SourceResult
|
||||
import coil.request.Options
|
||||
import io.element.android.libraries.designsystem.components.avatar.AvatarData
|
||||
import io.element.android.libraries.matrix.api.MatrixClient
|
||||
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
|
||||
import io.element.android.libraries.matrix.api.media.MediaSource
|
||||
import io.element.android.libraries.matrix.api.media.toFile
|
||||
@@ -38,11 +34,14 @@ import kotlin.math.roundToLong
|
||||
internal class CoilMediaFetcher(
|
||||
private val scalingFunction: (Float) -> Float,
|
||||
private val mediaLoader: MatrixMediaLoader,
|
||||
private val mediaData: MediaRequestData?,
|
||||
private val mediaData: MediaRequestData,
|
||||
private val options: Options
|
||||
) : Fetcher {
|
||||
override suspend fun fetch(): FetchResult? {
|
||||
if (mediaData?.source == null) return null
|
||||
if (mediaData.source == null) {
|
||||
Timber.e("MediaData source is null")
|
||||
return null
|
||||
}
|
||||
return when (mediaData.kind) {
|
||||
is MediaRequestData.Kind.Content -> fetchContent(mediaData.source, options)
|
||||
is MediaRequestData.Kind.Thumbnail -> fetchThumbnail(mediaData.source, mediaData.kind, options)
|
||||
@@ -76,6 +75,8 @@ internal class CoilMediaFetcher(
|
||||
source = mediaSource,
|
||||
).map { byteArray ->
|
||||
byteArray.asSourceResult(options)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
@@ -86,6 +87,8 @@ internal class CoilMediaFetcher(
|
||||
height = scalingFunction(kind.height.toFloat()).roundToLong(),
|
||||
).map { byteArray ->
|
||||
byteArray.asSourceResult(options)
|
||||
}.onFailure {
|
||||
Timber.e(it)
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
@@ -102,41 +105,4 @@ internal class CoilMediaFetcher(
|
||||
dataSource = DataSource.MEMORY
|
||||
)
|
||||
}
|
||||
|
||||
class MediaRequestDataFactory(
|
||||
private val context: Context,
|
||||
private val client: MatrixClient
|
||||
) :
|
||||
Fetcher.Factory<MediaRequestData> {
|
||||
override fun create(
|
||||
data: MediaRequestData,
|
||||
options: Options,
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data,
|
||||
options = options
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
class AvatarFactory(
|
||||
private val context: Context,
|
||||
private val client: MatrixClient
|
||||
) : Fetcher.Factory<AvatarData> {
|
||||
override fun create(
|
||||
data: AvatarData,
|
||||
options: Options,
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data.toMediaRequestData(),
|
||||
options = options
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -52,8 +52,8 @@ class DefaultLoggedInImageLoaderFactory @Inject constructor(
|
||||
}
|
||||
add(AvatarDataKeyer())
|
||||
add(MediaRequestDataKeyer())
|
||||
add(CoilMediaFetcher.AvatarFactory(context, matrixClient))
|
||||
add(CoilMediaFetcher.MediaRequestDataFactory(context, matrixClient))
|
||||
add(AvatarDataFetcherFactory(context, matrixClient))
|
||||
add(MediaRequestDataFetcherFactory(context, matrixClient))
|
||||
}
|
||||
.build()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright (c) 2024 New Vector Ltd
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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(
|
||||
data: MediaRequestData,
|
||||
options: Options,
|
||||
imageLoader: ImageLoader
|
||||
): Fetcher {
|
||||
return CoilMediaFetcher(
|
||||
scalingFunction = { context.resources.displayMetrics.density * it },
|
||||
mediaLoader = client.mediaLoader,
|
||||
mediaData = data,
|
||||
options = options
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user