-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Paywalls V2] Adds
ImageComponentView
(#1959)
### Description This adds a new component `ImageComponentView` and the corresponding `ImageComponentStyle`. It doesn't include transformations from `ImageComponent` to `ImageComponentStyle`. - Placeholder to image loading: https://github.com/user-attachments/assets/ae010171-2ae4-4d82-b84c-68fa9015d2bd - Previews <img width="643" alt="image" src="https://github.com/user-attachments/assets/0483020f-c3ad-400e-982d-1d33c0bef517">
- Loading branch information
Showing
6 changed files
with
260 additions
and
13 deletions.
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
...in/kotlin/com/revenuecat/purchases/ui/revenuecatui/components/image/ImageComponentView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
@file:JvmSynthetic | ||
|
||
package com.revenuecat.purchases.ui.revenuecatui.components.image | ||
|
||
import android.graphics.Color | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.shape.RoundedCornerShape | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Brush | ||
import androidx.compose.ui.graphics.RectangleShape | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import com.revenuecat.purchases.paywalls.components.properties.ImageUrls | ||
import com.revenuecat.purchases.paywalls.components.properties.Size | ||
import com.revenuecat.purchases.paywalls.components.properties.SizeConstraint | ||
import com.revenuecat.purchases.paywalls.components.properties.ThemeImageUrls | ||
import com.revenuecat.purchases.ui.revenuecatui.components.ktx.urlsForCurrentTheme | ||
import com.revenuecat.purchases.ui.revenuecatui.components.modifier.overlay | ||
import com.revenuecat.purchases.ui.revenuecatui.components.modifier.size | ||
import com.revenuecat.purchases.ui.revenuecatui.components.properties.ColorStyle | ||
import com.revenuecat.purchases.ui.revenuecatui.components.style.ImageComponentStyle | ||
import com.revenuecat.purchases.ui.revenuecatui.composables.RemoteImage | ||
import com.revenuecat.purchases.ui.revenuecatui.extensions.applyIfNotNull | ||
import java.net.URL | ||
import androidx.compose.ui.graphics.Color as ComposeColor | ||
|
||
@JvmSynthetic | ||
@Composable | ||
internal fun ImageComponentView( | ||
style: ImageComponentStyle, | ||
modifier: Modifier = Modifier, | ||
) { | ||
if (style.visible) { | ||
val currentUrls = style.themeImageUrls.urlsForCurrentTheme | ||
RemoteImage( | ||
urlString = currentUrls.webp.toString(), | ||
modifier = modifier | ||
.size(style.adjustedSize()) | ||
.applyIfNotNull(style.overlay) { overlay(it, style.shape ?: RectangleShape) } | ||
.applyIfNotNull(style.shape) { clip(it) }, | ||
placeholderUrlString = currentUrls.webpLowRes.toString(), | ||
contentScale = style.contentScale, | ||
) | ||
} | ||
} | ||
|
||
@Preview | ||
@Composable | ||
private fun ImageComponentView_Preview_Default() { | ||
Box(modifier = Modifier.background(ComposeColor.Red)) { | ||
ImageComponentView( | ||
style = previewImageComponentStyle(), | ||
) | ||
} | ||
} | ||
|
||
@Suppress("MagicNumber") | ||
@Preview | ||
@Composable | ||
private fun ImageComponentView_Preview_LinearGradient() { | ||
Box(modifier = Modifier.background(ComposeColor.Red)) { | ||
ImageComponentView( | ||
style = previewImageComponentStyle( | ||
overlay = ColorStyle.Gradient( | ||
Brush.verticalGradient( | ||
Pair(0f, ComposeColor(Color.parseColor("#88FF0000"))), | ||
Pair(0.5f, ComposeColor(Color.parseColor("#8800FF00"))), | ||
Pair(1f, ComposeColor(Color.parseColor("#880000FF"))), | ||
), | ||
), | ||
), | ||
) | ||
} | ||
} | ||
|
||
@Suppress("MagicNumber") | ||
@Preview | ||
@Composable | ||
private fun ImageComponentView_Preview_RadialGradient() { | ||
Box(modifier = Modifier.background(ComposeColor.Red)) { | ||
ImageComponentView( | ||
style = previewImageComponentStyle( | ||
overlay = ColorStyle.Gradient( | ||
Brush.radialGradient( | ||
Pair(0f, ComposeColor(Color.parseColor("#88FF0000"))), | ||
Pair(0.5f, ComposeColor(Color.parseColor("#8800FF00"))), | ||
Pair(1f, ComposeColor(Color.parseColor("#880000FF"))), | ||
), | ||
), | ||
), | ||
) | ||
} | ||
} | ||
|
||
@Suppress("LongParameterList") | ||
@Composable | ||
private fun previewImageComponentStyle( | ||
url: URL = URL("https://sample-videos.com/img/Sample-jpg-image-5mb.jpg"), | ||
lowResURL: URL = URL("https://assets.pawwalls.com/954459_1701163461.jpg"), | ||
visible: Boolean = true, | ||
size: Size = Size(width = SizeConstraint.Fixed(400u), height = SizeConstraint.Fit), | ||
contentScale: ContentScale = ContentScale.Crop, | ||
overlay: ColorStyle? = null, | ||
) = ImageComponentStyle( | ||
visible = visible, | ||
themeImageUrls = ThemeImageUrls(light = ImageUrls(url, url, lowResURL, 1000u, 1000u)), | ||
size = size, | ||
shape = RoundedCornerShape(20.dp, 20.dp, 20.dp, 20.dp), | ||
overlay = overlay, | ||
contentScale = contentScale, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
...i/src/main/kotlin/com/revenuecat/purchases/ui/revenuecatui/components/modifier/Overlay.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@file:JvmSynthetic | ||
|
||
package com.revenuecat.purchases.ui.revenuecatui.components.modifier | ||
|
||
import androidx.compose.runtime.Stable | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.drawWithCache | ||
import androidx.compose.ui.graphics.RectangleShape | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.graphics.drawOutline | ||
import com.revenuecat.purchases.ui.revenuecatui.components.properties.ColorStyle | ||
|
||
@JvmSynthetic | ||
@Stable | ||
internal fun Modifier.overlay( | ||
color: ColorStyle, | ||
shape: Shape = RectangleShape, | ||
): Modifier = this then drawWithCache { | ||
val outline = shape.createOutline(size, layoutDirection, this) | ||
|
||
onDrawWithContent { | ||
drawContent() | ||
when (color) { | ||
is ColorStyle.Solid -> drawOutline(outline, color.color) | ||
is ColorStyle.Gradient -> drawOutline(outline, color.brush) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
...n/kotlin/com/revenuecat/purchases/ui/revenuecatui/components/style/ImageComponentStyle.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package com.revenuecat.purchases.ui.revenuecatui.components.style | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.Immutable | ||
import androidx.compose.runtime.ReadOnlyComposable | ||
import androidx.compose.ui.graphics.Shape | ||
import androidx.compose.ui.layout.ContentScale | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.unit.Density | ||
import com.revenuecat.purchases.paywalls.components.properties.ImageUrls | ||
import com.revenuecat.purchases.paywalls.components.properties.Size | ||
import com.revenuecat.purchases.paywalls.components.properties.SizeConstraint.Fill | ||
import com.revenuecat.purchases.paywalls.components.properties.SizeConstraint.Fit | ||
import com.revenuecat.purchases.paywalls.components.properties.SizeConstraint.Fixed | ||
import com.revenuecat.purchases.paywalls.components.properties.ThemeImageUrls | ||
import com.revenuecat.purchases.ui.revenuecatui.components.ktx.urlsForCurrentTheme | ||
import com.revenuecat.purchases.ui.revenuecatui.components.properties.ColorStyle | ||
|
||
@Suppress("LongParameterList") | ||
@Immutable | ||
internal class ImageComponentStyle private constructor( | ||
@get:JvmSynthetic | ||
val visible: Boolean, | ||
@get:JvmSynthetic | ||
val themeImageUrls: ThemeImageUrls, | ||
@get:JvmSynthetic | ||
val size: Size, | ||
@get:JvmSynthetic | ||
val shape: Shape?, | ||
@get:JvmSynthetic | ||
val overlay: ColorStyle?, | ||
@get:JvmSynthetic | ||
val contentScale: ContentScale, | ||
) : ComponentStyle { | ||
|
||
companion object { | ||
|
||
@Suppress("LongParameterList") | ||
@JvmSynthetic | ||
@Composable | ||
operator fun invoke( | ||
visible: Boolean, | ||
size: Size, | ||
themeImageUrls: ThemeImageUrls, | ||
shape: Shape?, | ||
overlay: ColorStyle?, | ||
contentScale: ContentScale, | ||
): ImageComponentStyle { | ||
return ImageComponentStyle( | ||
visible = visible, | ||
size = size, | ||
themeImageUrls = themeImageUrls, | ||
shape = shape, | ||
overlay = overlay, | ||
contentScale = contentScale, | ||
) | ||
} | ||
} | ||
|
||
@JvmSynthetic | ||
@ReadOnlyComposable | ||
@Composable | ||
fun adjustedSize(): Size { | ||
val density = LocalDensity.current | ||
return size.adjustForImage(imageUrls = themeImageUrls.urlsForCurrentTheme, density = density) | ||
} | ||
} | ||
|
||
private fun Size.adjustForImage(imageUrls: ImageUrls, density: Density): Size = | ||
Size( | ||
width = when (width) { | ||
is Fit -> Fixed(with(density) { imageUrls.width.toInt().toDp().value.toUInt() }) | ||
is Fill, | ||
is Fixed, | ||
-> width | ||
}, | ||
height = when (height) { | ||
is Fit -> Fixed(with(density) { imageUrls.height.toInt().toDp().value.toUInt() }) | ||
is Fill, | ||
is Fixed, | ||
-> height | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters