Passing the size of the robohash rendering to the request in the hopes to improve performance.

This commit is contained in:
Vitor Pamplona
2023-04-20 10:07:03 -04:00
parent 7ee3b8a196
commit 00e5470642
5 changed files with 44 additions and 15 deletions

View File

@@ -11,6 +11,7 @@ import coil.fetch.Fetcher
import coil.fetch.SourceResult import coil.fetch.SourceResult
import coil.request.ImageRequest import coil.request.ImageRequest
import coil.request.Options import coil.request.Options
import coil.size.Size
import okio.Buffer import okio.Buffer
import java.security.MessageDigest import java.security.MessageDigest
@@ -61,6 +62,7 @@ private fun svgString(msg: String): String {
class HashImageFetcher( class HashImageFetcher(
private val context: Context, private val context: Context,
private val size: Size,
private val data: Uri private val data: Uri
) : Fetcher { ) : Fetcher {
@@ -79,16 +81,17 @@ class HashImageFetcher(
object Factory : Fetcher.Factory<Uri> { object Factory : Fetcher.Factory<Uri> {
override fun create(data: Uri, options: Options, imageLoader: ImageLoader): Fetcher { override fun create(data: Uri, options: Options, imageLoader: ImageLoader): Fetcher {
return HashImageFetcher(options.context, data) return HashImageFetcher(options.context, options.size, data)
} }
} }
} }
object Robohash { object Robohash {
fun imageRequest(context: Context, message: String): ImageRequest { fun imageRequest(context: Context, message: String, robotSize: Size): ImageRequest {
return ImageRequest return ImageRequest
.Builder(context) .Builder(context)
.data("robohash:$message") .data("robohash:$message")
.fetcherFactory(HashImageFetcher.Factory) .fetcherFactory(HashImageFetcher.Factory)
.size(robotSize)
.crossfade(100) .crossfade(100)
.build() .build()
} }

View File

@@ -9,13 +9,17 @@ import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Dp
import coil.compose.AsyncImage import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter import coil.compose.rememberAsyncImagePainter
import coil.size.Size
@Composable @Composable
fun RobohashAsyncImage( fun RobohashAsyncImage(
robot: String, robot: String,
robotSize: Dp,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
contentDescription: String? = null, contentDescription: String? = null,
transform: (AsyncImagePainter.State) -> AsyncImagePainter.State = AsyncImagePainter.DefaultTransform, transform: (AsyncImagePainter.State) -> AsyncImagePainter.State = AsyncImagePainter.DefaultTransform,
@@ -26,23 +30,30 @@ fun RobohashAsyncImage(
colorFilter: ColorFilter? = null, colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality filterQuality: FilterQuality = DrawScope.DefaultFilterQuality
) { ) {
AsyncImage( with(LocalDensity.current) {
model = Robohash.imageRequest(LocalContext.current, robot), AsyncImage(
contentDescription = contentDescription, model = Robohash.imageRequest(
modifier = modifier, LocalContext.current,
transform = transform, robot,
onState = onState, Size(robotSize.roundToPx(), robotSize.roundToPx())
alignment = alignment, ),
contentScale = contentScale, contentDescription = contentDescription,
alpha = alpha, modifier = modifier,
colorFilter = colorFilter, transform = transform,
filterQuality = filterQuality onState = onState,
) alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter,
filterQuality = filterQuality
)
}
} }
@Composable @Composable
fun RobohashFallbackAsyncImage( fun RobohashFallbackAsyncImage(
robot: String, robot: String,
robotSize: Dp,
model: String?, model: String?,
contentDescription: String?, contentDescription: String?,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
@@ -53,7 +64,15 @@ fun RobohashFallbackAsyncImage(
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality filterQuality: FilterQuality = DrawScope.DefaultFilterQuality
) { ) {
val context = LocalContext.current val context = LocalContext.current
val painter = rememberAsyncImagePainter(model = Robohash.imageRequest(context, robot)) val painter = with(LocalDensity.current) {
rememberAsyncImagePainter(
model = Robohash.imageRequest(
context,
robot,
Size(robotSize.roundToPx(), robotSize.roundToPx())
)
)
}
AsyncImage( AsyncImage(
model = model, model = model,
@@ -85,6 +104,7 @@ fun RobohashAsyncImageProxy(
if (model.url == null) { if (model.url == null) {
RobohashAsyncImage( RobohashAsyncImage(
robot = robot, robot = robot,
robotSize = model.size,
contentDescription = contentDescription, contentDescription = contentDescription,
modifier = modifier, modifier = modifier,
alignment = alignment, alignment = alignment,
@@ -96,6 +116,7 @@ fun RobohashAsyncImageProxy(
} else { } else {
RobohashFallbackAsyncImage( RobohashFallbackAsyncImage(
robot = robot, robot = robot,
robotSize = model.size,
model = model.proxyUrl(), model = model.proxyUrl(),
contentDescription = contentDescription, contentDescription = contentDescription,
modifier = modifier, modifier = modifier,

View File

@@ -373,6 +373,7 @@ private fun RelayBadges(baseNote: Note) {
) { ) {
RobohashFallbackAsyncImage( RobohashFallbackAsyncImage(
robot = "https://$url/favicon.ico", robot = "https://$url/favicon.ico",
robotSize = 15.dp,
model = "https://$url/favicon.ico", model = "https://$url/favicon.ico",
contentDescription = stringResource(id = R.string.relay_icon), contentDescription = stringResource(id = R.string.relay_icon),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }), colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }),

View File

@@ -898,6 +898,7 @@ private fun RelayBadges(baseNote: Note) {
) { ) {
RobohashFallbackAsyncImage( RobohashFallbackAsyncImage(
robot = "https://$url/favicon.ico", robot = "https://$url/favicon.ico",
robotSize = 15.dp,
model = "https://$url/favicon.ico", model = "https://$url/favicon.ico",
contentDescription = stringResource(R.string.relay_icon), contentDescription = stringResource(R.string.relay_icon),
colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }), colorFilter = ColorFilter.colorMatrix(ColorMatrix().apply { setToSaturation(0f) }),
@@ -970,6 +971,7 @@ fun NoteAuthorPicture(
if (author == null) { if (author == null) {
RobohashAsyncImage( RobohashAsyncImage(
robot = "authornotfound", robot = "authornotfound",
robotSize = size,
contentDescription = stringResource(R.string.unknown_author), contentDescription = stringResource(R.string.unknown_author),
modifier = modifier modifier = modifier
.width(size) .width(size)

View File

@@ -655,6 +655,7 @@ fun BadgeThumb(
if (image == null) { if (image == null) {
RobohashAsyncImage( RobohashAsyncImage(
robot = "authornotfound", robot = "authornotfound",
robotSize = size,
contentDescription = stringResource(R.string.unknown_author), contentDescription = stringResource(R.string.unknown_author),
modifier = pictureModifier modifier = pictureModifier
.width(size) .width(size)
@@ -664,6 +665,7 @@ fun BadgeThumb(
} else { } else {
RobohashFallbackAsyncImage( RobohashFallbackAsyncImage(
robot = note.idHex, robot = note.idHex,
robotSize = size,
model = image, model = image,
contentDescription = stringResource(id = R.string.profile_image), contentDescription = stringResource(id = R.string.profile_image),
modifier = pictureModifier modifier = pictureModifier