Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Paywalls V2] Parses UiConfig #2068

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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 @@ -7,6 +7,7 @@ package com.revenuecat.purchases

import com.revenuecat.purchases.paywalls.PaywallData
import com.revenuecat.purchases.paywalls.components.common.PaywallComponentsData
import dev.drewhamilton.poko.Poko

/**
* An offering is a collection of [Package] available for the user to purchase.
Expand All @@ -27,8 +28,14 @@ constructor(
val availablePackages: List<Package>,
val paywall: PaywallData? = null,
@InternalRevenueCatAPI
val paywallComponents: PaywallComponentsData? = null,
val paywallComponents: PaywallComponents? = null,
) {
@InternalRevenueCatAPI
@Poko
class PaywallComponents(
val uiConfig: UiConfig,
val data: PaywallComponentsData,
)

/**
* Lifetime package type configured in the RevenueCat dashboard, if available.
Expand Down
79 changes: 79 additions & 0 deletions purchases/src/main/kotlin/com/revenuecat/purchases/UiConfig.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.revenuecat.purchases

import com.revenuecat.purchases.paywalls.components.common.LocaleId
import com.revenuecat.purchases.paywalls.components.common.VariableLocalizationKey
import com.revenuecat.purchases.paywalls.components.properties.ColorScheme
import dev.drewhamilton.poko.Poko
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@InternalRevenueCatAPI
@Serializable
@JvmInline
value class ColorAlias(@get:JvmSynthetic val value: String)

@InternalRevenueCatAPI
@Serializable
@JvmInline
value class FontAlias(@get:JvmSynthetic val value: String)

@InternalRevenueCatAPI
@Poko
@Serializable
class UiConfig(
@get:JvmSynthetic
val app: AppConfig = AppConfig(),
@get:JvmSynthetic
val localizations: Map<LocaleId, Map<VariableLocalizationKey, String>> = emptyMap(),
@SerialName("variable_config")
@get:JvmSynthetic
val variableConfig: VariableConfig = VariableConfig(),
) {

@InternalRevenueCatAPI
@Poko
@Serializable
class AppConfig(
@get:JvmSynthetic
val colors: Map<ColorAlias, ColorScheme> = emptyMap(),
@get:JvmSynthetic
val fonts: Map<FontAlias, FontsConfig> = emptyMap(),
) {
@InternalRevenueCatAPI
@Poko
@Serializable
class FontsConfig(
@get:JvmSynthetic
val android: FontInfo,
) {

@InternalRevenueCatAPI
@Serializable
sealed interface FontInfo {
@InternalRevenueCatAPI
@Poko
@Serializable
@SerialName("name")
class Name(@get:JvmSynthetic val value: String) : FontInfo

@InternalRevenueCatAPI
@Poko
@Serializable
@SerialName("google_fonts")
class GoogleFonts(@get:JvmSynthetic val value: String) : FontInfo
}
}
}

@InternalRevenueCatAPI
@Poko
@Serializable
class VariableConfig(
@SerialName("variable_compatibility_map")
@get:JvmSynthetic
val variableCompatibilityMap: Map<String, String> = emptyMap(),
@SerialName("function_compatibility_map")
@get:JvmSynthetic
val functionCompatibilityMap: Map<String, String> = emptyMap(),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.revenuecat.purchases.Offerings
import com.revenuecat.purchases.Package
import com.revenuecat.purchases.PackageType
import com.revenuecat.purchases.PresentedOfferingContext
import com.revenuecat.purchases.UiConfig
import com.revenuecat.purchases.api.BuildConfig
import com.revenuecat.purchases.models.StoreProduct
import com.revenuecat.purchases.paywalls.PaywallData
Expand Down Expand Up @@ -38,16 +39,29 @@ internal abstract class OfferingParser {
/**
* Note: this may return an empty Offerings.
*/
@OptIn(InternalRevenueCatAPI::class)
fun createOfferings(offeringsJson: JSONObject, productsById: Map<String, List<StoreProduct>>): Offerings {
log(LogIntent.DEBUG, OfferingStrings.BUILDING_OFFERINGS.format(productsById.size))

val jsonOfferings = offeringsJson.getJSONArray("offerings")
val currentOfferingID = offeringsJson.getString("current_offering_id")

val uiConfigJson = offeringsJson.optJSONObject("ui_config")

@Suppress("TooGenericExceptionCaught")
val uiConfig: UiConfig? = uiConfigJson?.let {
try {
json.decodeFromString<UiConfig>(it.toString())
} catch (e: Throwable) {
errorLog("Error deserializing ui_config", e)
null
}
}

val offerings = mutableMapOf<String, Offering>()
for (i in 0 until jsonOfferings.length()) {
val offeringJson = jsonOfferings.getJSONObject(i)
createOffering(offeringJson, productsById)?.let {
createOffering(offeringJson, productsById, uiConfig)?.let {
offerings[it.identifier] = it

if (it.availablePackages.isEmpty()) {
Expand Down Expand Up @@ -94,7 +108,11 @@ internal abstract class OfferingParser {

@OptIn(InternalRevenueCatAPI::class)
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
fun createOffering(offeringJson: JSONObject, productsById: Map<String, List<StoreProduct>>): Offering? {
fun createOffering(
offeringJson: JSONObject,
productsById: Map<String, List<StoreProduct>>,
uiConfig: UiConfig?,
): Offering? {
val offeringIdentifier = offeringJson.getString("identifier")
val metadata = offeringJson.optJSONObject("metadata")?.toMap<Any>(deep = true) ?: emptyMap()
val jsonPackages = offeringJson.getJSONArray("packages")
Expand Down Expand Up @@ -135,14 +153,20 @@ internal abstract class OfferingParser {
null
}

val paywallComponents = if (paywallComponentsData != null && uiConfig != null) {
Offering.PaywallComponents(uiConfig, paywallComponentsData)
} else {
null
}

return if (availablePackages.isNotEmpty()) {
Offering(
offeringIdentifier,
offeringJson.getString("description"),
metadata,
availablePackages,
paywallData,
paywallComponentsData,
paywallComponents,
)
} else {
null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.revenuecat.purchases.paywalls.components.properties.ThemeImageUrls
import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.InternalSerializationApi
import kotlinx.serialization.KSerializer
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.SerializationException
import kotlinx.serialization.descriptors.PolymorphicKind
Expand Down Expand Up @@ -64,3 +65,134 @@ private object LocalizationDataSerializer : KSerializer<LocalizationData> {
decoder.decodeSerializableValue(LocalizationData.Image.serializer())
}
}

/**
* Keys for localized strings used as variable values.
*/
@InternalRevenueCatAPI
@Serializable
enum class VariableLocalizationKey {

@SerialName("day")
DAY,

@SerialName("daily")
DAILY,

@SerialName("day_short")
DAY_SHORT,

@SerialName("week")
WEEK,

@SerialName("weekly")
WEEKLY,

@SerialName("week_short")
WEEK_SHORT,

@SerialName("month")
MONTH,

@SerialName("monthly")
MONTHLY,

@SerialName("month_short")
MONTH_SHORT,

@SerialName("year")
YEAR,

@SerialName("yearly")
YEARLY,

@SerialName("year_short")
YEAR_SHORT,

@SerialName("annual")
ANNUAL,

@SerialName("annually")
ANNUALLY,

@SerialName("annual_short")
ANNUAL_SHORT,

@SerialName("free_price")
FREE_PRICE,

@SerialName("percent")
PERCENT,

@SerialName("num_day_zero")
NUM_DAY_ZERO,

@SerialName("num_day_one")
NUM_DAY_ONE,

@SerialName("num_day_two")
NUM_DAY_TWO,

@SerialName("num_day_few")
NUM_DAY_FEW,

@SerialName("num_day_many")
NUM_DAY_MANY,

@SerialName("num_day_other")
NUM_DAY_OTHER,

@SerialName("num_week_zero")
NUM_WEEK_ZERO,

@SerialName("num_week_one")
NUM_WEEK_ONE,

@SerialName("num_week_two")
NUM_WEEK_TWO,

@SerialName("num_week_few")
NUM_WEEK_FEW,

@SerialName("num_week_many")
NUM_WEEK_MANY,

@SerialName("num_week_other")
NUM_WEEK_OTHER,

@SerialName("num_month_zero")
NUM_MONTH_ZERO,

@SerialName("num_month_one")
NUM_MONTH_ONE,

@SerialName("num_month_two")
NUM_MONTH_TWO,

@SerialName("num_month_few")
NUM_MONTH_FEW,

@SerialName("num_month_many")
NUM_MONTH_MANY,

@SerialName("num_month_other")
NUM_MONTH_OTHER,

@SerialName("num_year_zero")
NUM_YEAR_ZERO,

@SerialName("num_year_one")
NUM_YEAR_ONE,

@SerialName("num_year_two")
NUM_YEAR_TWO,

@SerialName("num_year_few")
NUM_YEAR_FEW,

@SerialName("num_year_many")
NUM_YEAR_MANY,

@SerialName("num_year_other")
NUM_YEAR_OTHER,
}
Loading