From 1764a9c516aadd382fe9398af0ea4a68ce5bfecd Mon Sep 17 00:00:00 2001 From: Shubham Singh Date: Sun, 1 Oct 2023 04:22:28 +0530 Subject: [PATCH] Move imageview to common component --- .../features/details/ui/DetailsScreen.kt | 15 +-- .../splashy/ui/components/ComposeReusables.kt | 96 +++++++------------ 2 files changed, 35 insertions(+), 76 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/features/details/ui/DetailsScreen.kt b/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/features/details/ui/DetailsScreen.kt index a162ac3..e7d1fe4 100644 --- a/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/features/details/ui/DetailsScreen.kt +++ b/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/features/details/ui/DetailsScreen.kt @@ -91,6 +91,7 @@ import dev.ishubhamsingh.splashy.features.details.DetailsScreenModel import dev.ishubhamsingh.splashy.features.details.WallpaperScreenType import dev.ishubhamsingh.splashy.models.Photo import dev.ishubhamsingh.splashy.ui.components.BackButton +import dev.ishubhamsingh.splashy.ui.components.ImageViewComponent import dev.ishubhamsingh.splashy.ui.components.getKamelConfig import dev.ishubhamsingh.splashy.ui.components.parseColor import dev.ishubhamsingh.splashy.ui.theme.getLatoBold @@ -190,17 +191,7 @@ data class DetailsScreen(val id: String) : Screen { modifier = modifier.fillMaxSize().background(color = Color(parseColor(photo.color))), verticalArrangement = Arrangement.Center ) { - photo.urls?.regular?.let { - CompositionLocalProvider(LocalKamelConfig provides getKamelConfig(it)) { - KamelImage( - resource = asyncPainterResource(data = it), - contentDescription = photo.altDescription, - modifier = Modifier.fillMaxSize(), - contentScale = ContentScale.Crop, - animationSpec = tween() - ) - } - } + photo.urls?.regular?.let { ImageViewComponent(it, photo.altDescription) } } } @@ -251,7 +242,7 @@ data class DetailsScreen(val id: String) : Screen { ) { Row(verticalAlignment = Alignment.CenterVertically) { user?.profileImage?.let { profilePic -> - CompositionLocalProvider(LocalKamelConfig provides getKamelConfig(profilePic.medium)) { + CompositionLocalProvider(LocalKamelConfig provides getKamelConfig()) { KamelImage( modifier = Modifier.size(36.dp).clip(CircleShape), resource = asyncPainterResource(data = profilePic.medium), diff --git a/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/ui/components/ComposeReusables.kt b/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/ui/components/ComposeReusables.kt index bb0aace..487e9fd 100644 --- a/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/ui/components/ComposeReusables.kt +++ b/composeApp/src/commonMain/kotlin/dev/ishubhamsingh/splashy/ui/components/ComposeReusables.kt @@ -94,7 +94,6 @@ import dev.ishubhamsingh.splashy.core.presentation.CommonRes import dev.ishubhamsingh.splashy.features.categoriesPhotos.ui.CategoryType import dev.ishubhamsingh.splashy.models.Favourite import dev.ishubhamsingh.splashy.models.Photo -import io.kamel.core.config.DefaultCacheSize import io.kamel.core.config.KamelConfig import io.kamel.core.config.httpFetcher import io.kamel.core.config.takeFrom @@ -107,8 +106,8 @@ import io.ktor.client.plugins.HttpRequestRetry import io.ktor.client.plugins.defaultRequest import io.ktor.client.plugins.logging.LogLevel import io.ktor.client.plugins.logging.Logging +import io.ktor.client.request.header import io.ktor.http.CacheControl -import io.ktor.http.headersOf import io.ktor.http.isSuccess import kotlinx.coroutines.launch @@ -253,17 +252,7 @@ fun PhotoCardItem( val gradient = Brush.verticalGradient(colors = listOf(Color.Transparent, Color.Black.copy(alpha = 0.8f))) Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomStart) { - url?.let { - CompositionLocalProvider(LocalKamelConfig provides getKamelConfig(it)) { - KamelImage( - resource = asyncPainterResource(data = it), - contentDescription = altDescription, - modifier = Modifier.fillMaxSize(), - contentScale = ContentScale.Crop, - animationSpec = tween() - ) - } - } + url?.let { ImageViewComponent(it, altDescription) } Box(modifier.matchParentSize().background(gradient), contentAlignment = Alignment.Center) { Column( modifier = Modifier.padding(8.dp), @@ -295,17 +284,7 @@ fun PhotoCardItem( } } } else { - url?.let { - CompositionLocalProvider(LocalKamelConfig provides getKamelConfig(it)) { - KamelImage( - resource = asyncPainterResource(data = it), - contentDescription = altDescription, - modifier = Modifier.fillMaxSize(), - contentScale = ContentScale.Crop, - animationSpec = tween() - ) - } - } + url?.let { ImageViewComponent(it, altDescription) } } } } @@ -353,17 +332,7 @@ fun CategoriesCardItem( val gradient = Brush.verticalGradient(colors = listOf(Color.Transparent, Color.Black.copy(alpha = 0.8f))) Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.BottomStart) { - url?.let { - CompositionLocalProvider(LocalKamelConfig provides getKamelConfig(it)) { - KamelImage( - resource = asyncPainterResource(data = it), - contentDescription = altDescription, - modifier = Modifier.fillMaxSize(), - contentScale = ContentScale.Crop, - animationSpec = tween() - ) - } - } + url?.let { ImageViewComponent(it, altDescription) } Box(modifier.matchParentSize().background(gradient), contentAlignment = Alignment.Center) { Column( modifier = Modifier.padding(8.dp), @@ -395,47 +364,46 @@ fun CategoriesCardItem( } } } else { - url?.let { - CompositionLocalProvider(LocalKamelConfig provides getKamelConfig(it)) { - KamelImage( - resource = asyncPainterResource(data = it), - contentDescription = altDescription, - modifier = Modifier.fillMaxSize(), - contentScale = ContentScale.Crop, - animationSpec = tween() - ) - } - } + url?.let { ImageViewComponent(it, altDescription) } } } } @Composable -fun getKamelConfig(url: String): KamelConfig { - val kamelConfig = KamelConfig { - takeFrom(KamelConfig.Default) - imageBitmapCacheSize = DefaultCacheSize - imageBitmapDecoder() +fun getKamelConfig() = KamelConfig { + takeFrom(KamelConfig.Default) + imageBitmapCacheSize = 500 + imageBitmapDecoder() - httpFetcher { - defaultRequest { url(url) } + httpFetcher { + defaultRequest { header("Cache-Control", "max-age=31536000") } - CacheControl.MaxAge(maxAgeSeconds = 31536) - headersOf("Cache-Control", "max-age=31536000") + CacheControl.MaxAge(maxAgeSeconds = 31536) - install(HttpRequestRetry) { - maxRetries = 3 - retryIf { _, httpResponse -> !httpResponse.status.isSuccess() } - } + install(HttpRequestRetry) { + maxRetries = 3 + retryIf { _, httpResponse -> !httpResponse.status.isSuccess() } + } - install(Logging) { - logger = KtorLogger() - level = LogLevel.INFO - } + install(Logging) { + logger = KtorLogger() + level = LogLevel.INFO } } +} - return kamelConfig +@Composable +fun ImageViewComponent(url: String, altDescription: String?) { + CompositionLocalProvider(LocalKamelConfig provides getKamelConfig()) { + val painterResource = asyncPainterResource(data = url) + KamelImage( + resource = painterResource, + contentDescription = altDescription, + modifier = Modifier.fillMaxSize(), + contentScale = ContentScale.Crop, + animationSpec = tween() + ) + } } @Composable