Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.revenuecat.purchases.paywalls.components

import androidx.compose.runtime.Immutable
import com.revenuecat.purchases.InternalRevenueCatAPI
import dev.drewhamilton.poko.Poko
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@InternalRevenueCatAPI
@Poko
@Serializable
@SerialName("header")
@Immutable
public class HeaderComponent(
@get:JvmSynthetic
public val stack: StackComponent,
) : PaywallComponent
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ internal class PaywallComponentSerializer : KSerializer<PaywallComponent> {
"package" -> jsonDecoder.json.decodeFromString<PackageComponent>(json.toString())
"purchase_button" -> jsonDecoder.json.decodeFromString<PurchaseButtonComponent>(json.toString())
"stack" -> jsonDecoder.json.decodeFromString<StackComponent>(json.toString())
"header" -> jsonDecoder.json.decodeFromString<HeaderComponent>(json.toString())
"sticky_footer" -> jsonDecoder.json.decodeFromString<StickyFooterComponent>(json.toString())
"text" -> jsonDecoder.json.decodeFromString<TextComponent>(json.toString())
"icon" -> jsonDecoder.json.decodeFromString<IconComponent>(json.toString())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.revenuecat.purchases.paywalls.components.common

import com.revenuecat.purchases.InternalRevenueCatAPI
import com.revenuecat.purchases.paywalls.components.HeaderComponent
import com.revenuecat.purchases.paywalls.components.StackComponent
import com.revenuecat.purchases.paywalls.components.StickyFooterComponent
import dev.drewhamilton.poko.Poko
Expand All @@ -23,4 +24,6 @@ public class PaywallComponentsConfig(
@get:JvmSynthetic
@SerialName("sticky_footer")
public val stickyFooter: StickyFooterComponent? = null,
@get:JvmSynthetic
public val header: HeaderComponent? = null,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.revenuecat.purchases.InternalRevenueCatAPI
import com.revenuecat.purchases.paywalls.components.ButtonComponent
import com.revenuecat.purchases.paywalls.components.CarouselComponent
import com.revenuecat.purchases.paywalls.components.CountdownComponent
import com.revenuecat.purchases.paywalls.components.HeaderComponent
import com.revenuecat.purchases.paywalls.components.IconComponent
import com.revenuecat.purchases.paywalls.components.ImageComponent
import com.revenuecat.purchases.paywalls.components.PackageComponent
Expand Down Expand Up @@ -43,6 +44,7 @@ internal fun PaywallComponent.filter(predicate: (PaywallComponent) -> Boolean):
is PurchaseButtonComponent -> queue.add(current.stack)
is ButtonComponent -> queue.add(current.stack)
is PackageComponent -> queue.add(current.stack)
is HeaderComponent -> queue.add(current.stack)
is StickyFooterComponent -> queue.add(current.stack)
is CarouselComponent -> queue.addAll(current.pages)
is TabControlButtonComponent -> queue.add(current.stack)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.revenuecat.purchases.paywalls.components

import com.revenuecat.purchases.ColorAlias
import com.revenuecat.purchases.JsonTools
import com.revenuecat.purchases.paywalls.components.common.LocalizationKey
import com.revenuecat.purchases.paywalls.components.properties.ColorInfo
import com.revenuecat.purchases.paywalls.components.properties.ColorScheme
import org.intellij.lang.annotations.Language
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized

@RunWith(Parameterized::class)
internal class HeaderComponentTests(
@Suppress("UNUSED_PARAMETER") name: String,
private val args: Args,
) {

class Args(
@Language("json")
val json: String,
val expected: HeaderComponent,
)

companion object {

@Suppress("LongMethod")
@JvmStatic
@Parameterized.Parameters(name = "{0}")
fun parameters(): Collection<*> = listOf(
arrayOf(
"non-empty stack",
Args(
json = """
{
"type": "header",
"stack": {
"type": "stack",
"components": [
{
"color": {
"light": {
"type": "alias",
"value": "primary"
}
},
"components": [],
"id": "xmpgCrN9Rb",
"name": "Text",
"text_lid": "7bkohQjzIE",
"type": "text"
}
]
}
}
""".trimIndent(),
expected = HeaderComponent(
stack = StackComponent(
components = listOf(
TextComponent(
text = LocalizationKey("7bkohQjzIE"),
color = ColorScheme(light = ColorInfo.Alias(ColorAlias("primary")))
)
),
)
)
),
),
arrayOf(
"empty stack",
Args(
json = """
{
"type": "header",
"stack": {
"type": "stack",
"components": []
}
}
""".trimIndent(),
expected = HeaderComponent(
stack = StackComponent(
components = emptyList(),
)
)
),
),
arrayOf(
"extra fields ignored",
Args(
json = """
{
"type": "header",
"id": "header_1",
"name": "My Header",
"stack": {
"type": "stack",
"components": []
}
}
""".trimIndent(),
expected = HeaderComponent(
stack = StackComponent(
components = emptyList(),
)
)
),
),
)
}

@Test
fun `Should properly deserialize HeaderComponent as HeaderComponent`() {
// Arrange, Act
val actual = JsonTools.json.decodeFromString<HeaderComponent>(args.json)

// Assert
assert(actual == args.expected)
}

@Test
fun `Should properly deserialize HeaderComponent as PaywallComponent`() {
// Arrange, Act
val actual = JsonTools.json.decodeFromString<PaywallComponent>(args.json)

// Assert
assert(actual == args.expected)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.revenuecat.purchases.paywalls.components.common

import com.revenuecat.purchases.ColorAlias
import com.revenuecat.purchases.JsonTools
import com.revenuecat.purchases.paywalls.components.HeaderComponent
import com.revenuecat.purchases.paywalls.components.StackComponent
import com.revenuecat.purchases.paywalls.components.StickyFooterComponent
import com.revenuecat.purchases.paywalls.components.TextComponent
Expand Down Expand Up @@ -284,6 +285,190 @@ internal class ComponentsConfigTests {
)
),
),
arrayOf(
"header present",
Args(
json = """
{
"stack": {
"type": "stack",
"components": []
},
"background": {
"type": "color",
"value": {
"light": {
"type": "alias",
"value": "primary"
}
}
},
"header": {
"type": "header",
"stack": {
"type": "stack",
"components": [
{
"color": {
"light": {
"type": "alias",
"value": "primary"
}
},
"components": [],
"id": "xmpgCrN9Rb",
"name": "Text",
"text_lid": "7bkohQjzIE",
"type": "text"
}
]
}
}
}
""".trimIndent(),
expected = PaywallComponentsConfig(
stack = StackComponent(
components = emptyList(),
),
background = Background.Color(
value = ColorScheme(
light = ColorInfo.Alias(ColorAlias("primary"))
)
),
header = HeaderComponent(
stack = StackComponent(
components = listOf(
TextComponent(
text = LocalizationKey("7bkohQjzIE"),
color = ColorScheme(light = ColorInfo.Alias(ColorAlias("primary")))
)
),
)
)
)
),
),
arrayOf(
"header absent",
Args(
json = """
{
"stack": {
"type": "stack",
"components": []
},
"background": {
"type": "color",
"value": {
"light": {
"type": "alias",
"value": "primary"
}
}
}
}
""".trimIndent(),
expected = PaywallComponentsConfig(
stack = StackComponent(
components = emptyList(),
),
background = Background.Color(
value = ColorScheme(
light = ColorInfo.Alias(ColorAlias("primary"))
)
)
)
),
),
arrayOf(
"header null",
Args(
json = """
{
"stack": {
"type": "stack",
"components": []
},
"background": {
"type": "color",
"value": {
"light": {
"type": "alias",
"value": "primary"
}
}
},
"header": null
}
""".trimIndent(),
expected = PaywallComponentsConfig(
stack = StackComponent(
components = emptyList(),
),
background = Background.Color(
value = ColorScheme(
light = ColorInfo.Alias(ColorAlias("primary"))
)
)
)
),
),
arrayOf(
"header and sticky footer present",
Args(
json = """
{
"stack": {
"type": "stack",
"components": []
},
"background": {
"type": "color",
"value": {
"light": {
"type": "alias",
"value": "primary"
}
}
},
"header": {
"type": "header",
"stack": {
"type": "stack",
"components": []
}
},
"sticky_footer": {
"type": "sticky_footer",
"stack": {
"type": "stack",
"components": []
}
}
}
""".trimIndent(),
expected = PaywallComponentsConfig(
stack = StackComponent(
components = emptyList(),
),
background = Background.Color(
value = ColorScheme(
light = ColorInfo.Alias(ColorAlias("primary"))
)
),
header = HeaderComponent(
stack = StackComponent(
components = emptyList(),
)
),
stickyFooter = StickyFooterComponent(
stack = StackComponent(
components = emptyList(),
)
)
)
),
),
arrayOf(
"unknown background type",
Args(
Expand Down
Loading