Switches Robohash to Precompiled SVGs in order to reduce the memory burned of creating Strings with SVGs on the fly.

This commit is contained in:
Vitor Pamplona
2024-03-08 18:41:07 -05:00
parent e6da340879
commit 632bd77db3
59 changed files with 7910 additions and 177 deletions
@@ -73,6 +73,7 @@ class HashImageFetcher(
}
}
@Deprecated("Use the RobohashAssembler instead")
object RobohashImageRequest {
fun build(
context: Context,
@@ -36,12 +36,12 @@ import androidx.compose.ui.graphics.ColorFilter
import androidx.compose.ui.graphics.DefaultAlpha
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.drawscope.DrawScope
import androidx.compose.ui.graphics.vector.rememberVectorPainter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.core.graphics.drawable.toDrawable
import coil.ImageLoader
import coil.compose.AsyncImage
import coil.compose.AsyncImagePainter
import coil.compose.rememberAsyncImagePainter
import coil.decode.DataSource
import coil.fetch.DrawableResult
@@ -49,6 +49,7 @@ import coil.fetch.FetchResult
import coil.fetch.Fetcher
import coil.request.ImageRequest
import coil.request.Options
import com.vitorpamplona.amethyst.commons.CachedRobohash
import com.vitorpamplona.amethyst.service.checkNotInMainThread
import com.vitorpamplona.amethyst.ui.theme.isLight
import java.util.Base64
@@ -58,31 +59,18 @@ fun RobohashAsyncImage(
robot: String,
modifier: Modifier = Modifier,
contentDescription: String? = null,
transform: (AsyncImagePainter.State) -> AsyncImagePainter.State =
AsyncImagePainter.DefaultTransform,
onState: ((AsyncImagePainter.State) -> Unit)? = null,
alignment: Alignment = Alignment.Center,
contentScale: ContentScale = ContentScale.Fit,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null,
filterQuality: FilterQuality = DrawScope.DefaultFilterQuality,
) {
val context = LocalContext.current
val isLightTheme = MaterialTheme.colorScheme.isLight
val imageRequest = remember(robot) { RobohashImageRequest.build(context, robot, isLightTheme) }
val robotPainter =
remember(robot) {
CachedRobohash.get(robot, isLightTheme)
}
AsyncImage(
model = imageRequest,
Image(
imageVector = robotPainter,
contentDescription = contentDescription,
modifier = modifier,
transform = transform,
onState = onState,
alignment = alignment,
contentScale = contentScale,
alpha = alpha,
colorFilter = colorFilter,
filterQuality = filterQuality,
)
}
@@ -101,10 +89,6 @@ fun RobohashFallbackAsyncImage(
) {
val context = LocalContext.current
val isLightTheme = MaterialTheme.colorScheme.isLight
val painter =
rememberAsyncImagePainter(
model = RobohashImageRequest.build(context, robot, isLightTheme),
)
if (model != null && loadProfilePicture) {
val isBase64 by remember { derivedStateOf { model.startsWith("data:image/jpeg;base64,") } }
@@ -124,6 +108,11 @@ fun RobohashFallbackAsyncImage(
colorFilter = colorFilter,
)
} else {
val painter =
rememberVectorPainter(
image = CachedRobohash.get(robot, isLightTheme),
)
AsyncImage(
model = model,
contentDescription = contentDescription,
@@ -139,8 +128,10 @@ fun RobohashFallbackAsyncImage(
)
}
} else {
val robotPainter = CachedRobohash.get(robot, isLightTheme)
Image(
painter = painter,
imageVector = robotPainter,
contentDescription = contentDescription,
modifier = modifier,
alignment = alignment,