Skip to content

Commit 7875cf2

Browse files
committed
refactor(RevenueCatUI): stateReader lambda in ConditionContext
1 parent 0cafd0d commit 7875cf2

2 files changed

Lines changed: 6 additions & 8 deletions

File tree

ui/revenuecatui/src/main/kotlin/com/revenuecat/purchases/ui/revenuecatui/components/PresentedPartial.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ internal fun <T : PartialComponent, P : PresentedPartial<P>> List<ComponentOverr
9393
internal class ConditionContext(
9494
val selectedPackageId: String?,
9595
val customVariables: Map<String, CustomVariableValue>,
96-
val stateValues: Map<String, JsonPrimitive> = emptyMap(),
97-
val stateDefaults: Map<String, JsonPrimitive> = emptyMap(),
96+
// Calls inside derivedStateOf subscribe only to the keys condition evaluation actually reads.
97+
val stateReader: (String) -> JsonPrimitive? = { null },
9898
)
9999

100100
/**
@@ -151,7 +151,7 @@ private fun ComponentOverride.Condition.evaluate(
151151
is ComponentOverride.Condition.PromoOfferRule -> evaluate(offerEligibility)
152152
is ComponentOverride.Condition.SelectedPackage -> evaluate(conditionContext.selectedPackageId)
153153
is ComponentOverride.Condition.Variable -> evaluate(conditionContext.customVariables)
154-
is ComponentOverride.Condition.State -> evaluate(conditionContext.stateValues, conditionContext.stateDefaults)
154+
is ComponentOverride.Condition.State -> evaluate(conditionContext.stateReader)
155155
ComponentOverride.Condition.Unsupported -> false
156156
}
157157

@@ -205,12 +205,11 @@ private fun ComponentOverride.Condition.Variable.matchesValue(
205205
private const val STATE_NUMBER_COMPARISON_EPSILON = 1e-10
206206

207207
private fun ComponentOverride.Condition.State.evaluate(
208-
stateValues: Map<String, JsonPrimitive>,
209-
stateDefaults: Map<String, JsonPrimitive>,
208+
stateReader: (String) -> JsonPrimitive?,
210209
): Boolean {
211210
// An undeclared key (absent from both the store and the declared defaults) never applies its override,
212211
// regardless of operator.
213-
val current = stateValues[name] ?: stateDefaults[name] ?: return false
212+
val current = stateReader(name) ?: return false
214213
val matches = matchesValue(current)
215214
return when (operator) {
216215
ComponentOverride.EqualityOperator.EQUALS -> matches

ui/revenuecatui/src/test/kotlin/com/revenuecat/purchases/ui/revenuecatui/components/BuildPresentedPartialTests.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,8 +2165,7 @@ internal class BuildPresentedPartialTests(@Suppress("UNUSED_PARAMETER") name: St
21652165
conditionContext = ConditionContext(
21662166
selectedPackageId = args.selectedPackageId,
21672167
customVariables = args.customVariables,
2168-
stateValues = args.stateValues,
2169-
stateDefaults = args.stateDefaults,
2168+
stateReader = { key -> args.stateValues[key] ?: args.stateDefaults[key] },
21702169
),
21712170
)
21722171

0 commit comments

Comments
 (0)