From c8d68be0e125a8c86ef418d6fda382b7b95f6861 Mon Sep 17 00:00:00 2001 From: skydoves Date: Tue, 25 Jun 2024 21:13:45 +0900 Subject: [PATCH] Fix the scale type was not properly applied to the FrescoWebImage --- fresco-websupport/api/fresco-websupport.api | 2 +- .../fresco/websupport/FrescoWebImage.kt | 37 +++++++++++++------ gradle/libs.versions.toml | 4 +- 3 files changed, 29 insertions(+), 14 deletions(-) diff --git a/fresco-websupport/api/fresco-websupport.api b/fresco-websupport/api/fresco-websupport.api index 435f08e3..c3fbda64 100644 --- a/fresco-websupport/api/fresco-websupport.api +++ b/fresco-websupport/api/fresco-websupport.api @@ -1,4 +1,4 @@ public final class com/skydoves/landscapist/fresco/websupport/FrescoWebImage { - public static final fun FrescoWebImage (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Ljava/lang/String;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;FLandroidx/compose/ui/graphics/ColorFilter;ILandroidx/compose/runtime/Composer;II)V + public static final fun FrescoWebImage (Lkotlin/jvm/functions/Function0;Landroidx/compose/ui/Modifier;Ljava/lang/String;Landroidx/compose/ui/Alignment;Landroidx/compose/ui/layout/ContentScale;FLandroidx/compose/ui/graphics/ColorFilter;Landroidx/compose/ui/graphics/painter/Painter;Landroidx/compose/runtime/Composer;II)V } diff --git a/fresco-websupport/src/main/kotlin/com/skydoves/landscapist/fresco/websupport/FrescoWebImage.kt b/fresco-websupport/src/main/kotlin/com/skydoves/landscapist/fresco/websupport/FrescoWebImage.kt index 5a76d6c3..75bc874b 100644 --- a/fresco-websupport/src/main/kotlin/com/skydoves/landscapist/fresco/websupport/FrescoWebImage.kt +++ b/fresco-websupport/src/main/kotlin/com/skydoves/landscapist/fresco/websupport/FrescoWebImage.kt @@ -19,23 +19,24 @@ package com.skydoves.landscapist.fresco.websupport -import androidx.annotation.DrawableRes import androidx.compose.foundation.Image -import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.ColorFilter import androidx.compose.ui.graphics.DefaultAlpha import androidx.compose.ui.graphics.ImageBitmap +import androidx.compose.ui.graphics.painter.Painter import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalInspectionMode -import androidx.compose.ui.res.painterResource import com.facebook.drawee.backends.pipeline.PipelineDraweeControllerBuilder +import com.facebook.drawee.drawable.ScalingUtils import com.facebook.drawee.generic.GenericDraweeHierarchy import com.facebook.drawee.generic.GenericDraweeHierarchyInflater import com.facebook.drawee.view.DraweeHolder +import com.skydoves.landscapist.constraints.constraint import com.skydoves.landscapist.rememberDrawablePainter /** @@ -87,15 +88,15 @@ public fun FrescoWebImage( modifier: Modifier = Modifier, contentDescription: String? = null, alignment: Alignment = Alignment.Center, - contentScale: ContentScale = ContentScale.Fit, + contentScale: ContentScale = ContentScale.Crop, alpha: Float = DefaultAlpha, colorFilter: ColorFilter? = null, - @DrawableRes previewPlaceholder: Int = 0, + previewPlaceholder: Painter? = null, ) { - if (LocalInspectionMode.current && previewPlaceholder != 0) { + if (LocalInspectionMode.current && previewPlaceholder != null) { Image( modifier = modifier, - painter = painterResource(id = previewPlaceholder), + painter = previewPlaceholder, alignment = alignment, contentScale = contentScale, alpha = alpha, @@ -106,16 +107,18 @@ public fun FrescoWebImage( } val context = LocalContext.current + val builder = controllerBuilder() val hierarchy = GenericDraweeHierarchyInflater.inflateBuilder(context, null).build() val holder: DraweeHolder = DraweeHolder.create(hierarchy, context) - controllerBuilder().oldController = holder.controller - holder.controller = controllerBuilder().build() + builder.oldController = holder.controller.apply { this?.contentDescription = contentDescription } + holder.hierarchy.setActualImageScaleType(getScaleType(contentScale)) + holder.controller = builder.build() val topLevelDrawable = holder.topLevelDrawable - Box(modifier = modifier) { + BoxWithConstraints(modifier = modifier) { if (topLevelDrawable != null) { Image( - modifier = modifier, + modifier = Modifier.constraint(this), painter = rememberDrawablePainter(topLevelDrawable), contentDescription = contentDescription, alignment = alignment, @@ -126,3 +129,15 @@ public fun FrescoWebImage( } } } + +private fun getScaleType(contentScale: ContentScale): ScalingUtils.ScaleType { + return when (contentScale) { + ContentScale.Crop -> ScalingUtils.ScaleType.CENTER_CROP + ContentScale.FillWidth -> ScalingUtils.ScaleType.FIT_X + ContentScale.FillHeight -> ScalingUtils.ScaleType.FIT_Y + ContentScale.FillBounds -> ScalingUtils.ScaleType.FIT_XY + ContentScale.Fit -> ScalingUtils.ScaleType.FIT_CENTER + ContentScale.Inside -> ScalingUtils.ScaleType.CENTER_INSIDE + else -> ScalingUtils.ScaleType.CENTER_CROP + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index e2e3a4d0..61838da3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -2,10 +2,10 @@ kotlin = "2.0.0" dokka = "1.9.20" jvmTarget = "11" -nexusPlugin = "0.26.0" +nexusPlugin = "0.29.0" kotlinxCoroutines = "1.8.1" kotlinBinaryCompatibility = "0.14.0" -androidGradlePlugin = "8.5.0" +androidGradlePlugin = "8.4.2" androidxActivity = "1.9.0" androidxMaterial = "1.12.0" androidxComposeBom = "2024.06.00"