From efe4345511d7aa6f43bbdc15115c4b76c04e988e Mon Sep 17 00:00:00 2001 From: Ali Albaali Date: Sun, 24 Jul 2022 20:29:09 +0300 Subject: [PATCH] Refactor lazyPainterResource and fix its bugs --- .../io/kamel/image/LazyPainterResource.kt | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/kamel-image/src/commonMain/kotlin/io/kamel/image/LazyPainterResource.kt b/kamel-image/src/commonMain/kotlin/io/kamel/image/LazyPainterResource.kt index 28e1b0fc..e05e939f 100644 --- a/kamel-image/src/commonMain/kotlin/io/kamel/image/LazyPainterResource.kt +++ b/kamel-image/src/commonMain/kotlin/io/kamel/image/LazyPainterResource.kt @@ -2,9 +2,11 @@ package io.kamel.image import androidx.compose.runtime.* import androidx.compose.ui.graphics.FilterQuality +import androidx.compose.ui.graphics.ImageBitmap import androidx.compose.ui.graphics.drawscope.DrawScope import androidx.compose.ui.graphics.painter.BitmapPainter import androidx.compose.ui.graphics.painter.Painter +import androidx.compose.ui.graphics.vector.ImageVector import androidx.compose.ui.graphics.vector.rememberVectorPainter import androidx.compose.ui.platform.LocalDensity import io.kamel.core.* @@ -12,8 +14,6 @@ import io.kamel.core.config.ResourceConfig import io.kamel.core.config.ResourceConfigBuilder import io.kamel.image.config.LocalKamelConfig import io.ktor.http.* -import kotlinx.coroutines.flow.map -import kotlinx.coroutines.withContext /** * Loads a [Painter] resource asynchronously. @@ -31,7 +31,6 @@ public inline fun lazyPainterResource( block: ResourceConfigBuilder.() -> Unit = {}, ): Resource { - var painterResource by remember(key) { mutableStateOf>(Resource.Loading(0F)) } val kamelConfig = LocalKamelConfig.current val density = LocalDensity.current val resourceConfig = remember(key, density) { @@ -41,25 +40,19 @@ public inline fun lazyPainterResource( .build() } - val painterFlow = when (data.toString().substringAfterLast(".")) { - "svg" -> kamelConfig.loadSvgResource(data, resourceConfig) - "xml" -> kamelConfig.loadImageVectorResource(data, resourceConfig).map { resource -> - resource.map { imageVector -> - rememberVectorPainter(imageVector) - } + val painterResource by remember(data, resourceConfig) { + when (data.toString().substringAfterLast(".")) { + "svg" -> kamelConfig.loadSvgResource(data, resourceConfig) + "xml" -> kamelConfig.loadImageVectorResource(data, resourceConfig) + else -> kamelConfig.loadImageBitmapResource(data, resourceConfig) } - else -> kamelConfig.loadImageBitmapResource(data, resourceConfig).map { resource -> - resource.map { imageBitmap -> - BitmapPainter(imageBitmap, filterQuality = filterQuality) - } - } - } + }.collectAsState(Resource.Loading(0F), resourceConfig.coroutineContext) - LaunchedEffect(key) { - withContext(resourceConfig.coroutineContext) { - painterFlow.collect { painterResource = it } + return painterResource.map { value -> + when (value) { + is ImageVector -> rememberVectorPainter(value) + is ImageBitmap -> remember(value) { BitmapPainter(value, filterQuality = filterQuality) } + else -> remember(value) { value as Painter } } } - - return painterResource } \ No newline at end of file