Skip to content

Commit

Permalink
Use the modern currency API on Transaction on newer OS versions
Browse files Browse the repository at this point in the history
This fixes #158
  • Loading branch information
Jeehut committed Jun 28, 2024
1 parent ddd53e4 commit a329390
Showing 1 changed file with 30 additions and 10 deletions.
40 changes: 30 additions & 10 deletions Sources/TelemetryClient/Presets/TelemetryDeck+Purchases.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,29 @@ extension TelemetryDeck {
let priceValueInNativeCurrency = NSDecimalNumber(decimal: transaction.price ?? Decimal()).doubleValue

let priceValueInUSD: Double
if transaction.currencyCode == "USD" {
priceValueInUSD = priceValueInNativeCurrency
} else if
let currencyCode = transaction.currencyCode,
let oneUSDExchangeRate = self.currencyCodeToOneUSDExchangeRate[currencyCode]
{
priceValueInUSD = priceValueInNativeCurrency / oneUSDExchangeRate

if #available(iOS 16, macOS 13, tvOS 16, visionOS 1, watchOS 9, *) {
if transaction.currency?.identifier == "USD" {
priceValueInUSD = priceValueInNativeCurrency
} else if
let currencyCode = transaction.currency?.identifier,
let oneUSDExchangeRate = self.currencyCodeToOneUSDExchangeRate[currencyCode]
{
priceValueInUSD = priceValueInNativeCurrency / oneUSDExchangeRate
} else {
priceValueInUSD = 0
}
} else {
priceValueInUSD = 0
if transaction.currencyCode == "USD" {
priceValueInUSD = priceValueInNativeCurrency
} else if
let currencyCode = transaction.currencyCode,
let oneUSDExchangeRate = self.currencyCodeToOneUSDExchangeRate[currencyCode]
{
priceValueInUSD = priceValueInNativeCurrency / oneUSDExchangeRate
} else {
priceValueInUSD = 0
}
}

#if os(visionOS)
Expand All @@ -44,8 +58,14 @@ extension TelemetryDeck {
"TelemetryDeck.Purchase.countryCode": countryCode,
]

if let currencyCode = transaction.currencyCode {
purchaseParameters["TelemetryDeck.Purchase.currencyCode"] = currencyCode
if #available(iOS 16, macOS 13, tvOS 16, visionOS 1, watchOS 9, *) {
if let currencyCode = transaction.currency?.identifier {
purchaseParameters["TelemetryDeck.Purchase.currencyCode"] = currencyCode
}
} else {
if let currencyCode = transaction.currencyCode {
purchaseParameters["TelemetryDeck.Purchase.currencyCode"] = currencyCode
}
}

self.signal(
Expand Down

0 comments on commit a329390

Please sign in to comment.