Skip to content

Commit

Permalink
feature(dynamicPlans): Fix free plan currency
Browse files Browse the repository at this point in the history
  • Loading branch information
MargeBot committed Oct 16, 2023
2 parents 450c5d1 + 18b0e8f commit 0517e11
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2008,7 +2008,7 @@ let package = Package(
),
.package(
url: "https://github.com/ProtonMail/apple-fusion",
from: "2.0.1"
"2.0.1"..<"3.0.0"
),
.package(
url: "https://github.com/kylef/JSONSchema.swift",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public enum PUITranslations: TranslationsExposing {
case plan_details_high_speed
case plan_details_highest_speed
case plan_details_multi_user_support
case plan_details_free_price
case plan_details_free_description
case plan_details_plus_description
case plan_limited_time_offer
Expand Down Expand Up @@ -174,6 +175,8 @@ public enum PUITranslations: TranslationsExposing {
return localized(key: "Highest speed", comment: "Plan details highest speed message")
case .plan_details_multi_user_support:
return localized(key: "Multi-user support", comment: "Plan details multi-user support message")
case .plan_details_free_price:
return localized(key: "Free", comment: "Plan price when it's free")
case .plan_details_free_description:
return localized(key: "The basic for private and secure communications.", comment: "Plan details free description")
case .plan_details_plus_description:
Expand Down
5 changes: 4 additions & 1 deletion libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
import Foundation

enum PriceFormatter {
static func formatPlanPrice(price: Double, locale: Locale, maximumFractionDigits: Int = 2) -> String {
static func formatPlanPrice(price: Double, locale: Locale = Locale.current, currencyCode: String? = nil, maximumFractionDigits: Int = 2) -> String {
let formatter = NumberFormatter()
formatter.numberStyle = .currency
formatter.locale = locale
if let currencyCode {
formatter.currencyCode = currencyCode
}
formatter.maximumFractionDigits = maximumFractionDigits
let priceString = formatter.string(from: NSNumber(value: price)) ?? ""
return priceString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,5 @@
"Pay annually" = "Pay annually";

"Pay every two years" = "Pay every two years";

"Free" = "Free";
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ struct AvailablePlansDetails {
cycleDescription: nil,
defaultCycle: defaultCycle,
cycle: nil,
price: PriceFormatter.formatPlanPrice(price: 0, locale: Locale.current, maximumFractionDigits: 0),
price: PUITranslations.plan_details_free_price.l10n,
decorations: decorations,
entitlements: entitlements
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,10 @@ struct CurrentPlanDetailsV5 {
}

var price: String
if let amount = details.amount {
price = PriceFormatter.formatPlanPrice(price: Double(amount) / 100, locale: Locale(identifier: "en-US"))
if let amount = details.amount, amount != 0 {
price = PriceFormatter.formatPlanPrice(price: Double(amount) / 100, currencyCode: details.currency)
} else {
price = PriceFormatter.formatPlanPrice(price: 0, locale: Locale.current, maximumFractionDigits: 0)
price = PUITranslations.plan_details_free_price.l10n
}

return .init(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ final class AvailablePlansDetailsTests: XCTestCase {
XCTAssertEqual(plan?.title, "title")
XCTAssertNil(plan?.iapID)
XCTAssertNil(plan?.description)
XCTAssertEqual(plan?.price, "$0")
XCTAssertEqual(plan?.price, "Free")
XCTAssertEqual(plan?.decorations.isEmpty, true)
XCTAssertEqual(plan?.entitlements[0], .init(text: "text", iconUrl: nil))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ final class CurrentPlanDetailsV5Tests: XCTestCase {
XCTAssertEqual(plan.cycleDescription, "cycleDescription")
XCTAssertEqual(plan.title, "title")
XCTAssertEqual(plan.description, "description")
XCTAssertEqual(plan.price, "$0")
XCTAssertEqual(plan.price, "Free")
XCTAssertNil(plan.endDate)
XCTAssertEqual(plan.entitlements, [.description(.init(text: "text"))])

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ final class CurrentPlanPresentationTests: XCTestCase {
XCTAssertEqual(sut.details.title, "title")
XCTAssertEqual(sut.details.description, "description")
XCTAssertEqual(sut.details.cycleDescription, "cycleDescription")
XCTAssertEqual(sut.details.price, "$0")
XCTAssertEqual(sut.details.price, "Free")
XCTAssertNil(sut.details.endDate)
XCTAssertTrue(sut.details.entitlements.isEmpty)
}
Expand Down

0 comments on commit 0517e11

Please sign in to comment.