Merge pull request #3366 from element-hq/feature/bma/fixAvatarLoading
Fix avatar sometimes not loading
This commit is contained in:
commit
0e5ecf2d07
5 changed files with 95 additions and 46 deletions
|
|
@ -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 io.element.android.libraries.matrix.api.media.MediaSource
|
||||||
import kotlin.math.roundToLong
|
import kotlin.math.roundToLong
|
||||||
|
|
||||||
fun AvatarData.toMediaRequestData(): MediaRequestData {
|
internal fun AvatarData.toMediaRequestData(): MediaRequestData {
|
||||||
return MediaRequestData(
|
return MediaRequestData(
|
||||||
source = url?.let { MediaSource(it) },
|
source = url?.let { MediaSource(it) },
|
||||||
kind = MediaRequestData.Kind.Thumbnail(size.dp.value.roundToLong())
|
kind = MediaRequestData.Kind.Thumbnail(size.dp.value.roundToLong())
|
||||||
|
|
|
||||||
|
|
@ -16,16 +16,12 @@
|
||||||
|
|
||||||
package io.element.android.libraries.matrix.ui.media
|
package io.element.android.libraries.matrix.ui.media
|
||||||
|
|
||||||
import android.content.Context
|
|
||||||
import coil.ImageLoader
|
|
||||||
import coil.decode.DataSource
|
import coil.decode.DataSource
|
||||||
import coil.decode.ImageSource
|
import coil.decode.ImageSource
|
||||||
import coil.fetch.FetchResult
|
import coil.fetch.FetchResult
|
||||||
import coil.fetch.Fetcher
|
import coil.fetch.Fetcher
|
||||||
import coil.fetch.SourceResult
|
import coil.fetch.SourceResult
|
||||||
import coil.request.Options
|
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.MatrixMediaLoader
|
||||||
import io.element.android.libraries.matrix.api.media.MediaSource
|
import io.element.android.libraries.matrix.api.media.MediaSource
|
||||||
import io.element.android.libraries.matrix.api.media.toFile
|
import io.element.android.libraries.matrix.api.media.toFile
|
||||||
|
|
@ -38,11 +34,14 @@ import kotlin.math.roundToLong
|
||||||
internal class CoilMediaFetcher(
|
internal class CoilMediaFetcher(
|
||||||
private val scalingFunction: (Float) -> Float,
|
private val scalingFunction: (Float) -> Float,
|
||||||
private val mediaLoader: MatrixMediaLoader,
|
private val mediaLoader: MatrixMediaLoader,
|
||||||
private val mediaData: MediaRequestData?,
|
private val mediaData: MediaRequestData,
|
||||||
private val options: Options
|
private val options: Options
|
||||||
) : Fetcher {
|
) : Fetcher {
|
||||||
override suspend fun fetch(): FetchResult? {
|
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) {
|
return when (mediaData.kind) {
|
||||||
is MediaRequestData.Kind.Content -> fetchContent(mediaData.source, options)
|
is MediaRequestData.Kind.Content -> fetchContent(mediaData.source, options)
|
||||||
is MediaRequestData.Kind.Thumbnail -> fetchThumbnail(mediaData.source, mediaData.kind, options)
|
is MediaRequestData.Kind.Thumbnail -> fetchThumbnail(mediaData.source, mediaData.kind, options)
|
||||||
|
|
@ -76,6 +75,8 @@ internal class CoilMediaFetcher(
|
||||||
source = mediaSource,
|
source = mediaSource,
|
||||||
).map { byteArray ->
|
).map { byteArray ->
|
||||||
byteArray.asSourceResult(options)
|
byteArray.asSourceResult(options)
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -86,6 +87,8 @@ internal class CoilMediaFetcher(
|
||||||
height = scalingFunction(kind.height.toFloat()).roundToLong(),
|
height = scalingFunction(kind.height.toFloat()).roundToLong(),
|
||||||
).map { byteArray ->
|
).map { byteArray ->
|
||||||
byteArray.asSourceResult(options)
|
byteArray.asSourceResult(options)
|
||||||
|
}.onFailure {
|
||||||
|
Timber.e(it)
|
||||||
}.getOrNull()
|
}.getOrNull()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -102,41 +105,4 @@ internal class CoilMediaFetcher(
|
||||||
dataSource = DataSource.MEMORY
|
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(AvatarDataKeyer())
|
||||||
add(MediaRequestDataKeyer())
|
add(MediaRequestDataKeyer())
|
||||||
add(CoilMediaFetcher.AvatarFactory(context, matrixClient))
|
add(AvatarDataFetcherFactory(context, matrixClient))
|
||||||
add(CoilMediaFetcher.MediaRequestDataFactory(context, matrixClient))
|
add(MediaRequestDataFetcherFactory(context, matrixClient))
|
||||||
}
|
}
|
||||||
.build()
|
.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
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue