From 13e8a8041bfff4be77dbba86a5c4e306566325a3 Mon Sep 17 00:00:00 2001 From: magohamote Date: Mon, 16 Oct 2023 15:42:45 +0200 Subject: [PATCH 1/2] feature(dynamicPlans): Fix free plan currency Jira-Id: CP-6633 --- Package.swift | 2 +- .../Sources/Extensions/PaymentsUI+Translations.swift | 3 +++ libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift | 5 ++++- .../Resources/Translations/en.lproj/Localizable.strings | 2 ++ .../PlanPresentation/V5/AvailablePlansDetails.swift | 2 +- .../PlanPresentation/V5/CurrentPlanDetailsV5.swift | 4 ++-- .../Tests/UnitTests/AvailablePlansDetailsTests.swift | 2 +- .../Tests/UnitTests/CurrentPlanDetailsV5Tests.swift | 2 +- .../Tests/UnitTests/CurrentPlanPresentationTests.swift | 2 +- 9 files changed, 16 insertions(+), 8 deletions(-) diff --git a/Package.swift b/Package.swift index 4400d4103..d71389bdc 100644 --- a/Package.swift +++ b/Package.swift @@ -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", diff --git a/libraries/PaymentsUI/Sources/Extensions/PaymentsUI+Translations.swift b/libraries/PaymentsUI/Sources/Extensions/PaymentsUI+Translations.swift index 7d6411703..f667395b3 100644 --- a/libraries/PaymentsUI/Sources/Extensions/PaymentsUI+Translations.swift +++ b/libraries/PaymentsUI/Sources/Extensions/PaymentsUI+Translations.swift @@ -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 @@ -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: diff --git a/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift b/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift index c72dd1ea1..b9fc7cb84 100644 --- a/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift +++ b/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift @@ -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, currency: String? = nil, maximumFractionDigits: Int = 2) -> String { let formatter = NumberFormatter() formatter.numberStyle = .currency formatter.locale = locale + if let currency { + formatter.currencyCode = currency + } formatter.maximumFractionDigits = maximumFractionDigits let priceString = formatter.string(from: NSNumber(value: price)) ?? "" return priceString diff --git a/libraries/PaymentsUI/Sources/Resources/Translations/en.lproj/Localizable.strings b/libraries/PaymentsUI/Sources/Resources/Translations/en.lproj/Localizable.strings index e1df721f1..ffc5a3c0c 100644 --- a/libraries/PaymentsUI/Sources/Resources/Translations/en.lproj/Localizable.strings +++ b/libraries/PaymentsUI/Sources/Resources/Translations/en.lproj/Localizable.strings @@ -129,3 +129,5 @@ "Pay annually" = "Pay annually"; "Pay every two years" = "Pay every two years"; + +"Free" = "Free"; diff --git a/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/AvailablePlansDetails.swift b/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/AvailablePlansDetails.swift index c3f72d755..9db20d9b9 100644 --- a/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/AvailablePlansDetails.swift +++ b/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/AvailablePlansDetails.swift @@ -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 ) diff --git a/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift b/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift index 2d47f875b..7a09a3151 100644 --- a/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift +++ b/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift @@ -74,9 +74,9 @@ struct CurrentPlanDetailsV5 { var price: String if let amount = details.amount { - price = PriceFormatter.formatPlanPrice(price: Double(amount) / 100, locale: Locale(identifier: "en-US")) + price = PriceFormatter.formatPlanPrice(price: Double(amount) / 100, currency: details.currency) } else { - price = PriceFormatter.formatPlanPrice(price: 0, locale: Locale.current, maximumFractionDigits: 0) + price = PUITranslations.plan_details_free_price.l10n } return .init( diff --git a/libraries/PaymentsUI/Tests/UnitTests/AvailablePlansDetailsTests.swift b/libraries/PaymentsUI/Tests/UnitTests/AvailablePlansDetailsTests.swift index 9a39eb40b..a461b49c1 100644 --- a/libraries/PaymentsUI/Tests/UnitTests/AvailablePlansDetailsTests.swift +++ b/libraries/PaymentsUI/Tests/UnitTests/AvailablePlansDetailsTests.swift @@ -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)) } diff --git a/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanDetailsV5Tests.swift b/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanDetailsV5Tests.swift index 0b2ecbf89..9fb600021 100644 --- a/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanDetailsV5Tests.swift +++ b/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanDetailsV5Tests.swift @@ -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"))]) diff --git a/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanPresentationTests.swift b/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanPresentationTests.swift index 906aef9bb..ee021c829 100644 --- a/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanPresentationTests.swift +++ b/libraries/PaymentsUI/Tests/UnitTests/CurrentPlanPresentationTests.swift @@ -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) } From 18b0e8f07c2fe60cfa0a0a785c39668752a6cbca Mon Sep 17 00:00:00 2001 From: magohamote Date: Mon, 16 Oct 2023 17:28:09 +0200 Subject: [PATCH 2/2] address comment --- .../PaymentsUI/Sources/Extensions/PriceFormatter.swift | 6 +++--- .../PlanPresentation/V5/CurrentPlanDetailsV5.swift | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift b/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift index b9fc7cb84..67c02d8f1 100644 --- a/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift +++ b/libraries/PaymentsUI/Sources/Extensions/PriceFormatter.swift @@ -22,12 +22,12 @@ import Foundation enum PriceFormatter { - static func formatPlanPrice(price: Double, locale: Locale = Locale.current, currency: String? = nil, 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 currency { - formatter.currencyCode = currency + if let currencyCode { + formatter.currencyCode = currencyCode } formatter.maximumFractionDigits = maximumFractionDigits let priceString = formatter.string(from: NSNumber(value: price)) ?? "" diff --git a/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift b/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift index 7a09a3151..f5c135692 100644 --- a/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift +++ b/libraries/PaymentsUI/Sources/ViewModels/PlanPresentation/V5/CurrentPlanDetailsV5.swift @@ -73,8 +73,8 @@ struct CurrentPlanDetailsV5 { } var price: String - if let amount = details.amount { - price = PriceFormatter.formatPlanPrice(price: Double(amount) / 100, currency: details.currency) + if let amount = details.amount, amount != 0 { + price = PriceFormatter.formatPlanPrice(price: Double(amount) / 100, currencyCode: details.currency) } else { price = PUITranslations.plan_details_free_price.l10n }