Skip to content

Commit

Permalink
Paywalls: fixed {{ total_price_and_per_month }} (#2881)
Browse files Browse the repository at this point in the history
We were relying on both the price and price per month to be formatted
the same way. When debugging in the simulator, the actual region doesn't
change, so StoreKit's internal price formatter doesn't pick it up.
To make this more resilient, this now checks whether it's a monthly
subscription instead of comparing strings.

This is an example where `Locale.current` is only overriden partially
and the logic breaks:

![image](https://github.com/RevenueCat/purchases-ios/assets/685609/76eabfdb-ea16-4d67-b7b3-411a3c4393a5)
  • Loading branch information
NachoSoto authored Jul 26, 2023
1 parent 24b07ba commit 0b19fc6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
11 changes: 5 additions & 6 deletions RevenueCatUI/Data/Variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extension PaywallData.LocalizedConfiguration {
/// A type that can provide necessary information for `VariableHandler` to replace variable content in strings.
protocol VariableDataProvider {

var isMonthly: Bool { get }

var localizedPrice: String { get }
var localizedPricePerMonth: String { get }
var productName: String { get }
Expand Down Expand Up @@ -86,14 +88,11 @@ private extension VariableDataProvider {
case "price": return self.localizedPrice
case "price_per_month": return self.localizedPricePerMonth
case "total_price_and_per_month":
let price = self.localizedPrice
let perMonth = self.localizedPricePerMonth

if price == perMonth {
return price
if self.isMonthly {
return self.localizedPrice
} else {
let unit = Localization.abbreviatedUnitLocalizedString(for: .month, locale: locale)
return "\(price) (\(perMonth)/\(unit))"
return "\(self.localizedPrice) (\(self.localizedPricePerMonth)/\(unit))"
}

case "product_name": return self.productName
Expand Down
4 changes: 4 additions & 0 deletions RevenueCatUI/Helpers/Package+VariableDataProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import RevenueCat
@available(iOS 16.0, macOS 13.0, tvOS 16.0, *)
extension Package: VariableDataProvider {

var isMonthly: Bool {
return self.packageType == .monthly
}

var localizedPrice: String {
return self.storeProduct.localizedPriceString
}
Expand Down
4 changes: 3 additions & 1 deletion Tests/RevenueCatUITests/Data/VariablesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,16 @@ class VariablesTests: TestCase {
}

func testTotalPriceAndPerMonthWithDifferentPricesFrench() {
self.provider.isMonthly = false
self.provider.localizedPrice = "49,99€"
self.provider.localizedPricePerMonth = "4,16€"
expect(self.process("{{ total_price_and_per_month }}",
locale: .init(identifier: "fr_FR"))) == "49,99€ (4,16€/m)"
}

func testTotalPriceAndPerMonthWithSamePrice() {
self.provider.isMonthly = true
self.provider.localizedPrice = "$4.99"
self.provider.localizedPricePerMonth = "$4.99"
expect(self.process("{{ total_price_and_per_month }}")) == "$4.99"
}

Expand Down Expand Up @@ -125,6 +126,7 @@ private extension VariablesTests {

private struct MockVariableProvider: VariableDataProvider {

var isMonthly: Bool = false
var localizedPrice: String = ""
var localizedPricePerMonth: String = ""
var productName: String = ""
Expand Down

0 comments on commit 0b19fc6

Please sign in to comment.