Skip to content

Commit dee70ca

Browse files
committed
fix(RevenueCatUI): accept integral double writes to integer state keys (iOS parity)
1 parent bdc5fac commit dee70ca

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

ui/revenuecatui/src/main/kotlin/com/revenuecat/purchases/ui/revenuecatui/data/PaywallStateStore.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@ private fun JsonPrimitive.matchesDeclaredType(declaredType: StateDeclaration.Val
6767
when (declaredType) {
6868
StateDeclaration.ValueType.BOOLEAN -> !isString && booleanOrNull != null
6969
StateDeclaration.ValueType.STRING -> isString
70-
StateDeclaration.ValueType.INTEGER -> !isString && longOrNull != null
70+
// Integral doubles are accepted for parity with iOS, which coerces them via Int(exactly:).
71+
StateDeclaration.ValueType.INTEGER -> !isString && (longOrNull != null || doubleOrNull?.rem(1.0) == 0.0)
7172
StateDeclaration.ValueType.DOUBLE -> !isString && doubleOrNull != null
7273
StateDeclaration.ValueType.UNKNOWN -> false
7374
}

ui/revenuecatui/src/test/kotlin/com/revenuecat/purchases/ui/revenuecatui/data/PaywallStateStoreTests.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ internal class PaywallStateStoreTests {
7474
assertThat(store.currentValueOrDefault("open")).isEqualTo(JsonPrimitive(false))
7575
}
7676

77+
@Test
78+
fun `accepts an integral double write to an integer-typed key`() {
79+
val store = PaywallStateStore(mapOf("slide" to declaration(StateDeclaration.ValueType.INTEGER, JsonPrimitive(0))))
80+
81+
store.applyUpdates(listOf(setUpdate("slide", StateUpdateValue.Literal(JsonPrimitive(2.0)))))
82+
83+
assertThat(store.currentValueOrDefault("slide")).isEqualTo(JsonPrimitive(2.0))
84+
}
85+
86+
@Test
87+
fun `ignores a fractional double write to an integer-typed key`() {
88+
val store = PaywallStateStore(mapOf("slide" to declaration(StateDeclaration.ValueType.INTEGER, JsonPrimitive(0))))
89+
90+
store.applyUpdates(listOf(setUpdate("slide", StateUpdateValue.Literal(JsonPrimitive(2.5)))))
91+
92+
assertThat(store.currentValueOrDefault("slide")).isEqualTo(JsonPrimitive(0))
93+
}
94+
7795
@Test
7896
fun `accepts an integer write to a double-typed key`() {
7997
val store = PaywallStateStore(mapOf("ratio" to declaration(StateDeclaration.ValueType.DOUBLE, JsonPrimitive(0.0))))

0 commit comments

Comments
 (0)