Skip to content

Commit

Permalink
Paywalls: Add support for SUB_DURATION variable in paywalls (#1276)
Browse files Browse the repository at this point in the history
### Description
Adds support for the `sub_duration` variable in paywalls.

---------

Co-authored-by: Cesar de la Vega <cesarvegaro@gmail.com>
  • Loading branch information
tonidero and vegaro committed Oct 31, 2023
1 parent e72a2d8 commit 41f4058
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.revenuecat.purchases.ui.revenuecatui.data.processed

import android.icu.text.MeasureFormat
import com.revenuecat.purchases.Package
import com.revenuecat.purchases.PackageType
import com.revenuecat.purchases.ui.revenuecatui.R
Expand Down Expand Up @@ -44,8 +45,10 @@ internal class VariableDataProvider(
return stringId?.let { applicationContext.getString(it) } ?: ""
}

fun subscriptionDuration(rcPackage: Package): String? {
return "SUBS_DURATION"
fun subscriptionDuration(rcPackage: Package, locale: Locale): String? {
return rcPackage.product.period?.let { period ->
period.localizedPeriod(locale, formatWidth = MeasureFormat.FormatWidth.WIDE)
} ?: periodName(rcPackage)
}

fun introductoryOfferDuration(rcPackage: Package): String? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ internal object VariableProcessor {
"product_name" -> variableDataProvider.productName(rcPackage)
"sub_period" -> variableDataProvider.periodName(rcPackage)
"sub_price_per_month" -> variableDataProvider.localizedPricePerMonth(rcPackage, locale)
"sub_duration" -> variableDataProvider.subscriptionDuration(rcPackage)
"sub_duration" -> variableDataProvider.subscriptionDuration(rcPackage, locale)
"sub_offer_duration" -> variableDataProvider.introductoryOfferDuration(rcPackage)
"sub_offer_price" -> variableDataProvider.localizedIntroductoryOfferPrice(rcPackage)
else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import android.icu.util.MeasureUnit
import com.revenuecat.purchases.models.Period
import java.util.Locale

internal fun Period.localizedPeriod(locale: Locale): String {
return MeasureFormat.getInstance(locale, MeasureFormat.FormatWidth.SHORT).format(
internal fun Period.localizedPeriod(
locale: Locale,
formatWidth: MeasureFormat.FormatWidth = MeasureFormat.FormatWidth.SHORT,
): String {
return MeasureFormat.getInstance(locale, formatWidth).format(
Measure(value, unit.measureUnit),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,15 @@ class VariableProcessorTest {
@Test
fun `process variables processes price_per_period`() {
expectVariablesResult("{{ price_per_period }}", "$67.99/yr")
expectVariablesResult("{{ price_per_period }}", "$7.99/mth", rcPackage = TestData.Packages.monthly)
expectVariablesResult("{{ price_per_period }}", "$1.99/wk", rcPackage = TestData.Packages.weekly)
}

@Test
fun `process variables processes price_per_period localized in spanish`() {
expectVariablesResult("{{ price_per_period }}", "$67.99/a", esLocale)
expectVariablesResult("{{ price_per_period }}", "$7.99/m.", esLocale, TestData.Packages.monthly)
expectVariablesResult("{{ price_per_period }}", "$1.99/sem.", esLocale, TestData.Packages.weekly)
}

@Test
Expand Down Expand Up @@ -130,9 +134,19 @@ class VariableProcessorTest {

@Test
fun `process variables processes sub_duration`() {
expectVariablesResult("{{ sub_duration }}", "SUBS_DURATION")
expectVariablesResult("{{ sub_duration }}", "1 year")
expectVariablesResult("{{ sub_duration }}", "1 month", rcPackage = TestData.Packages.monthly)
expectVariablesResult("{{ sub_duration }}", "1 week", rcPackage = TestData.Packages.weekly)
}

@Test
fun `process variables processes sub_duration in spanish`() {
expectVariablesResult("{{ sub_duration }}", "1 año", esLocale)
expectVariablesResult("{{ sub_duration }}", "1 mes", esLocale, rcPackage = TestData.Packages.monthly)
expectVariablesResult("{{ sub_duration }}", "1 semana", esLocale, rcPackage = TestData.Packages.weekly)
}


@Test
fun `process variables processes sub_offer_duration`() {
expectVariablesResult("{{ sub_offer_duration }}", "INT_OFFER_DURATION")
Expand Down

0 comments on commit 41f4058

Please sign in to comment.