Skip to content

Commit

Permalink
feat: currencySymbol and toreProductDiscount localizedPriceString
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangyu committed Sep 2, 2024
1 parent 308966c commit a39b759
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ protocol ISKProduct {

/// The currency code for the product's price.
var currencyCode: String? { get }

/// The currency Symbol for the product's price.
var currencySymbol: String? { get }

/// The price of the product in decimal format.
var price: Decimal { get }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ protocol IStoreProductDiscount: Sendable {

/// The discounted price in the specified currency.
var price: Decimal { get }

/// A localized string representing the price of the product.
var localizedPriceString: String? { get }

/// The payment mode associated with the discount (e.g., freeTrial, payUpFront, payAsYouGo).
var paymentMode: PaymentMode { get }
Expand Down
4 changes: 4 additions & 0 deletions Sources/Flare/Classes/Models/Internal/SK1StoreProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ extension SK1StoreProduct: ISKProduct {
var currencyCode: String? {
product.priceLocale.currencyCodeID
}

var currencySymbol: String? {
numberFormatter.currencySymbol
}

var price: Decimal {
product.price as Decimal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ struct SK1StoreProductDiscount: IStoreProductDiscount {

/// The discounted price in the specified currency.
let price: Decimal

/// A localized string representing the price of the product.
let localizedPriceString: String?

/// The payment mode associated with the discount (e.g., freeTrial, payUpFront, payAsYouGo).
let paymentMode: PaymentMode
Expand Down Expand Up @@ -56,5 +59,9 @@ struct SK1StoreProductDiscount: IStoreProductDiscount {
self.subscriptionPeriod = subscriptionPeriod
numberOfPeriods = productDiscount.numberOfPeriods
type = discountType

/// The price formatter.
let numberFormatter: NumberFormatter = .numberFormatter(with: self.productDiscount.priceLocale)
localizedPriceString = numberFormatter.string(from: self.productDiscount.price)
}
}
7 changes: 7 additions & 0 deletions Sources/Flare/Classes/Models/Internal/SK2StoreProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ final class SK2StoreProduct {
private var currencyFormat: Decimal.FormatStyle.Currency {
product.priceFormatStyle
}

/// The price formatter.
private lazy var numberFormatter: NumberFormatter = .numberFormatter(with: self.currencyFormat.locale)

// MARK: Initialization

Expand All @@ -42,6 +45,10 @@ extension SK2StoreProduct: ISKProduct {
var currencyCode: String? {
currencyFormat.currencyCode
}

var currencySymbol: String? {
numberFormatter.currencySymbol
}

var price: Decimal {
product.price
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ struct SK2StoreProductDiscount: IStoreProductDiscount, Sendable {

/// The discounted price in the specified currency.
let price: Decimal

/// A localized string representing the price of the product.
let localizedPriceString: String?

/// The payment mode associated with the discount (e.g., freeTrial, payUpFront, payAsYouGo).
let paymentMode: PaymentMode
Expand Down Expand Up @@ -58,5 +61,6 @@ struct SK2StoreProductDiscount: IStoreProductDiscount, Sendable {
self.subscriptionPeriod = subscriptionPeriod
numberOfPeriods = subscriptionOffer.periodCount
type = discountType
localizedPriceString = subscriptionOffer.displayPrice
}
}
4 changes: 4 additions & 0 deletions Sources/Flare/Classes/Models/StoreProduct.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ extension StoreProduct: ISKProduct {
public var currencyCode: String? {
product.currencyCode
}

public var currencySymbol: String? {
product.currencySymbol
}

public var price: Decimal {
product.price
Expand Down
4 changes: 4 additions & 0 deletions Sources/Flare/Classes/Models/StoreProductDiscount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ extension StoreProductDiscount: IStoreProductDiscount {
public var price: Decimal {
discount.price
}

public var localizedPriceString: String? {
discount.localizedPriceString
}

public var paymentMode: PaymentMode {
discount.paymentMode
Expand Down
10 changes: 10 additions & 0 deletions Sources/FlareMock/Mocks/ProductMock.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public final class ProductMock: ISKProduct {
invokedCurrencyCodeGetterCount += 1
return stubbedCurrencyCode
}

public var invokedCurrencySymbolGetter = false
public var invokedCurrencySymbolGetterCount = 0
public var stubbedCurrencySymbol: String!

public var currencySymbol: String? {
invokedCurrencySymbolGetter = true
invokedCurrencySymbolGetterCount += 1
return stubbedCurrencySymbol
}

public var invokedPriceGetter = false
public var invokedPriceGetterCount = 0
Expand Down

0 comments on commit a39b759

Please sign in to comment.