From 8f1357bb332f2d46f9d695d96aae31ddebcd9c27 Mon Sep 17 00:00:00 2001 From: Gleb Kulik Date: Tue, 29 Oct 2024 09:09:48 +0100 Subject: [PATCH] feat(GiniBankSDK): SkontoViewModel: new setPrice reusable method with validatedPrice. validatePrice method. Localization update PP-680 --- .../Core/Skonto/Models/SkontoViewModel.swift | 57 ++++++++++++------- .../Resources/de.lproj/Localizable.strings | 3 +- .../Resources/en.lproj/Localizable.strings | 3 +- 3 files changed, 40 insertions(+), 23 deletions(-) diff --git a/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Core/Skonto/Models/SkontoViewModel.swift b/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Core/Skonto/Models/SkontoViewModel.swift index 1b6fa983a..f3299aca5 100644 --- a/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Core/Skonto/Models/SkontoViewModel.swift +++ b/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Core/Skonto/Models/SkontoViewModel.swift @@ -158,35 +158,54 @@ class SkontoViewModel { } func setSkontoAmountToPayPrice(_ price: String) { - guard let price = convertPriceStringToPrice(price: price), - price.value <= amountToPay.value else { - let errorMessage = NSLocalizedStringPreferredGiniBankFormat("ginibank.skonto.withdiscount.validation", - comment: "Discounted value cannot exceed initial value") - setErrorMessage(errorMessage) - notifyStateChangeHandlers() - return + setPrice( + price, + maxValue: amountToPay.value, + errorMessage: NSLocalizedStringPreferredGiniBankFormat("ginibank.skonto.withdiscount.validation", + comment: "Discounted value cannot exceed initial...") + ) { validatedPrice in + skontoAmountToPay = validatedPrice + updateDocumentPagesModelData() + recalculateSkontoPercentage() } - skontoAmountToPay = price - updateDocumentPagesModelData() - recalculateSkontoPercentage() - notifyStateChangeHandlers() } func setAmountToPayPrice(_ price: String) { - guard let price = convertPriceStringToPrice(price: price), - price.value <= maximumAmountToPayValue else { - let errorMessage = NSLocalizedStringPreferredGiniBankFormat("ginibank.skonto.withoutdiscount.validation", - comment: "Discounted value cannot exceed initial value") - setErrorMessage(errorMessage) + setPrice( + price, + maxValue: maximumAmountToPayValue, + errorMessage: NSLocalizedStringPreferredGiniBankFormat("ginibank.skonto.withoutdiscount.validation", + comment: "Your transfer limit has been exceeded: %@") + ) { validatedPrice in + amountToPay = validatedPrice + recalculateAmountToPayWithSkonto() + updateDocumentPagesModelData() + } + } + + private func setPrice(_ price: String, maxValue: Decimal, errorMessage: String, completion: (Price) -> Void) { + let validationMessage = validatePrice(price, maxValue: maxValue, errorMessage: errorMessage) + if let validationMessage { + setErrorMessage(validationMessage) notifyStateChangeHandlers() return } - amountToPay = price - recalculateAmountToPayWithSkonto() - updateDocumentPagesModelData() + guard let validatedPrice = convertPriceStringToPrice(price: price) else { return } + completion(validatedPrice) notifyStateChangeHandlers() } + private func validatePrice(_ price: String, maxValue: Decimal, errorMessage: String) -> String? { + guard let convertedPrice = convertPriceStringToPrice(price: price), convertedPrice.value <= maxValue else { + let formatter = NumberFormatter.twoDecimalPriceFormatter + if let maxPriceString = formatter.string(from: NSDecimalNumber(decimal: maxValue)) { + return String.localizedStringWithFormat(errorMessage, maxPriceString) + } + return errorMessage + } + return nil + } + func setExpiryDate(_ date: Date) { dueDate = date recalculateRemainingDays() diff --git a/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/de.lproj/Localizable.strings b/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/de.lproj/Localizable.strings index 58f41f922..b0f436dc9 100644 --- a/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/de.lproj/Localizable.strings +++ b/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/de.lproj/Localizable.strings @@ -100,8 +100,7 @@ "ginibank.skonto.withoutdiscount.price.title" = "Betrag ohne Abzug"; "ginibank.skonto.withdiscount.validation" = "Der Betrag mit Skonto darf nicht höher sein als ohne"; -// TODO: 680 finalise string -"ginibank.skonto.withoutdiscount.validation" = "Der Betrag mit Skonto darf nicht höher sein als ohne"; +"ginibank.skonto.withoutdiscount.validation" = "Ihr Überweisungslimit wurde überschritten: %@"; "ginibank.skonto.total.title" = "Gesamtpreis"; "ginibank.skonto.total.savings" = "%@ sparen"; diff --git a/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/en.lproj/Localizable.strings b/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/en.lproj/Localizable.strings index fa87b235b..a6c38eb7f 100644 --- a/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/en.lproj/Localizable.strings +++ b/BankSDK/GiniBankSDK/Sources/GiniBankSDK/Resources/en.lproj/Localizable.strings @@ -107,8 +107,7 @@ "ginibank.skonto.withoutdiscount.price.title" = "Full amount"; "ginibank.skonto.withdiscount.validation" = "Discounted value cannot exceed initial value"; -// TODO: 680 finalise string -"ginibank.skonto.withoutdiscount.validation" = "Discounted value cannot exceed initial value"; +"ginibank.skonto.withoutdiscount.validation" = "Your transfer limit has been exceeded: %@"; "ginibank.skonto.total.title" = "Total"; "ginibank.skonto.total.savings" = "Save %@";