From afbb9a77b10101ee733f3b24c2f6759933640ccf Mon Sep 17 00:00:00 2001 From: NachoSoto Date: Thu, 13 Jul 2023 14:16:41 -0700 Subject: [PATCH] `Paywalls`: added `price` variable (#2809) --- .../Helpers/Package+VariableDataProvider.swift | 4 ++++ RevenueCatUI/Helpers/Variables.swift | 2 ++ RevenueCatUI/TestData.swift | 2 +- Tests/RevenueCatUITests/VariablesTests.swift | 10 ++++++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/RevenueCatUI/Helpers/Package+VariableDataProvider.swift b/RevenueCatUI/Helpers/Package+VariableDataProvider.swift index 054094ea22..222fbdc829 100644 --- a/RevenueCatUI/Helpers/Package+VariableDataProvider.swift +++ b/RevenueCatUI/Helpers/Package+VariableDataProvider.swift @@ -4,6 +4,10 @@ import RevenueCat @available(iOS 16.0, macOS 13.0, tvOS 16.0, *) extension Package: VariableDataProvider { + var localizedPrice: String { + return self.storeProduct.localizedPriceString + } + var localizedPricePerMonth: String { return self.priceFormatter.string(from: self.pricePerMonth) ?? "" } diff --git a/RevenueCatUI/Helpers/Variables.swift b/RevenueCatUI/Helpers/Variables.swift index a37b878218..c899a8f6c2 100644 --- a/RevenueCatUI/Helpers/Variables.swift +++ b/RevenueCatUI/Helpers/Variables.swift @@ -13,6 +13,7 @@ extension PaywallData.LocalizedConfiguration { /// A type that can provide necessary information for `VariableHandler` to replace variable content in strings. protocol VariableDataProvider { + var localizedPrice: String { get } var localizedPricePerMonth: String { get } var productName: String { get } var introductoryOfferDuration: String? { get } @@ -70,6 +71,7 @@ private extension VariableDataProvider { func value(for variableName: String) -> String { switch variableName { + case "price": return self.localizedPrice case "price_per_month": return self.localizedPricePerMonth case "product_name": return self.productName case "intro_duration": diff --git a/RevenueCatUI/TestData.swift b/RevenueCatUI/TestData.swift index 4b8c27d6c0..418d495915 100644 --- a/RevenueCatUI/TestData.swift +++ b/RevenueCatUI/TestData.swift @@ -100,7 +100,7 @@ internal enum TestData { private static let localization: PaywallData.LocalizedConfiguration = .init( title: "Ignite your child's curiosity", subtitle: "Get access to all our educational content trusted by thousands of parents.", - callToAction: "Continue", + callToAction: "Purchase for {{ price }}", callToActionWithIntroOffer: nil, offerDetails: "{{ price_per_month }} per month", offerDetailsWithIntroOffer: "Start your {{ intro_duration }} trial, then {{ price_per_month }} per month" diff --git a/Tests/RevenueCatUITests/VariablesTests.swift b/Tests/RevenueCatUITests/VariablesTests.swift index 846835d1d5..78723c8ddb 100644 --- a/Tests/RevenueCatUITests/VariablesTests.swift +++ b/Tests/RevenueCatUITests/VariablesTests.swift @@ -26,6 +26,11 @@ class VariablesTests: TestCase { expect(self.process("{{price_per_month}}")) == "{{price_per_month}}" } + func testPrice() { + self.provider.localizedPrice = "$10.99" + expect(self.process("Purchase for {{ price }}")) == "Purchase for $10.99" + } + func testPricePerMonth() { self.provider.localizedPricePerMonth = "$3.99" expect(self.process("{{ price_per_month }} per month")) == "$3.99 per month" @@ -54,11 +59,11 @@ class VariablesTests: TestCase { func testProcessesLocalizedConfiguration() { let configuration = PaywallData.LocalizedConfiguration( title: "Title {{ product_name }}", - subtitle: "Price: {{ price_per_month }}", + subtitle: "Price: {{ price }}", callToAction: "Unlock {{ product_name }} for {{ price_per_month }}", callToActionWithIntroOffer: "Start your {{ intro_duration }} free trial\n" + "Then {{ price_per_month }} every month", - offerDetails: "Purchase for {{ price_per_month }}", + offerDetails: "Purchase for {{ price }}", offerDetailsWithIntroOffer: "Start your {{ intro_duration }} free trial\n" + "Then {{ price_per_month }} every month" ) @@ -87,6 +92,7 @@ private extension VariablesTests { private struct MockVariableProvider: VariableDataProvider { + var localizedPrice: String = "" var localizedPricePerMonth: String = "" var productName: String = "" var introductoryOfferDuration: String?