Skip to content

Commit

Permalink
Fixed serialisation of computed property requests
Browse files Browse the repository at this point in the history
- Added Android test 18 - an occurrence limit of once per minute.
- Added Android test 19 – a daysSince test that only matches when one day has passed since the last `once_a_minute` event.
  • Loading branch information
yusuftor committed Oct 24, 2023
1 parent 6ea5fba commit 3356eef
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ fun UITestTable() {
UITestHandler.test82Info to { CoroutineScope(Dispatchers.IO).launch { UITestHandler.test82() } },
UITestHandler.testAndroid4Info to { CoroutineScope(Dispatchers.IO).launch { UITestHandler.testAndroid4() } },
UITestHandler.testAndroid9Info to { CoroutineScope(Dispatchers.IO).launch { UITestHandler.testAndroid9() } },
UITestHandler.testAndroid18Info to { CoroutineScope(Dispatchers.IO).launch { UITestHandler.testAndroid18() } },
UITestHandler.testAndroid19Info to { CoroutineScope(Dispatchers.IO).launch { UITestHandler.testAndroid19() } },
)

LazyColumn {
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/com/superwall/superapp/test/UITestHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1351,5 +1351,28 @@ class UITestHandler {
suspend fun testAndroid9() {
Superwall.instance.register(event = "one_time_occurrence")
}

var testAndroid18Info = UITestInfo(
18,
"Tap launch button. It should display the paywall. Tap again and it should NOT " +
"display again within a minute, printing a NoRuleMatch in the console. After a " +
"minute, tap again, and it should show. You will need to delete and reinstall " +
"the app to test this again.",
testCaseType = TestCaseType.Android
)
suspend fun testAndroid18() {
Superwall.instance.register(event = "once_a_minute")
}

var testAndroid19Info = UITestInfo(
19,
"Tap launch button. The paywall should not display until one day or longer has " +
"passed since the last once_a_minute event. You'll get a NoRuleMatch in the " +
"console.",
testCaseType = TestCaseType.Android
)
suspend fun testAndroid19() {
Superwall.instance.register(event = "one_day_since_last_event")
}
}
}
2 changes: 1 addition & 1 deletion superwall/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ plugins {
}


version = "1.0.0-alpha08"
version = "1.0.0-alpha09"

android {
compileSdk = 33
Expand Down
2 changes: 1 addition & 1 deletion superwall/src/main/java/com/superwall/sdk/Superwall.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.filterNotNull
import java.util.*

public class Superwall(
class Superwall(
context: Context,
apiKey: String,
purchaseController: PurchaseController?,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import com.superwall.sdk.config.models.SurveyShowCondition
import com.superwall.sdk.config.models.SurveyShowConditionSerializer
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.Serializer
import java.util.Calendar
import java.util.Date

Expand All @@ -13,11 +18,21 @@ data class ComputedPropertyRequest(
/**
* The type of device property to compute.
*/
@Serializable(with = ComputedPropertyRequestTypeSerializer::class)
enum class ComputedPropertyRequestType(val rawValue: String) {
@SerialName("MINUTES_SINCE")
MINUTES_SINCE("MINUTES_SINCE"),

@SerialName("HOUSE_SINCE")
HOURS_SINCE("HOURS_SINCE"),

@SerialName("DAYS_SINCE")
DAYS_SINCE("DAYS_SINCE"),

@SerialName("MONTHS_SINCE")
MONTHS_SINCE("MONTHS_SINCE"),

@SerialName("YEARS_SINCE")
YEARS_SINCE("YEARS_SINCE");

val prefix: String
Expand Down Expand Up @@ -47,18 +62,21 @@ data class ComputedPropertyRequest(
YEARS_SINCE -> components[Calendar.YEAR]
}
}
}

fun dateComponent(date: Date): Int {
val calendar = Calendar.getInstance()
calendar.time = date
return calendar.get(calendarComponent)
@Serializer(forClass = ComputedPropertyRequestType::class)
object ComputedPropertyRequestTypeSerializer : KSerializer<ComputedPropertyRequestType> {
override fun serialize(
encoder: kotlinx.serialization.encoding.Encoder,
value: ComputedPropertyRequestType
) {
encoder.encodeString(value.rawValue)
}

companion object {
fun fromRawValue(value: String): ComputedPropertyRequestType? {
return values().find { it.rawValue == value }
?: throw IllegalArgumentException("Unsupported computed property type.")
}
override fun deserialize(decoder: kotlinx.serialization.encoding.Decoder): ComputedPropertyRequestType {
val rawValue = decoder.decodeString()
return ComputedPropertyRequestType.values().find { it.rawValue == rawValue }
?: throw IllegalArgumentException("Unsupported computed property type.")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ data class Paywall(
),

@SerialName("presentation_style_v2")
private val presensentationStyle: String,
private val presentationStyle: String,

private val presentationCondition: String,

Expand All @@ -64,7 +64,7 @@ data class Paywall(

var featureGating: FeatureGatingBehavior = FeatureGatingBehavior.NonGated,

@SerialName("computedProperties")
@SerialName("computed_properties")
var computedPropertyRequests: List<ComputedPropertyRequest> = emptyList(),

/**
Expand All @@ -87,7 +87,7 @@ data class Paywall(
init {
productIds = products.map { it.id }
presentation = Presentation(
style = PaywallPresentationStyle.valueOf(presensentationStyle.uppercase()),
style = PaywallPresentationStyle.valueOf(presentationStyle.uppercase()),
condition = PresentationCondition.valueOf(presentationCondition.uppercase())
)
}
Expand Down Expand Up @@ -166,7 +166,7 @@ data class Paywall(
PaywallPresentationStyle.MODAL,
PresentationCondition.CHECK_USER_SUBSCRIPTION
),
presensentationStyle = "MODAL",
presentationStyle = "MODAL",
presentationCondition = "CHECK_USER_SUBSCRIPTION",
backgroundColorHex = "000000",
products = arrayListOf(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ data class TriggerRule(
val expression: String? = null,
val expressionJs: String? = null,
val occurrence: TriggerRuleOccurrence? = null,
@SerialName("computedProperties")
@SerialName("computed_properties")
val computedPropertyRequests: List<ComputedPropertyRequest> = emptyList()
) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ class CoreDataManager(
}

return request.type.dateComponent(componentsMap)


} catch (error: Exception) {
Logger.debug(
logLevel = LogLevel.error,
Expand Down

0 comments on commit 3356eef

Please sign in to comment.