Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -3,6 +3,7 @@ package com.superwall.sdk.compose
import android.app.Activity
import android.content.Context
import android.content.ContextWrapper
import android.view.View
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -92,9 +93,6 @@ fun PaywallComposable(
when {
viewState.value != null -> {
viewState.value?.let { viewToRender ->
LaunchedEffect(viewToRender) {
viewToRender.onViewCreated()
}
val themeChanged = rememberThemeChanged()
AndroidView(
modifier = modifier,
Expand All @@ -103,7 +101,21 @@ fun PaywallComposable(
it.onThemeChanged()
}
},
factory = { context ->
factory = {
if (viewToRender.isAttachedToWindow) {
viewToRender.onViewCreated()
} else {
viewToRender.addOnAttachStateChangeListener(
object : View.OnAttachStateChangeListener {
override fun onViewAttachedToWindow(view: View) {
view.removeOnAttachStateChangeListener(this)
viewToRender.onViewCreated()
}

override fun onViewDetachedFromWindow(view: View) = Unit
},
)
}
viewToRender
},
Comment thread
AndroidPoet marked this conversation as resolved.
onRelease = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ sealed class TrackingLogic {
input?.let { value ->
when (value) {
is List<*> -> null
is Map<*, *> -> value
is Map<*, *> -> cleanMap(value)
is String -> value
is Int, is Float, is Double, is Long, is Boolean -> value
is JsonElement -> value.convertFromJsonElement()
Expand All @@ -161,6 +161,28 @@ sealed class TrackingLogic {
}
}

private fun cleanNested(input: Any?): Any? =
input?.let { value ->
when (value) {
is List<*> -> cleanList(value)
is Map<*, *> -> cleanMap(value)
else -> clean(value)
}
}

private fun cleanMap(value: Map<*, *>): Map<String, Any>? =
value
.mapNotNull { (key, nestedValue) ->
val cleanedValue = cleanNested(nestedValue) ?: return@mapNotNull null
key?.toString()?.let { it to cleanedValue }
}.toMap()
.takeIf { it.isNotEmpty() }

private fun cleanList(value: List<*>): List<Any>? {
val cleanedValues = value.mapNotNull { cleanNested(it) }
return cleanedValues.ifEmpty { null }
}

@Throws(Exception::class)
fun checkNotSuperwallEvent(event: String) {
// Try to create a SuperwallEvents event from the event string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,45 @@ class TrackingLogicTest {
assertEquals("https://example.com", parameters.delegateParams["uri_value"])
}

@Test
fun processParameters_recursivelyCleansNestedMaps() =
runBlocking {
val trackable =
TrackableTestUtils.fakeTrackable(
rawName = "event_a",
superwallParams =
mapOf(
"customer_info" to
mapOf(
"userId" to "user-123",
"subscriptions" to
listOf(
mapOf(
"productId" to "pro_monthly",
"unsupported" to Any(),
"nullValue" to null,
),
),
"emptyMap" to mapOf("unsupported" to Any()),
"unsupported" to Any(),
),
),
canImplicitlyTrigger = false,
)

val parameters = TrackingLogic.processParameters(trackable, appSessionId = "session-123")

val customerInfo = parameters.delegateParams["customer_info"] as Map<*, *>
val subscriptions = customerInfo["subscriptions"] as List<*>
val subscription = subscriptions.first() as Map<*, *>
assertEquals("user-123", customerInfo["userId"])
assertEquals("pro_monthly", subscription["productId"])
assertFalse(subscription.containsKey("unsupported"))
assertFalse(subscription.containsKey("nullValue"))
assertFalse(customerInfo.containsKey("emptyMap"))
assertFalse(customerInfo.containsKey("unsupported"))
}

@Test
fun isNotDisabledVerboseEvent_handlesVariousEventTypes() {
val paywallInfo = PaywallInfo.empty()
Expand Down