Skip to content

Commit

Permalink
Move imageview to common component
Browse files Browse the repository at this point in the history
  • Loading branch information
ishubhamsingh committed Sep 30, 2023
1 parent f704275 commit 1764a9c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) }
}
}

Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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) }
}
}
}
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 1764a9c

Please sign in to comment.