Skip to content

Commit

Permalink
Paywalls: fix backwards-compatible blurring of default paywall back…
Browse files Browse the repository at this point in the history
…ground (#1380)
  • Loading branch information
NachoSoto authored and tonidero committed Oct 31, 2023
1 parent da84ed8 commit 2ed2017
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.revenuecat.purchases.ui.revenuecatui.composables

import BlurTransformation
import android.os.Build
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
Expand All @@ -11,7 +10,6 @@ import androidx.compose.ui.draw.blur
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.revenuecat.purchases.paywalls.PaywallData
Expand Down Expand Up @@ -43,23 +41,21 @@ internal fun BoxScope.PaywallBackground(templateConfiguration: TemplateConfigura
}

if (templateConfiguration.configuration.images.background == PaywallData.defaultBackgroundPlaceholder) {
Image(
LocalImage(
resource = R.drawable.default_background,
modifier = modifier,
painter = painterResource(id = R.drawable.default_background),
contentDescription = null,
contentScale = BackgroundUIConstants.contentScale,
transformation = backwardsCompatibleTransformation,
alpha = imageAlpha,
)
} else if (templateConfiguration.images.backgroundUri != null) {
RemoteImage(
urlString = templateConfiguration.images.backgroundUri.toString(),
modifier = modifier,
contentScale = BackgroundUIConstants.contentScale,
transformation = backwardsCompatibleTransformation,
alpha = imageAlpha,
)
} else {
templateConfiguration.images.backgroundUri?.let {
RemoteImage(
urlString = it.toString(),
modifier = modifier,
contentScale = BackgroundUIConstants.contentScale,
transformation = backwardsCompatibleTransformation,
alpha = imageAlpha,
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.revenuecat.purchases.ui.revenuecatui.composables

import android.content.Context
import androidx.annotation.DrawableRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.Modifier
Expand All @@ -15,6 +16,26 @@ import coil.transform.Transformation
import com.revenuecat.purchases.ui.revenuecatui.UIConstant
import com.revenuecat.purchases.ui.revenuecatui.helpers.Logger

@SuppressWarnings("LongParameterList")
@Composable
internal fun LocalImage(
@DrawableRes resource: Int,
modifier: Modifier = Modifier,
contentScale: ContentScale = ContentScale.Fit,
contentDescription: String? = null,
transformation: Transformation? = null,
alpha: Float = 1f,
) {
Image(
source = ImageSource.Local(resource),
modifier = modifier,
contentScale = contentScale,
contentDescription = contentDescription,
transformation = transformation,
alpha = alpha,
)
}

@SuppressWarnings("LongParameterList")
@Composable
internal fun RemoteImage(
Expand All @@ -24,10 +45,41 @@ internal fun RemoteImage(
contentDescription: String? = null,
transformation: Transformation? = null,
alpha: Float = 1f,
) {
Image(
source = ImageSource.Remote(urlString),
modifier = modifier,
contentScale = contentScale,
contentDescription = contentDescription,
transformation = transformation,
alpha = alpha,
)
}

private sealed class ImageSource {
data class Local(@DrawableRes val resource: Int) : ImageSource() {
override val data: Any = resource
}
data class Remote(val urlString: String) : ImageSource() {
override val data: Any = urlString
}

abstract val data: Any
}

@SuppressWarnings("LongParameterList")
@Composable
private fun Image(
source: ImageSource,
modifier: Modifier = Modifier,
contentScale: ContentScale,
contentDescription: String?,
transformation: Transformation?,
alpha: Float,
) {
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(urlString)
.data(source.data)
.crossfade(durationMillis = UIConstant.defaultAnimationDurationMillis)
.transformations(listOfNotNull(transformation))
.build(),
Expand All @@ -39,7 +91,12 @@ internal fun RemoteImage(
onState = {
when (it) {
is AsyncImagePainter.State.Error -> {
Logger.e("Error loading image from '$urlString': ${it.result}")
val error = when (source) {
is ImageSource.Local -> "Error loading local image: '${source.resource}'"
is ImageSource.Remote -> "Error loading image from '${source.urlString}'"
}

Logger.e("$error: ${it.result}")
}
else -> {}
}
Expand Down

0 comments on commit 2ed2017

Please sign in to comment.