Skip to content

Commit

Permalink
feat(GiniBankSDK): SkontoViewModel: new setPrice reusable method with…
Browse files Browse the repository at this point in the history
… validatedPrice. validatePrice method. Localization update

PP-680
  • Loading branch information
mrkulik committed Oct 29, 2024
1 parent 18b8cbd commit 8f1357b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 %@";
Expand Down

0 comments on commit 8f1357b

Please sign in to comment.